Code:
// luogu-judger-enable-o2 #include<bits/stdc++.h> #define maxn 150000 #define ll long long #define inf 2147483647000 using namespace std; #define getset chil[x].empty()?inf:*chil[x].begin() void setIO(string s) { string in=s+".in", out=s+".out"; freopen(in.c_str(),"r",stdin); // freopen(out.c_str(),"w",stdout); } multiset<ll>chil[maxn]; ll value[maxn],mintot[maxn]; namespace tree { #define lson ch[x][0] #define rson ch[x][1] int ch[maxn][2], f[maxn], rev[maxn], sta[maxn]; ll tag[maxn]; int get(int x) { return ch[f[x]][1]==x; } int isroot(int x) { return (1^(ch[f[x]][1]==x||ch[f[x]][0]==x)); } void markrev(int x) { if(!x) return; swap(lson,rson),rev[x]^=1; } void pushup(int x) { if(!x) return; mintot[x]=value[x]; mintot[x]=min(mintot[x],min(getset,min(mintot[lson], mintot[rson]))); } void marktag(int x,ll delta) { if(!x) return; value[x]=tag[x]=delta; mintot[x]=min(value[x],getset); } void pushdown(int x) { if(!x) return; if(tag[x]) { marktag(lson,tag[x]); marktag(rson,tag[x]); tag[x]=0; } if(rev[x]) { markrev(lson); markrev(rson); rev[x]^=1; } } void rotate(int x) { int old=f[x],fold=f[old],which=get(x); if(!isroot(old)) ch[fold][ch[fold][1]==old]=x; ch[old][which]=ch[x][which^1],f[ch[old][which]]=old; ch[x][which^1]=old,f[old]=x,f[x]=fold; pushup(old),pushup(x); } void splay(int x) { int u=x,v=0,fa; sta[++v]=u; while(!isroot(u)) sta[++v]=f[u],u=f[u]; while(v) pushdown(sta[v--]); for(u=f[u];(fa=f[x])!=u;rotate(x)) if(f[fa]!=u) rotate(get(fa)==get(x)?fa:x); } void Access(int x) { for(int t = 0 ; x ; t = x, x = f[x]) { splay(x); if(rson) chil[x].insert(mintot[rson]); if(t) chil[x].erase(chil[x].lower_bound(mintot[t])); rson=t; pushup(x); } } void MakeRoot(int x) { Access(x),splay(x), markrev(x); } void split(int x,int y) { MakeRoot(x),Access(y),splay(y); } void link(int x,int y) { MakeRoot(x),f[x]=y, chil[y].insert(mintot[x]), pushup(y); } }; int n,Q,edges,root; int from[maxn],to[maxn<<1],hd[maxn],nex[maxn<<1]; void add(int u,int v) { nex[++edges]=hd[u],hd[u]=edges,to[edges]=v; } void dfs(int u,int ff) { for(int i=hd[u];i;i=nex[i]) { int v=to[i]; if(v==ff) continue; tree::f[v]=u; dfs(v,u); chil[u].insert(mintot[v]); } tree::pushup(u); } int main() { // setIO("input"); scanf("%d%d",&n,&Q); for(int i=1,u,v;i<n;++i) { scanf("%d%d",&u,&v); add(u,v); add(v,u); } mintot[0]=value[0]=inf; chil[0].insert(inf); for(int i=1;i<=n;++i) scanf("%lld",&value[i]),mintot[i]=value[i]; //for(int i=1;i<=edges;++i) tree::link(to[i],from[i]); scanf("%d",&root); dfs(root,0); tree::MakeRoot(root); int opt,a,b,c,x; ll xx; while(Q--) { scanf("%d",&opt); switch(opt) { case 1 : { scanf("%d",&a); tree::MakeRoot(root=a); break; } case 2 : { scanf("%d%d%lld",&a,&b,&xx); tree::split(a,b); tree::marktag(b,xx); tree::MakeRoot(root); break; } case 3 : { scanf("%d",&x); // tree::MakeRoot(root); tree::Access(x); tree::splay(x); printf("%lld\n",min(value[x],getset)); break; } } } return 0; }