1、题目大意:三个操作,换根,修改树上的某条路径,查询一个子树的最小值
2、分析:这个其实还是挺好做的,修改树上的某条路径,裸树剖,查询子树的最小值,这个是树剖满足dfs序
那么就是换根了,对吧,其实换根是o(1)的,就是root = u嘛。。。那么另两个操作就要变一变了,
那个修改路径是不变的,因为你不管怎么换根,树上的路径都是唯一的。。。这个结束
查询子树的最小值呢,分情况讨论,我分了三种 1.如果询问根,全询问,2.如果询问的是现在根的n辈祖先,
那么除了现在根的n-1辈祖先的那棵子树不询问以外,其余的都要查询3.否则就是原来的那棵子树
#include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> using namespace std; #define M 1000000 struct tree_chain_partition{ int Size[M], Top[M], Fa[M], value[M], num[M], Height[M], left[M], right[M]; int n; int root; int son[M], head[M], Next[M]; int tot, ST_tot; int q[M], lazy[M]; int tt[M][30]; void init(){ memset(lazy, -1, sizeof(lazy)); memset(head, -1, sizeof(head)); tot = ST_tot = 0; Top[1] = 1; } void pushdown(int o){ if(lazy[o] != -1){ q[2 * o] = q[2 * o + 1] = lazy[2 * o] = lazy[2 * o + 1] = lazy[o]; lazy[o] = -1; } } void add(int l, int r, int o, int x, int y, int k){ if(x <= l && r <= y){ q[o] = k; lazy[o] = k; return; } pushdown(o); int mid = (l + r) / 2; if(x <= mid) add(l, mid, 2 * o, x, y, k); if(y > mid) add(mid + 1, r, 2 * o + 1, x, y, k); q[o] = min(q[2 * o + 1], q[2 * o]); } int query(int l, int r, int o, int x, int y){ if(x > y) return 2147483647; if(x <= l && r <= y) return q[o]; pushdown(o); int mid = (l + r) / 2; int ret = 2147483647; if(x <= mid) ret = min(ret, query(l, mid, 2 * o, x, y)); if(y > mid) ret = min(ret, query(mid + 1, r, 2 * o + 1, x, y)); return ret; } inline void insert(int x, int y){ tot ++; son[tot] = y; Next[tot] = head[x]; head[x] = tot; } void dfs1(int x, int fa, int height){ Height[x] = height; Fa[x] = fa; for(int i = head[x]; i != -1; i = Next[i]) if(son[i] != fa){ dfs1(son[i], x, height + 1); Size[x] += Size[son[i]]; } Size[x] ++; } void dfs2(int x, int fa){ ST_tot ++; add(1, n, 1, ST_tot, ST_tot, value[x]); num[x] = ST_tot; left[x] = ST_tot; int o = 0, ss = 0; for(int i = head[x]; i != -1; i = Next[i]) if(son[i] != fa){ if(Size[son[i]] > ss){ o = i; ss = Size[son[i]]; } } if(o != 0){ Top[son[o]] = Top[x]; dfs2(son[o], x); } for(int i = head[x]; i != -1; i = Next[i]) if(o != i && son[i] != fa){ Top[son[i]] = son[i]; dfs2(son[i], x); } right[x] = ST_tot; } void init_lca(){ for(int i = 1; i <= n; i ++) tt[i][0] = Fa[i]; for(int i = 1; i <= 20; i ++){ for(int j = 1; j <= n; j ++){ tt[j][i] = tt[tt[j][i - 1]][i - 1]; } } } int zux(int x, int k){ for(int i = 0; i <= 20; i ++) if((1 << i) & k){ x = tt[x][i]; } return x; } bool is_(int x, int y){ if(Height[x] >= Height[y]) return false; if(zux(y, Height[y] - Height[x]) == x) return true; return false; } void add_root(int o){ root = o; } void real_add(int x, int y, int v){ while(Top[x] != Top[y]){ if(Height[Top[x]] < Height[Top[y]]) swap(x, y); add(1, n, 1, num[Top[x]], num[x], v); x = Fa[Top[x]]; } if(Height[x] < Height[y]) swap(x, y); add(1, n, 1, num[y], num[x], v); } int real_query(int x){ if(x == root){ return query(1, n, 1, left[1], right[1]); } else if(is_(x, root)){ int qt = zux(root, Height[root] - Height[x] - 1); return min(query(1, n, 1, left[1], left[qt] - 1), query(1, n, 1, right[qt] + 1, right[1])); } else{ return query(1, n, 1, left[x], right[x]); } } } wt; int main(){ int n, m; scanf("%d%d", &n, &m); wt.n = n; wt.init(); for(int i = 1; i < n; i ++){ int x, y; scanf("%d%d", &x, &y); wt.insert(x, y); wt.insert(y, x); } for(int i = 1; i <= n; i ++){ scanf("%d", &wt.value[i]); } scanf("%d", &wt.root); wt.dfs1(1, 0, 1); wt.dfs2(1, 0); wt.init_lca(); for(int i = 1; i <= m; i ++){ int op, x, y, z; scanf("%d", &op); if(op == 1){ scanf("%d", &x); wt.add_root(x); } else if(op == 2){ scanf("%d%d%d", &x, &y, &z); wt.real_add(x, y, z); } else{ scanf("%d", &x); printf("%d\n", wt.real_query(x)); } } return 0; }