实现了路径修改,子树查询,及换根。
换根其实很简单,分三种情况讨论,画画图就明白了。
#include <cstdio>
#include <algorithm>
using namespace std;
#define m ((L+R)>>1)
#define lc o<<1
#define rc o<<1|1
#define ls lc,L,m
#define rs rc,m+1,R const int N = , M = ;
int n,q,e,x,y,z,op,sd,tt;
int a[N],d[N],f[N],p[N],s[N],bg[N],ed[N],sz[N],tp[N],hd[N],nxt[M],to[M],v[N<<],mn[N<<];
void add(int x, int y) {to[++e] = y, nxt[e] = hd[x], hd[x] = e;} void dfs(int x) {
int mx = ; sz[x] = ;
for(int i = hd[x]; i; i = nxt[i]) if(!d[to[i]]) {
d[to[i]] = d[x]+, f[to[i]] = x, dfs(to[i]), sz[x] += sz[to[i]];
if(sz[to[i]] > mx) mx = sz[to[i]], s[x] = to[i];
}
}
void dfs2(int x) {
bg[x] = ++tt, p[tt] = x;
if(s[x]) tp[s[x]] = tp[x], dfs2(s[x]);
for(int i = hd[x]; i; i = nxt[i]) if(to[i] != f[x] && to[i] != s[x])
tp[to[i]] = to[i], dfs2(to[i]);
ed[x] = tt;
} void bd(int o, int L, int R) {
if(L == R) {mn[o] = a[p[L]]; return;}
bd(ls), bd(rs), mn[o] = min(mn[lc], mn[rc]);
}
void pd(int o) {
if(!v[o]) return;
mn[lc] = mn[rc] = v[o], v[lc] = v[rc] = v[o], v[o] = ;
}
void gai(int o, int L, int R, int l, int r, int x) {
if(L >= l && R <= r) {v[o] = mn[o] = x; return;}
pd(o);
if(l <= m) gai(ls, l, r, x); if(r > m) gai(rs, l, r, x);
mn[o] = min(mn[lc], mn[rc]);
}
int qry(int o, int L, int R, int l, int r) {
if(L >= l && R <= r) return mn[o];
pd(o);
if(r <= m) return qry(ls, l, r); if(l > m) return qry(rs, l, r);
return min(qry(ls, l, r), qry(rs, l, r));
} void upd(int x, int y, int z) {
while(tp[x]^tp[y]) {
if(d[tp[x]] < d[tp[y]]) swap(x, y);
gai(, , n, bg[tp[x]], bg[x], z), x = f[tp[x]];
}
if(d[x] < d[y]) swap(x, y);
gai(, , n, bg[y], bg[x], z);
} int main() {
scanf("%d%d", &n, &q);
for(int i = ; i < n; i++) scanf("%d%d", &x, &y), add(x, y), add(y, x);
for(int i = ; i <= n; i++) scanf("%d", &a[i]);
scanf("%d", &sd), d[sd] = , tp[sd] = sd, dfs(sd), dfs2(sd), bd(, , n);
while(q--) {
scanf("%d%d", &op, &x);
if(op == ) sd = x;
else if(op == ) scanf("%d%d", &y, &z), upd(x, y, z);
else {
if(sd == x) printf("%d\n", mn[]);
else if(bg[x] > bg[sd] || ed[x] < bg[sd]) printf("%d\n", qry(,,n,bg[x],ed[x]));
else {
for(int i = hd[x]; i; i = nxt[i])
if(to[i] != f[x] && bg[to[i]] <= bg[sd] && ed[to[i]] >= bg[sd]) {
int ans = qry(,,n,,bg[to[i]]-);
if(ed[to[i]]^n) ans = min(ans, qry(,,n,ed[to[i]]+,n));
printf("%d\n", ans);
break;
}
}
}
}
return ;
}