#include<bits/stdc++.h>
#define LL long long
#define LLL __int128
#define uint unsigned
#define ldb long double
#define uLL unsigned long long
using namespace std;
/**----------------Fast IO----------------**/
const int N=2e5+5,INF=2e9+7;
int n,q,top,rt;
int Gm,Gh[N],Gv[N],Gnxt[N],stk[N];
int ext[N],rv[N],f[N],T[N][3];
struct tag{
int k,b;
tag():k(1),b(0){}
tag(const int&_k,const int&_b):k(_k),b(_b){}
inline void operator+=(const tag&o){(o.k)?(b+=o.b):(k=0,b=o.b);}
}GC[N],GS[N];
struct node{
int mx,mn,sm,sz;
inline void operator+=(const tag&o){
if(sz)if(o.k)mn+=o.b,mx+=o.b,sm+=o.b*sz;else sm=sz*(mn=mx=o.b);
}
inline node operator+(const node&o){
return {max(mx,o.mx),min(mn,o.mn),sm+o.sm,sz+o.sz};
}
}TC[N],TS[N],A[N];
inline void add(const int&u,const int&v){
Gv[++Gm]=v,Gnxt[Gm]=Gh[u],Gh[u]=Gm;
Gv[++Gm]=u,Gnxt[Gm]=Gh[v],Gh[v]=Gm;
}
inline bool isrt(const int&x){
return !f[x]||((x>n)^(f[x]>n));
}
inline int isr(const int&x){
return (T[f[x]][2]==x)?2:x==T[f[x]][1];
}
inline void Rev(const int&x){
swap(T[x][0],T[x][1]),rv[x]^=1;
}
inline void Tag(const int&x,const tag&tl,const tag&tc){
if(x>n)TS[x]+=tc,GS[x]+=tc;
else TC[x]+=tl,GC[x]+=tl,TS[x]+=tc,GS[x]+=tc,A[x]+=tl;
}
inline void pushdown(const int&x){
const int ls=T[x][0],rs=T[x][1],ms=T[x][2];
if(x>n){
if(ls)Tag(ls,GS[x],GS[x]);
if(rs)Tag(rs,GS[x],GS[x]);
GS[x]=tag();
}
else{
if(rv[x]){
if(ls)Rev(ls);
if(rs)Rev(rs);
rv[x]=0;
}
if(ls)Tag(ls,GC[x],GS[x]);
if(rs)Tag(rs,GC[x],GS[x]);
if(ms)Tag(ms,GS[x],GS[x]);
GC[x]=GS[x]=tag();
}
}
inline void pushup(const int&x){
const int ls=T[x][0],rs=T[x][1],ms=T[x][2];
if(x>n)TS[x]=TC[ls]+TS[ls]+TC[rs]+TS[rs];
else TC[x]=TC[ls]+A[x]+TC[rs],TS[x]=TS[ls]+TS[ms]+TS[rs];
}
inline void rotate(const int&x){
int y=f[x],c=isr(x),w=T[x][!c];
T[f[w]=T[x][!c]=y][c]=w;
if(f[y])T[f[y]][isr(y)]=x;
f[x]=f[y],f[y]=x,pushup(y);
}
inline void splay(const int&x){
for(int y=f[x];!isrt(x);rotate(x),y=f[x])
if(!isrt(y))rotate(isr(x)^isr(y)?x:y);
pushup(x);
}
inline void ins(const int&x,const int&z){
int p=T[x][2],q=ext[top--];
T[q][0]=p,T[q][1]=z,f[p]=f[z]=q;
pushup(q),f[T[x][2]=q]=x;
}
inline void del(int&x){
int y=f[x],z=f[y],c=isr(x),w=T[y][!c];
T[f[w]=z][isr(y)]=w,ext[++top]=y;
if(z>n)splay(z),z=f[z];
x=z;
}
inline void access(int x){
int z=0,y=x;
for(;stk[++z]=y;y=f[y]);
for(;--z;pushdown(stk[z]));
for(y=0;y?(f[x]?(del(x),1):0):1;y=x){
splay(x);
if(T[x][1])ins(x,T[x][1]);
f[T[x][1]=y]=x,pushup(x);
}
}
inline void makert(const int&x){
access(x),splay(x),Rev(x);
}
inline void split(const int&x,const int&y){
access(x),splay(x),Rev(x),access(y),splay(y);
}
inline void dfs(const int&u,const int&fa){
for(int i=Gh[u],v;v=Gv[i];i=Gnxt[i])
if(v!=fa)dfs(v,u),ins(u,v);
pushup(u);
}
inline void changefa(const int&x,const int&y){
if(x==y)return;
split(rt,y);
int z=1,t=f[stk[1]=x];
for(;t&&t<=n;t=f[t])stk[++z]=t;
for(;z;pushdown(stk[z--]));
splay(x);
if(!f[y]){
access(x),splay(x);
f[T[x][0]]=0,T[x][0]=0,pushup(x);
access(y),splay(y);
ins(y,x),pushup(x);
}
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
#define LOCAL
#ifndef LOCAL
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
TC[0]=TS[0]=A[0]={-INF,INF,0,0};
n=read(),q=read();
for(int i=1;i<n;++i)add(read(),read());
for(int i=1;i<=n;++i){
int x=read();
ext[++top]=i+n;
TS[i]=TC[i+n]={-INF,INF,0,0};
TC[i]=A[i]={x,x,x,1};
}
dfs(1,0),rt=read();
for(int i=1;i<=q;++i){
int op=read(),x=read();
if(op==1)rt=x;
else if(op==9)changefa(x,read());
else if(op==0||(op>2&&op<6)||op==11){
split(rt,x);
auto nd=A[x]+TS[T[x][2]];
if(op==0||op==5){
tag tg=tag(op==5,read());
A[x]+=tg,TS[T[x][2]]+=tg,GS[T[x][2]]+=tg;
pushup(x);
}
else if(op==3)write(nd.mn,'\n');
else if(op==4)write(nd.mx,'\n');
else write(nd.sm,'\n');
}
else{
int y=read();
split(x,y);
auto nd=TC[y];
if(op<7)Tag(y,tag(op==6,read()),tag());
else if(op==7)write(nd.mn,'\n');
else if(op==8)write(nd.mx,'\n');
else write(nd.sm,'\n');
}
}
return flush(),0;
}剪贴板
剪贴板 0w70ii93
剪贴板信息
剪贴板信息
- 收录
- 更新