BZOJ3083: 遥远的国度(树链剖分)
题意
$n$个节点的树,每个点有权值,支持三种操作
1、 换根
2、把$x$到$y$路径上节点权值变为$z$
3、询问路径最小值
Sol
啥?你说这是TopTree的裸题?那你写去啊
很显然,如果没有第一个操作就是树剖的裸题
其实有了第一个操作也是树剖的裸题
我们考虑换根之后会对那些节点产生影响
以下图片来自(https://blog.csdn.net/lcomyn/article/details/45718295)
第一种情况:x == root
很显然直接查询子树的最小值就行

第二种情况:$lca(x,root) != x$
这种情况也简单,直接查询$x$子树中的最小值即可

第三种情况:$lca(x,root) = x$
这种情况稍微复杂一些
我们需要找到$root$往上走,离$x$最近的点。
很显然,这个点以上的部分,就是我们要查询的区间
那么我们查询这个点的子树对应区间的补集即可

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int MAXN = 1e5 + , B = , INF = ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int N, M, root = ;
int a[MAXN], b[MAXN];
vector<int> v[MAXN];
int fa[MAXN], top[MAXN], jump[MAXN][], deep[MAXN], siz[MAXN], l[MAXN], r[MAXN], tot = , cnt, son[MAXN], ID[MAXN];
void dfs1(int x, int _fa) {
fa[x] = _fa; siz[x] = ; l[x] = ++cnt;
jump[x][] = fa[x];
for(int i = ; i < v[x].size(); i++) {
int to = v[x][i];
if(deep[to]) continue;
deep[to] = deep[x] + ;
dfs1(to, x);
siz[x] += siz[to];
if(siz[to] > siz[son[x]]) son[x] = to;
}
r[x] = cnt;
}
void dfs2(int x, int topf) {
top[x] = topf; ID[x] = ++tot; a[tot] = b[x];
l[x] = tot;
if(!son[x]) {r[x] = tot; return ;}
dfs2(son[x], topf);
for(int i = ; i < v[x].size(); i++) {
int to = v[x][i];
if(top[to]) continue;
dfs2(to, to);
}
r[x] = tot;
}
void Pre() {
for(int i = ; i <= B; i++)
for(int j = ; j <= N; j++)
jump[j][i] = jump[jump[j][i - ]][i - ];
}
#define ls k << 1
#define rs k << 1 | 1
struct Node {
int l, r, mi, si, tag;
}T[MAXN * ];
void update(int k) {T[k].mi = min(T[ls].mi, T[rs].mi);}
void ps(int k, int val) {T[k].mi = val; T[k].tag = val; return ;}
void pushdown(int k) {
if(!T[k].tag) return;
ps(ls, T[k].tag); ps(rs, T[k].tag);
T[k].tag = ;
}
void Build(int k, int ll, int rr) {
T[k].l = ll; T[k].r = rr; T[k].si = r - l + ;
if(ll == rr) {T[k].mi = a[ll]; return ;}
int mid = ll + rr >> ;
Build(ls, ll, mid); Build(rs, mid + , rr);
update(k);
}
void IntervalMem(int k, int ll, int rr, int val) {
if(ll <= T[k].l && T[k].r <= rr) {
T[k].mi = T[k].tag = val;
return;
}
pushdown(k);
int mid = T[k].l + T[k].r >> ;
if(ll <= mid) IntervalMem(ls, ll, rr, val);
if(rr > mid) IntervalMem(rs, ll, rr, val);
update(k);
}
void TreeChange(int x, int y, int val) {
while(top[x] != top[y]) {
if(deep[top[x]] < deep[top[y]]) swap(x, y);
IntervalMem(, ID[top[x]], ID[x], val);
x = fa[top[x]];
}
if(deep[x] < deep[y]) swap(x, y);
IntervalMem(, ID[y], ID[x], val);
}
int IntervalMin(int k, int ll, int rr) {
int ans = INF;
if(ll <= T[k].l && T[k].r <= rr) return T[k].mi;
pushdown(k);
int mid = (T[k].l + T[k].r) >> ;
if(ll <= mid) ans = min(ans, IntervalMin(ls, ll, rr));
if(rr > mid) ans = min(ans, IntervalMin(rs, ll, rr));
return ans;
}
int LCA(int x, int y) {
while(top[x] != top[y]) {
if(deep[top[x]] < deep[top[y]]) swap(x, y);
x = fa[top[x]];
}
if(deep[x] < deep[y]) swap(x, y);
return y;
}
int Find(int rt, int x) {
for(int i = B; i >= ; i--)
while(deep[jump[rt][i]] > deep[x])
rt = jump[rt][i];
return rt;
}
int Query(int x) {
if(x == root) return T[].mi;
int lca = LCA(x, root);
if(lca != x) return IntervalMin(, l[x], r[x]);
int v = Find(root, x), ans = INF;
if(l[v] > ) ans = min(ans, IntervalMin(, , l[v] - ));//tag
if(r[v] < N) ans = min(ans, IntervalMin(, r[v] + , tot));
return ans;
}
int main() {
N = read(); M = read();
for(int i = ; i <= N - ; i++) {
int x = read(), y = read();
v[x].push_back(y); v[y].push_back(x);
}
for(int i = ; i <= N; i++) b[i] = read();
root = read();
deep[] = ;
dfs1(, );
dfs2(, );
Pre();
Build(, , tot);
while(M--) {
int opt = read();
if(opt == ) root = read();
else if(opt == ){
int x1 = read(), x2 = read(), v = read();
TreeChange(x1, x2, v);
} else {
int x = read();
printf("%d\n", Query(x));
}
}
return ;
} /*
3 7
1 2
1 3
1 2 3
1
3 1
2 1 1 6
3 1
2 2 2 5
3 1
2 3 3 4
3 1
*/
BZOJ3083: 遥远的国度(树链剖分)的更多相关文章
- [日常摸鱼]bzoj3083遥远的国度-树链剖分
		一无聊就找树剖写 题意:一颗带点权的树,三种操作:1.换根 2.链赋值 3.查询子树最小值 如果没有换根的话直接就是裸的树剖了,对于换根的操作我们可以分类讨论. 1.如果查询的$x$就是根,那答案就是 ... 
- 【bzoj3083】遥远的国度  树链剖分+线段树
		题目描述 描述zcwwzdjn在追杀十分sb的zhx,而zhx逃入了一个遥远的国度.当zcwwzdjn准备进入遥远的国度继续追杀时,守护神RapiD阻拦了zcwwzdjn的去路,他需要zcwwzdjn ... 
- BZOJ 3083 遥远的国度 树链剖分
		3083: 遥远的国度 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 797 Solved: 181[Submit][Status] Descrip ... 
- BZOJ 3083 遥远的国度(树链剖分+LCA)
		Description 描述zcwwzdjn在追杀十分sb的zhx,而zhx逃入了一个遥远的国度.当zcwwzdjn准备进入遥远的国度继续追杀时,守护神RapiD阻拦了zcwwzdjn的去路,他需要z ... 
- BZOJ 3083: 遥远的国度(树链剖分+DFS序)
		可以很显而易见的看出,修改就是树链剖分,而询问就是在dfs出的线段树里查询最小值,但由于这道题会修改根节点,所以在查询的时候需判断x是否为root的祖先,如果不是就直接做,是的话应该查询从1-st[y ... 
- BZOJ 3083 遥远的国度 树链剖分+线段树
		有换根的树链剖分的裸题. 在换根的时候注意讨论. 注意数据范围要开unsigned int或longlong #include<iostream> #include<cstdio&g ... 
- 洛谷P3979 遥远的国度 树链剖分+分类讨论
		题意:给出一棵树,这棵树每个点有权值,然后有3种操作.操作一:修改树根为rt,操作二:修改u到v路径上点权值为w,操作三:询问以rt为根x子树的最小权值. 解法:如果没有修改树根操作那么这题就是树链剖 ... 
- BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]
		3083: 遥远的国度 Time Limit: 10 Sec Memory Limit: 1280 MBSubmit: 3127 Solved: 795[Submit][Status][Discu ... 
- luoguP3979 遥远的国度 树链剖分
		\(1, 2\)操作没什么好说的 对于\(3\)操作,分三种情况讨论下 \(id = rt\)的情况下,查整棵树的最小值即可 如果\(rt\)在\(1\)号点为根的情况下不在\(id\)的子树中,那么 ... 
随机推荐
- React引入,运行
			1.引入 <script src="https://cdn.bootcss.com/react/15.5.4/react.min.js"></script> ... 
- Python机器视觉编程常用数据结构与示例
			本文总结了使用Python进行机器视觉(图像处理)编程时常用的数据结构,主要包括以下内容: 数据结构 通用序列操作:索引(indexing).分片(slicing).加(adding).乘(multi ... 
- 字符串转UTF-8码(%开头)
			var str = '中'; var code = encodeURI(str); console.log(code); // => %E4%B8%AD 
- MongoDB复制集安全认证
			之前我有一篇博客写的是“node.js通过权限验证连接MongoDB”,这篇博客上提到如何在启动文件中通过配置auth参数来开启权限认证,但这种认证方式只适合单机节点,当我们使用复制集时应该怎么开启权 ... 
- 洛谷 P3957 跳房子 —— 二分答案+单调队列优化DP
			题目:https://www.luogu.org/problemnew/show/P3957 先二分一个 g,然后判断: 由于转移的范围是一个区间,也就是滑动窗口,所以单调队列优化: 可以先令队尾为 ... 
- RDA TDT & TOT
			首先看下面的TS PSI分析图: 注意:TOT UTC与TDT是一致的 TDT下的时间为: UTC+手动TIMEZONE TOT下的时间为: UTC+解析的time_offset time_offes ... 
- 腾讯视频API --关闭广告推荐
			官方文档:http://v.qq.com/open/doc/tvpapi2.0.pdf 使用: <script src="http://imgcache.qq.com/tencentv ... 
- BFS+PRIM
			转载请注明出处:優YoU http://user.qzone.qq.com/289065406/blog/1299324104 在一个y行 x列的迷宫中,有可行走的通路空格' ',不可行走的墙'#', ... 
- CF1059E Split the Tree(倍增)
			题意翻译 现有n个点组成一棵以1为根的有根树,第i个点的点权为wi,需将其分成若干条垂直路径使得每一个点当且仅当被一条垂直路径覆盖,同时,每条垂直路径长度不能超过L,点权和不能超过S,求最少需要几条垂 ... 
- 【POJ - 2664】Prerequisites? (排序+查找)
			Prerequisites? 原文是English,这里直接就写中文吧 题意简述 k:已经选择的科目数:m:选择的科目类别:c:能够选择的科目数.r:要求最少选择的科目数量 在输入的k和m以下的一行是 ... 
