题目大意:有一棵$n$个点的树,每个点有一个点权,有三种操作:

  1. $1\;x:$把根变成$x$
  2. $2\;u\;v\;x:$把路径$u->v$上的点权改为$x$
  3. $3\;x:$询问以$x$为根的子树中最小的点权

题解:树剖,发现换根操作比较困难,可以进行一波分类讨论(下面的$lca$以及子树都是在以$1$为根的情况下(其实任意一个固定的点均可)

  1. $root=x:$就是询问整棵树
  2. $lca(root,x)\not=x:$就是正常询问$x$的子树
  3. $lca(root,x)=x:$就是整棵树减去$root$所在的子树

然后步骤三的减去$root$所在的子树中的找这棵子树可以用倍增来求

卡点:步骤三中只查询了$x$的子树减去$root$子树

C++ Code:

#include <algorithm>
#include <cstdio>
#include <cctype>
namespace std {
struct istream {
#define M (1 << 24 | 3)
char buf[M], *ch = buf - 1;
inline istream() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
fread(buf, 1, M, stdin);
}
inline istream& operator >> (int &x) {
while (isspace(*++ch));
for (x = *ch & 15; isdigit(*++ch); ) x = x * 10 + (*ch & 15);
return *this;
}
#undef M
} cin;
struct ostream {
#define M (1 << 24 | 3)
char buf[M], *ch = buf - 1;
inline ostream& operator << (int x) {
if (!x) {
*++ch = '0';
return *this;
}
static int S[20], *top; top = S;
while (x) {
*++top = x % 10 ^ 48;
x /= 10;
}
for (; top != S; --top) *++ch = *top;
return *this;
}
inline ostream& operator << (const char x) {*++ch = x; return *this;}
inline ~ostream() {
#ifndef ONLINE_JUDGE
freopen("output.txt", "w", stdout);
#endif
fwrite(buf, 1, ch - buf + 1, stdout);
}
#undef M
} cout;
} #define maxn 100010
const int inf = 0x7fffffff; int head[maxn], cnt;
struct Edge {
int to, nxt;
} e[maxn << 1];
inline void addedge(int a, int b) {
e[++cnt] = (Edge) {b, head[a]}; head[a] = cnt;
e[++cnt] = (Edge) {a, head[b]}; head[b] = cnt;
} int n, m;
int w[maxn], W[maxn];
namespace SgT {
int V[maxn << 2], tg[maxn << 2];
inline void pushdown(int rt) {
int &__tg = tg[rt];
V[rt << 1] = tg[rt << 1] = V[rt << 1 | 1] = tg[rt << 1 | 1] = __tg;
__tg = 0;
} int L, R, num;
void build(int rt, int l, int r) {
if (l == r) {
V[rt] = W[l];
return ;
}
int mid = l + r >> 1;
build(rt << 1, l, mid);
build(rt << 1 | 1, mid + 1, r);
V[rt] = std::min(V[rt << 1], V[rt << 1 | 1]);
}
void __modify(int rt, int l, int r) {
if (L <= l && R >= r) {
V[rt] = tg[rt] = num;
return ;
}
int mid = l + r >> 1;
if (tg[rt]) pushdown(rt);
if (L <= mid) __modify(rt << 1, l, mid);
if (R > mid) __modify(rt << 1 | 1, mid + 1, r);
V[rt] = std::min(V[rt << 1], V[rt << 1 | 1]);
}
void modify(int __L, int __R, int __num) {
L = __L, R = __R, num = __num;
__modify(1, 1, n);
} int ans;
void __query(int rt, int l, int r) {
if (L <= l && R >= r) {
ans = std::min(ans, V[rt]);
return ;
}
int mid = l + r >> 1;
if (tg[rt]) pushdown(rt);
if (L <= mid) __query(rt << 1, l, mid);
if (R > mid) __query(rt << 1 | 1, mid + 1, r);
}
int query(int __L, int __R) {
L = __L, R = __R;
ans = inf;
__query(1, 1, n);
return ans;
}
} int root;
int fa[maxn], sz[maxn], dfn[maxn], idx;
int son[maxn], top[maxn], dep[maxn]; namespace BZ {
#define M 17
int fa[maxn][M + 1];
inline void init(int u) {
*fa[u] = ::fa[u];
for (int i = 1; i <= M; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1];
}
inline int get_son(int x, int y) {
for (int i = M; ~i; i--) if (dep[fa[x][i]] > dep[y]) x = fa[x][i];
return x;
}
#undef M
}
using BZ::get_son; void dfs1(int u) {
BZ::init(u);
sz[u] = 1;
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (v != fa[u]) {
fa[v] = u;
dep[v] = dep[u] + 1;
dfs1(v);
sz[u] += sz[v];
if (!son[u] || sz[v] > sz[son[u]]) son[u] = v;
}
}
}
void dfs2(int u) {
dfn[u] = ++idx;
int v = son[u];
if (v) top[v] = top[u], dfs2(v);
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (v != fa[u] && v != son[u]) {
top[v] = v;
dfs2(v);
}
}
}
inline int LCA(int x, int y) {
if (x == y) return x;
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) std::swap(x, y);
x = fa[top[x]];
}
return dep[x] > dep[y] ? y : x;
}
void modify(int x, int y, int z) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) std::swap(x, y);
SgT::modify(dfn[top[x]], dfn[x], z);
x = fa[top[x]];
}
if (dep[x] > dep[y]) std::swap(x, y);
SgT::modify(dfn[x], dfn[y], z);
}
inline int query(int x) {
if (root == x) return SgT::query(1, n);
if (LCA(x, root) != x) return SgT::query(dfn[x], dfn[x] + sz[x] - 1);
const int S = get_son(root, x), l = dfn[S], r = dfn[S] + sz[S] - 1;
int ans = inf;
if (1 < l) ans = SgT::query(1, l - 1);
if (r < n) ans = std::min(ans, SgT::query(r + 1, n));
return ans;
} int main() {
std::cin >> n >> m;
for (int i = 1, a, b; i < n; i++) {
std::cin >> a >> b;
addedge(a, b);
}
dfs1(1);
dfs2(top[1] = 1);
for (int i = 1; i <= n; i++) std::cin >> w[i];
for (int i = 1; i <= n; i++) W[dfn[i]] = w[i];
SgT::build(1, 1, n);
std::cin >> root;
while (m --> 0) {
int op, u, v, x;
std::cin >> op >> u;
switch (op) {
case 1:
root = u;
break;
case 2:
std::cin >> v >> x;
modify(u, v, x);
break;
case 3:
std::cout << query(u) << '\n';
}
}
return 0;
}

  

[洛谷P3979]遥远的国度的更多相关文章

  1. 洛谷 P3979 遥远的国度

    题目描述 修改某条路径上的值以及询问子树的最小值都最树剖的基础操作,那么如何实现换根呢? 考虑一下三种情况: 1.rot=询问的子树x,答案就是整棵树的最小值 2.rot在x的子树里,只有rot到x这 ...

  2. 洛谷P3979 遥远的国度 树链剖分+分类讨论

    题意:给出一棵树,这棵树每个点有权值,然后有3种操作.操作一:修改树根为rt,操作二:修改u到v路径上点权值为w,操作三:询问以rt为根x子树的最小权值. 解法:如果没有修改树根操作那么这题就是树链剖 ...

  3. 遥远的国度 (树链剖分换根),洛谷P3979

    析:显然,若没有换根操作,则为树链剖分板子题,但是这道题我们考虑换根操作 考虑这样一个性质:在一棵树上,两点的距离路径是唯一的!! 也就是说,我们在修改路径上的点权时,不必考虑根在哪里,直接利用模板修 ...

  4. 洛谷P1514 [NOIP2010提高组T4]引水入城

    P1514 引水入城 题目描述 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个N 行M 列的矩形,如上图所示,其中每个格子都代表一座城市,每座城 ...

  5. 洛谷1640 bzoj1854游戏 匈牙利就是又短又快

    bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...

  6. 洛谷P1352 codevs1380 没有上司的舞会——S.B.S.

    没有上司的舞会  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond       题目描述 Description Ural大学有N个职员,编号为1~N.他们有 ...

  7. 洛谷P1108 低价购买[DP | LIS方案数]

    题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...

  8. 洛谷 P2701 [USACO5.3]巨大的牛棚Big Barn Label:二维数组前缀和 你够了 这次我用DP

    题目背景 (USACO 5.3.4) 题目描述 农夫约翰想要在他的正方形农场上建造一座正方形大牛棚.他讨厌在他的农场中砍树,想找一个能够让他在空旷无树的地方修建牛棚的地方.我们假定,他的农场划分成 N ...

  9. 洛谷P1710 地铁涨价

    P1710 地铁涨价 51通过 339提交 题目提供者洛谷OnlineJudge 标签O2优化云端评测2 难度提高+/省选- 提交  讨论  题解 最新讨论 求教:为什么只有40分 数组大小一定要开够 ...

随机推荐

  1. SpringBoot学习:整合MyBatis,使用Druid连接池

    项目下载地址:http://download.csdn.NET/detail/aqsunkai/9805821 (一)添加pom依赖: <!-- https://mvnrepository.co ...

  2. SSM-最新pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  3. egrep及扩展正则

    模式:pattern 正则: grep:基本正则,查找速度慢 Extended grep:扩展正则 fgrep:fast grep,不支持正则,直接查找字符串,执行速度快 基本正则: . :任意单个字 ...

  4. GIt学习第一天之安装和版本库创建

    搬运自 ‘廖雪峰的官方网站’ 1.git安装 官网下载地址:https://git-scm.com/download/win   百度网盘下载地址:https://pan.baidu.com/s/1k ...

  5. Linux命令应用大词典-第29章 SELinux管理

    29.1 sestaus:显示SElinux的状态 29.2 getenforce:显示当前SELinux的应用模式 29.3 setenforce:修改SELinux的应用模式 29.4 getfa ...

  6. Swift使用AVAudioPlayer来调节游戏的背景音乐大小

    音乐文件的声音大小有时在做为游戏背景音乐时会过大,而如果我们只是简单应用SKAudioNode来加载音乐的话,是无法进行声音大小的调节的,因此我们必须使用更强大的AVAudioPlayer来进行声音大 ...

  7. SVG Sprite 使用Symbol元素制作ICON

    介绍 SVG是一种全新的使用方式,应该说这才是未来的主流,也是平台目前推荐的用法.之前写过两篇关于CSS icon在页面显示的博客,后来了解到现在大多数前端团队和项目都在使用SVG Sprite这种方 ...

  8. Matlab 图象操作函数讲解

    h = imrect;pos = getPosition(h); 这个函数用来获取图象上特定区域的坐标,其中pos的返回值中有四个参数[xmin,ymin,width,height],特定区域的左上角 ...

  9. day-20 tensorflow持久化之入门学习

    如果不对模型参数进行保存,当训练结束以后,模型也在内存中被释放,下一轮又需要对模型进行重新训练,有没有一种方法,可以利用之前已经训练好的模型参数值,直接进行模型推理或者继续训练?这里需要引入一个数据之 ...

  10. 【转载】Android 内存溢出如何发生的。

    [转载]Android 内存溢出如何发生的. 且谈Android内存溢出 前言 关于android的内存溢出在创新文档库中也有不少,网络上也有很多这方面的资料.所以这遍文章不算是正真意义上的创新,仅仅 ...