跳到正文

剪贴板

剪贴板 eq274hy0

原文
剪贴板信息

剪贴板信息

收录
更新
C++
//ayame保佑,夸哥保佑,狗妈保佑,MDR保佑,锉刀怪保佑,M99保佑,克爹保佑
#include<bits/stdc++.h>
using namespace std;
int p1=1000000,p2=0;
char buf[1000005],wb[1000005];
int gc() {
	if(p1>=1000000)fread(buf,1,1000000,stdin),p1=0;
	return buf[p1++];
}
#define gc getchar
#define Loli true
#define Kon xor true
long long getint() {
	long long ret=0,flag=1;
	char c=gc();
	while(c<'0'||c>'9') {
		if(c=='-')flag=-1;
		c=gc();
	}
	while(c<='9'&&c>='0') {
		ret=(ret<<3)+(ret<<1)+c-'0';
		c=gc();
	}
	return ret*flag;
}
void pc(char x) {
	if(p2>=1000000)fwrite(wb,1,1000000,stdout),p2=0;
	wb[p2++]=x;
}
void wrt(long long x) {
	if(x<0)pc('-'),x=-x;
	int c[24]= {0};
	if(!x)return pc('0'),void();
	while(x)c[++c[0]]=x%10,x/=10;
	while(c[0])pc(c[c[0]--]+'0');
}
const int inf = 0x3f3f3f3f ;
struct data {
	int sum,mi,mx,sz;
	data() {sum=sz=0,mi=inf,mx=-inf;}
	data(int x) {sum=mi=mx=x,sz=1;}
	data(int a,int b,int c,int d) {sum=a,mi=b,mx=c,sz=d;}
	data operator +(data x) {return data(sum+x.sum,min(mi,x.mi),max(mx,x.mx),sz+x.sz);}
};
struct tag {
	int k,b;
	tag(int _k=1,int _b=0) {k=_k,b=_b;}
	bool unit() {return k!=1||b;}
	tag operator +(tag x) {return tag(k*x.k,b*x.k+x.b);}
	int f(int x) {return k*x+b;}
};
data operator +(data a,tag b) {return a.sz?data(a.sum*b.k+a.sz*b.b,b.f(a.mi),b.f(a.mx),a.sz):a;}
int n,m,root;
namespace SITT {
	int tot,S[3000005];
	struct node {
		int ch[2],fa,root,val;
		int pre,suf;
		bool rev,in;
		data csum,tsum,sum;
		tag ctag,ttag;
		node(int _in=0) {
			ch[0]=ch[1]=0,fa=root=val=0,pre=suf=0;
			rev=0,in=_in,sum=tsum=sum=data();
			ctag=ttag=tag();
		}
	} v[3000005];
	bool isroot(int x) {
		return x!=v[v[x].fa].ch[0]&&x!=v[v[x].fa].ch[1];
	}
	void tag_rev(int x) {
		if(!x)return;
		swap(v[x].ch[0],v[x].ch[1]);
		swap(v[x].pre,v[x].suf);
		v[x].rev^=1;
	}
	void tag_chain(int x,tag p) {
		if(!x)return;
		v[x].csum=v[x].csum+p;
		v[x].sum=v[x].csum+v[x].tsum;
		v[x].val=p.f(v[x].val);
		v[x].ctag=v[x].ctag+p;
	}
	void tag_tree(int x,tag p,bool chain) {
		if(!x)return;
		v[x].tsum=v[x].tsum+p;
		v[x].ttag=v[x].ttag+p;
		if(!v[x].in&&chain)tag_chain(x,p);
		else v[x].sum=v[x].csum+v[x].tsum;
	}
	void push_down(int x) {
		if(!x)return;
		if(v[x].rev)tag_rev(v[x].ch[0]),tag_rev(v[x].ch[1]),v[x].rev^=1;
		if(!v[x].in&&v[x].ctag.unit())tag_chain(v[x].ch[0],v[x].ctag),tag_chain(v[x].ch[1],v[x].ctag),v[x].ctag=tag();
		if(v[x].ttag.unit()) {
			tag_tree(v[x].ch[0],v[x].ttag,0),tag_tree(v[x].ch[1],v[x].ttag,0);
			tag_tree(v[x].root,v[x].ttag,1),v[x].ttag=tag();
		}
	}
	void push_up(int x) {
		if(v[x].in) {
			v[x].tsum=data();
			if(v[x].ch[0])v[x].tsum=v[x].tsum+v[v[x].ch[0]].tsum;
			if(v[x].ch[1])v[x].tsum=v[x].tsum+v[v[x].ch[1]].tsum;
			v[x].sum=v[x].tsum;
		} else {
			if(v[x].root)v[x].tsum=v[v[x].root].sum;
			else v[x].tsum=data();
			v[x].pre=v[x].suf=x;
			v[x].csum=data(v[x].val);
			if(v[x].ch[0])v[x].csum=v[x].csum+v[v[x].ch[0]].csum,v[x].tsum=v[x].tsum+v[v[x].ch[0]].tsum,v[x].pre=v[v[x].ch[0]].pre;
			if(v[x].ch[1])v[x].csum=v[x].csum+v[v[x].ch[1]].csum,v[x].tsum=v[x].tsum+v[v[x].ch[1]].tsum,v[x].suf=v[v[x].ch[1]].suf;
			v[x].sum=v[x].csum+v[x].tsum;
		}
	}
	void rot(int x) {
		int p=v[x].fa,g=v[p].fa;
		bool d=(x==v[p].ch[1]);
		if(!isroot(p))v[g].ch[p==v[g].ch[1]]=x;
		v[p].ch[d]=v[x].ch[d^1];
		v[v[x].ch[d^1]].fa=p;
		v[x].ch[d^1]=p;
		v[p].fa=x;
		v[x].fa=g;
		push_up(p);
		push_up(x);
	}
	void pre_push_down(int x) {
		if(!isroot(x))pre_push_down(v[x].fa);
		push_down(x);
	}
	void splay(int x,int f=0) {
		pre_push_down(x);
		while(!isroot(x)&&v[x].fa!=f) {
			int p=v[x].fa,g=v[p].fa;
			if(!isroot(p))rot((v[g].ch[0]==p)^(v[p].ch[0]==x)?x:p);
			rot(x);
		}
		push_up(x);
	}
	int new_node() {
		int x=S[0]?S[S[0]--]:++tot;
		v[x]=node(1);
		return x;
	}
	void ins_son(int &x,int y,data d,int L=1,int R=n) {
		if(!y)return;
		if(!x)x=new_node();
		if(L==R)return v[x].root=y,v[x].tsum=v[x].sum=d,void();
		int mid=(L+R)>>1;
		push_down(x);
		if(y<=mid)ins_son(v[x].ch[0],y,d,L,mid);
		if(mid<y)ins_son(v[x].ch[1],y,d,mid+1,R);
		push_up(x);
	}
	void del_son(int &x,int y,int p,int L=1,int R=n) {
		if(!y)return;
		if(L==R)return tag_tree(p,v[x].ttag,1),v[x]=node(1),x=0,void();
		int mid=(L+R)>>1;
		push_down(x);
		if(y<=mid)del_son(v[x].ch[0],y,p,L,mid);
		if(mid<y)del_son(v[x].ch[1],y,p,mid+1,R);
		push_up(x);
		if(!v[x].ch[0]&&!v[x].ch[1])S[++S[0]]=x,v[x]=node(1),x=0;
	}
	int access(int x) {
		int ret=0;
		for(int y=0; x; y=x,x=v[x].fa) {
			splay(x);
			if(y)del_son(v[x].root,v[y].pre,y);
			if(v[x].ch[1])ins_son(v[x].root,v[v[x].ch[1]].pre,v[v[x].ch[1]].sum);
			v[x].ch[1]=y,push_up(x),ret=x;
		}
		return ret;
	}
	int LCA(int x,int y) {
		access(x);
		return access(y);
	}
	void makeroot(int x) {
		access(x);
		splay(x);
		tag_rev(x);
	}
	void link(int x,int y) {
		makeroot(x),makeroot(y);
		v[x].fa=y,ins_son(v[y].root,v[x].pre,v[x].sum);
		push_up(y);
	}
	void cut(int x) {
		access(x);
		splay(x);
		v[v[x].ch[0]].fa=0;
		v[x].ch[0]=0;
		push_up(x);
	}
	void modify_chain(int x,int y,tag p) {
		makeroot(x);
		access(y);
		splay(y);
		tag_chain(y,p);
	}
	data ask_chain(int x,int y) {
		makeroot(x);
		access(y);
		splay(y);
		return v[y].csum;
	}
	void modify_tree(int x,tag p) {
		access(x),splay(x);
		v[x].val=p.f(v[x].val);
		if(v[x].root)tag_tree(v[x].root,p,1);
		push_up(x);
	}
	data ask_tree(int x) {
		access(x),splay(x);
		return (v[x].root?v[v[x].root].sum:data())+data(v[x].val);
	}
}
int u[100005],v[100005];
int main() {
	n=getint();
	m=getint();
	SITT::tot=n;
	for(int i=1; i<n; i++)u[i]=getint(),v[i]=getint();
	for(int i=1; i<=n; i++)SITT::v[i].val=getint(),SITT::push_up(i);
	for(int i=1; i<n; i++)SITT::link(u[i],v[i]);
	root=getint();
	SITT::makeroot(root);
	for(int i=1; i<=m; i++) {
		int opt=getint();
		tag tmp=tag();
		if(opt==0) {
			int x=getint(),y=getint();
			tmp.k=0,tmp.b=y;
			SITT::modify_tree(x,tmp);
		}
		if(opt==1)root=getint();
		if(opt==2) {
			int x=getint(),y=getint(),z=getint();
			tmp.k=0,tmp.b=z;
			SITT::modify_chain(x,y,tmp);
		}
		if(opt==3) {
			int x=getint();
			wrt(SITT::ask_tree(x).mi),pc('\n');
		}
		if(opt==4) {
			int x=getint();
			wrt(SITT::ask_tree(x).mx),pc('\n');
		}
		if(opt==5) {
			int x=getint(),y=getint();
			tmp.k=1,tmp.b=y;
			SITT::modify_tree(x,tmp);
		}
		if(opt==6) {
			int x=getint(),y=getint(),z=getint();
			tmp.k=1,tmp.b=z;
			SITT::modify_chain(x,y,tmp);
		}
		if(opt==7) {
			int x=getint(),y=getint();
			wrt(SITT::ask_chain(x,y).mi),pc('\n');
		}
		if(opt==8) {
			int x=getint(),y=getint();
			wrt(SITT::ask_chain(x,y).mx),pc('\n');
		}
		if(opt==9) {
			int x=getint(),y=getint();
			if(SITT::LCA(x,y)==x)continue;
			SITT::cut(x),SITT::link(y,x);
		}
		if(opt==10) {
			int x=getint(),y=getint();
			wrt(SITT::ask_chain(x,y).sum),pc('\n');
		}
		if(opt==11) {
			int x=getint();
			wrt(SITT::ask_tree(x).sum),pc('\n');
		}
		SITT::makeroot(root);
	}
	fwrite(wb,1,p2,stdout);
	return Loli Kon;
}
C++
//from @linfourxu
#include<bits/stdc++.h>
using namespace std;
namespace lin4xu{

#define re register
#define rint re int
#define ll long long
#define rll re ll
#define db double
#define rdb re db
#define rch re char
#define ldb long db
#define rldb re ldb
#define un unsigned
#define ull un ll
#define rull re ull

template <typename T> T read()
{
	re T ans=0;rint f=0;rch ch=getchar();
	for(;!isdigit(ch);ch=getchar()) f=ch=='-';
	for(;isdigit(ch);ch=getchar()) ans=ans*10+(ch&15);
	return f?-ans:ans;
}

const int maxn=1e5+10;
const int inf=2147383647;

#define lx t[x].son[0]
#define rx t[x].son[1]

struct Tag
{
	int k,b;
	inline friend bool operator ! (const Tag &x){return x.k!=1||x.b;}
	inline friend Tag operator += (Tag &x,const Tag &y){return x=(Tag){x.k*y.k,x.b*y.k+y.b};}
	inline friend int operator * (const rint &x,const Tag &y){return x*y.k+y.b;}
	inline friend int operator *= (rint &x,const Tag y){return x=x*y;}
	inline void clear(){k=1,b=0;}
};
struct Req
{
	int sum,mx,mn,sz;
	inline friend Req operator + (const Req &x,const Req &y){return (Req){x.sum+y.sum,max(x.mx,y.mx),min(x.mn,y.mn),x.sz+y.sz};}
	inline friend Req operator += (Req &x,const Req &y){return x=x+y;}
	inline friend Req operator * (Req &x,const Tag &y){return (Req){x.sum*y.k+x.sz*y.b,x.mx*y,x.mn*y,x.sz};}
	inline friend Req operator *= (Req &x,const Tag &y){return x=x*y;}
};
struct Node
{
	int son[2],fa,val,rt;
	Tag tag0,tag1;
	Req ans,ans0,ans1;
	bool rev;
	Node(){tag0.clear(),tag1.clear();}
}t[maxn<<1];
struct edge
{
	int to,next;
}e[maxn<<1];
int head[maxn];
int n,cnt;

inline void add(rint x,rint y)
{
	e[++cnt].to=y;
	e[cnt].next=head[x];
	head[x]=cnt;
}

inline void add_edge(rint x,rint y)
{
	add(x,y),add(y,x);
}

inline int not_rt(rint x)
{
	return t[t[x].fa].son[0]==x||t[t[x].fa].son[1]==x;
}

inline int identify(rint x)
{
	return t[t[x].fa].son[1]==x;
}

inline void connect(rint x,rint fa,rint k)
{
	if(fa) t[fa].son[k]=x;
	if(x) t[x].fa=fa;
}

inline void push_up(rint x)
{
	if(x<=n) t[x].ans0=(Req){t[x].val,t[x].val,t[x].val,1}+t[lx].ans0+t[rx].ans0,t[x].ans1=t[lx].ans1+t[rx].ans1+t[t[x].rt].ans1,t[x].ans=t[x].ans0+t[x].ans1;
	else t[x].ans1=t[x-n].ans+t[lx].ans1+t[rx].ans1;
}

inline void push_tag0(rint x,Tag tag)
{
	if(!x) return;
	t[x].val*=tag,t[x].tag0+=tag,t[x].ans0*=tag,t[x].ans=t[x].ans0+t[x].ans1;
}

inline void push_tag1(rint x,Tag tag)
{
	if(!x) return;
	t[x].val*=tag,t[x].ans0*=tag,t[x].ans*=tag,t[x].tag1+=tag;
	if(!t[x].tag0.k||!tag.k) t[x].tag0+=tag;
	if(t[x].ans1.sz) t[x].ans1*=tag;
}

inline void push_tag1_(rint x,Tag tag)
{
	if(!x) return;
	t[x].tag1+=tag,t[x].ans1*=tag,push_tag1(x-n,tag);
}

inline void push_rev(rint x)
{
	if(!x) return;
	t[x].rev^=1,swap(lx,rx);
}

inline void push_down(rint x)
{
	if(t[x].rev) push_rev(lx),push_rev(rx),t[x].rev=0;
	if(!t[x].tag1)
	{
		if(x<=n) push_tag1_(t[x].rt,t[x].tag1),push_tag1(lx,t[x].tag1),push_tag1(rx,t[x].tag1);
		else push_tag1_(lx,t[x].tag1),push_tag1_(rx,t[x].tag1);
		t[x].tag1.clear();
	}
	if(!t[x].tag0) push_tag0(lx,t[x].tag0),push_tag0(rx,t[x].tag0),t[x].tag0.clear();
}

inline void rotate(rint x)
{
	rint fx=t[x].fa,kx=identify(x);t[x].fa=t[fx].fa;
	if(not_rt(fx)) t[t[x].fa].son[identify(fx)]=x;
	connect(t[x].son[kx^1],fx,kx),connect(fx,x,kx^1);
	push_up(fx),push_up(x);
}

inline int modify(rint x)
{
	rint rt=x;
	if(not_rt(x)) rt=modify(t[x].fa);
	return push_down(x),rt;
}

inline int splay(rint x)
{
	rint rt=modify(x);
	while(not_rt(x))
	{
		rint fx=t[x].fa;
		if(!not_rt(fx)) rotate(x);
		else if(identify(x)==identify(fx)) rotate(fx),rotate(x);
		else rotate(x),rotate(x);
	}
	return rt;
}

inline void splay(rint x,rint goal)
{
	modify(x);
	while(t[x].fa!=goal)
	{
		rint fx=t[x].fa;
		if(t[fx].fa==goal) rotate(x);
		else if(identify(x)==identify(fx)) rotate(fx),rotate(x);
		else rotate(x),rotate(x);
	}
}

inline void erase(rint &rt,rint x)
{
	splay(x);rint pre=lx,nxt=rx;
	while(t[pre].son[1]) pre=t[pre].son[1];
	while(t[nxt].son[0]) nxt=t[nxt].son[0];
	if(pre&&nxt) splay(pre),splay(nxt,pre),t[nxt].son[0]=t[x].fa=0,push_up(nxt),push_up(pre),rt=pre;
	else if(pre) splay(pre),t[pre].son[1]=t[x].fa=0,push_up(pre),rt=pre;
	else if(nxt) splay(nxt),t[nxt].son[0]=t[x].fa=0,push_up(nxt),rt=nxt;
	else rt=0;
}

inline void insert(rint &rt,rint x)
{
	push_up(x);
	if(!rt) return rt=x,void();
	rint u=rt;
	while(t[u].son[1]) u=t[u].son[1];
	splay(u),rt=t[x].fa=u,t[u].son[1]=x,push_up(u);
}

inline void Modify(rint x)
{
	if(t[x].fa)
	{
		Modify(t[x].fa);
		if(!not_rt(x)) splay(x+n);
	}
	push_down(x);
}

inline void access(rint x)
{
	Modify(x);
	for(rint tmp=0;x;x=t[tmp=x].fa)
	{
		rint rt=splay(x);
		if(t[x].fa) erase(t[t[x].fa].rt,rt+n);
		if(rx) insert(t[x].rt,rx+n);
		rx=tmp,push_up(x);
	}
}

inline void change_rt(rint x)
{
	access(x),splay(x),push_rev(x);
}

inline void cut(rint x,rint y)
{
	access(x),splay(x);rint pre=lx;push_down(pre);
	while(t[pre].son[1]) pre=t[pre].son[1],push_down(pre);
	splay(pre),t[pre].son[1]=0,push_up(pre);
	access(y),splay(y),t[x].fa=y,insert(t[y].rt,x+n),push_up(y);
}

inline void link(rint x,rint y)
{
	splay(x),access(y),splay(y),t[x].fa=y,insert(t[y].rt,x+n),push_up(y);
}

inline void dfs(rint x,rint fa)
{
	for(rint i=head[x];i;i=e[i].next)
	{
		rint v=e[i].to;
		if(v==fa) continue;
		dfs(v,x),link(v,x);
	}
}

int main()
{
	n=read<int>();rint q=read<int>();t[0].ans0=t[0].ans1=(Req){0,-inf,inf,0};
	for(rint i=1;i<n;i++) add_edge(read<int>(),read<int>());
	for(rint i=1;i<=n;i++) t[i].val=read<int>(),push_up(i),push_up(i+n);
	rint rt=read<int>();dfs(rt,-1);
	for(rint tt=1;tt<=q;tt++)
	{
		rint opt=read<int>(),x=read<int>();
		if(opt==0)
		{
			rint y=read<int>();
			access(x),splay(x),t[x].val=y,push_tag1_(t[x].rt,(Tag){0,y}),push_up(x);
		}
		if(opt==1) change_rt(rt=x);
		if(opt==2)
		{
			rint y=read<int>(),z=read<int>();
			change_rt(x),access(y),splay(y),push_tag0(y,(Tag){0,z}),change_rt(rt);
		}
		if(opt==3) access(x),splay(x),printf("%d\n",min(t[x].val,t[t[x].rt].ans1.mn));
		if(opt==4) access(x),splay(x),printf("%d\n",max(t[x].val,t[t[x].rt].ans1.mx));
		if(opt==5)
		{
			rint y=read<int>();
			access(x),splay(x),t[x].val+=y,push_tag1_(t[x].rt,(Tag){1,y}),push_up(x);
		}
		if(opt==6)
		{
			rint y=read<int>(),z=read<int>();
			change_rt(x),access(y),splay(y),push_tag0(y,(Tag){1,z}),change_rt(rt);
		}
		if(opt==7)
		{
			rint y=read<int>();
			change_rt(x),access(y),splay(y),printf("%d\n",t[y].ans0.mn),change_rt(rt);
		}
		if(opt==8)
		{
			rint y=read<int>();
			change_rt(x),access(y),splay(y),printf("%d\n",t[y].ans0.mx),change_rt(rt);
		}
		if(opt==9)
		{
			rint y=read<int>(),tmp=x;
			if(x==y) continue;
			access(y),splay(y);
			while(not_rt(tmp)) tmp=t[tmp].fa;
			if(tmp!=y) cut(x,y);
		}
		if(opt==10)
		{
			rint y=read<int>();
			change_rt(x),access(y),splay(y),printf("%d\n",t[y].ans0.sum),change_rt(rt);
		}
		if(opt==11) access(x),splay(x),printf("%d\n",t[x].val+t[t[x].rt].ans1.sum);
	}
	return 0;
}};

int main(){return lin4xu::main();}
C++
//ayame保佑,夸哥保佑,狗妈保佑,MDR保佑,锉刀怪保佑,M99保佑,克爹保佑
#include<bits/stdc++.h>
using namespace std;
int p1=1000000,p2=0;
char buf[1000005],wb[1000005];
int gc() {
	if(p1>=1000000)fread(buf,1,1000000,stdin),p1=0;
	return buf[p1++];
}
#define gc getchar
long long getint() {
	long long ret=0,flag=1;
	char c=gc();
	while(c<'0'||c>'9') {
		if(c=='-')flag=-1;
		c=gc();
	}
	while(c<='9'&&c>='0') {
		ret=(ret<<3)+(ret<<1)+c-'0';
		c=gc();
	}
	return ret*flag;
}
void pc(char x) {
	if(p2>=1000000)fwrite(wb,1,1000000,stdout),p2=0;
	wb[p2++]=x;
}
void wrt(long long x) {
	if(x<0)pc('-'),x=-x;
	int c[24]= {0};
	if(!x)return pc('0'),void();
	while(x)c[++c[0]]=x%10,x/=10;
	while(c[0])pc(c[c[0]--]+'0');
}
#define inf 0x3f3f3f3f
int n,m;
struct data {
	int sum,mi,mx,sz;
	data() {sum=sz=0,mi=inf,mx=-inf;}
	data(int x) {sum=mi=mx=x,sz=1;}
	data(int a,int b,int c,int d){sum=a,mi=b,mx=c,sz=d;}
	data operator+(data x) {return data(sum+x.sum,min(mi,x.mi),max(mx,x.mx),sz+x.sz);}
};
struct tag {
	int k,b;
	tag(int _k=1,int _b=0) {k=_k,b=_b;}
	bool unit() {return k!=1||b;}
	tag operator+(tag x) {return tag(k*x.k,b*x.k+x.b);}
	int f(int x) {return k*x+b;}
};
data operator+(data a,tag b) {return a.sz?data(a.sum*b.k+a.sz*b.b,b.f(a.mi),b.f(a.mx),a.sz):a;}
namespace SATT {
	//compress tree: 0
	//rake tree: 1
	int root,tot,S[500005];
	struct node {
		bool rev,bj;
		int ch[3],fa;
		data csum,tsum;
		tag ctag,ttag;
		int val;
		node() {
			for(int i=0; i<3; i++)ch[i]=0;
			rev=fa=0,val=0,csum=tsum=data(),ctag=ttag=tag();
		}
	} v[500005];
	void init(int x) {
		for(int i=1; i<=x; i++)v[i]=node();
	}
	bool isroot(int x) {
		return v[v[x].fa].ch[0]!=x&&v[v[x].fa].ch[1]!=x;
	}
	void add_son(int x,int t,int y) {
		v[x].ch[t]=y,v[y].fa=x;
	}
	void tag_rev(int x) {
		if(!x)return;
		swap(v[x].ch[0],v[x].ch[1]);
		v[x].rev^=1;
	}
	void tag_chain(int x,tag p) {
		if(!x)return;
		v[x].csum=v[x].csum+p;
		v[x].val=p.f(v[x].val);
		v[x].ctag=v[x].ctag+p;
	}
	void tag_tree(int x,tag p) {
		if(!x)return;
		v[x].tsum=v[x].tsum+p;
		v[x].ttag=v[x].ttag+p;
	}
	void push_up(int x,bool bj) {
		if(bj==0) {
			v[x].csum=data(v[x].val),v[x].csum=v[x].csum+v[v[x].ch[0]].csum+v[v[x].ch[1]].csum;
			v[x].tsum=v[v[x].ch[0]].tsum+v[v[x].ch[1]].tsum+v[v[x].ch[2]].tsum;
		}
		if(bj==1) {
			v[x].tsum=v[v[x].ch[0]].tsum+v[v[x].ch[1]].tsum+v[v[x].ch[2]].csum+v[v[x].ch[2]].tsum;
		}
	}
	void push_down(int x,bool bj) {
		if(bj==0) {
			if(v[x].rev)tag_rev(v[x].ch[0]),tag_rev(v[x].ch[1]),v[x].rev=0;
			if(v[x].ctag.unit()) {
				tag_chain(v[x].ch[0],v[x].ctag);
				tag_chain(v[x].ch[1],v[x].ctag);
				v[x].ctag=tag();
			}
			if(v[x].ttag.unit()) {
				tag_tree(v[x].ch[0],v[x].ttag);
				tag_tree(v[x].ch[1],v[x].ttag);
				tag_tree(v[x].ch[2],v[x].ttag);
				v[x].ttag=tag();
			}
		}
		if(bj==1) {
			if(v[x].ttag.unit()) {
				tag_tree(v[x].ch[0],v[x].ttag);
				tag_tree(v[x].ch[1],v[x].ttag);
				tag_tree(v[x].ch[2],v[x].ttag);
				tag_chain(v[x].ch[2],v[x].ttag);
				v[x].ttag=tag();
			}
		}
	}
	void rot(int x,bool bj) {
		int p=v[x].fa,g=v[p].fa,d=(v[p].ch[1]==x);
		if(g)v[g].ch[v[g].ch[2]==p?2:(v[g].ch[1]==p)]=x;
		v[x].fa=g;
		add_son(p,d,v[x].ch[!d]);
		add_son(x,!d,p);
		push_up(p,bj);
		push_up(x,bj);
	}
	void pre_push_down(int x,bool bj,int goal) {
		if(!isroot(x)&&v[x].fa!=goal)pre_push_down(v[x].fa,bj,goal);
		push_down(x,bj);
	}
	void splay(int x,bool bj,int goal=0) {
		pre_push_down(x,bj,goal);
		while(!isroot(x)&&v[x].fa!=goal) {
			int p=v[x].fa,g=v[p].fa;
			if(!isroot(p)&&g!=goal)rot(p==v[g].ch[0]^x==v[p].ch[0]?x:p,bj);
			rot(x,bj);
		}
	}
	void local_splay(int x,int d,bool bj) {
		int goal=v[x].fa;
		while(v[x].ch[d])push_down(x,bj),x=v[x].ch[d];
		splay(x,bj,goal);
	}
	void del(int x) {
		S[++S[0]]=x;
		v[x]=node();
	}
	int new_node() {
		int x=S[0]?S[S[0]--]:++tot;
		return x;
	}
	void splice(int x) {
		splay(x,1),x=v[x].fa,splay(x,0);
		int y=v[x].ch[2];
		push_down(y,1);
		if(v[x].ch[1]) {
			swap(v[v[x].ch[1]].fa,v[v[y].ch[2]].fa);
			swap(v[x].ch[1],v[y].ch[2]);
		} else {
			add_son(x,1,v[y].ch[2]);
			if(v[y].ch[0]) {
				local_splay(v[y].ch[0],1,1);
				add_son(v[y].ch[0],1,v[y].ch[1]);
				v[x].ch[2]=v[y].ch[0];
			} else {
				push_down(v[y].ch[1],1);
				v[x].ch[2]=v[y].ch[1];
			}
			del(y);
			y=v[x].ch[2];
			v[y].fa=x;
		}
		push_up(y,1);
		push_up(x,0);
		push_down(v[x].ch[1],0);
		rot(v[x].ch[1],0);
	}
	void access(int x) {
		splay(x,0);
		if(v[x].ch[1]) {
			int y=new_node();
			add_son(y,0,v[x].ch[2]);
			add_son(y,2,v[x].ch[1]);
			push_up(y,1);
			v[x].ch[1]=0;
			add_son(x,2,y);
			push_up(x,0);
		}
		while(v[x].fa)splice(v[x].fa);
	}
	void makeroot(int x) {
		access(x);
		tag_rev(x);
	}
	void expose(int x,int y) {
		makeroot(x);
		access(y);
	}
	int findroot(int x) {
		access(x);
		while(v[x].ch[0])x=v[x].ch[0];
		splay(x,0);
		return x;
	}
	void link(int x,int y) {
		access(x);
		makeroot(y);
		add_son(x,1,y);
		push_up(x,0);
	}
	int cut(int x) {
		int ret=0;
		access(x);
		push_down(x,0);
		ret=v[x].ch[0];
		v[x].ch[0]=v[v[x].ch[0]].fa=0;
		push_up(x,0);
		return ret;
	}
	void modify(int x,int val) {
		makeroot(root);
		access(x);
		v[x].val=val;
		push_up(x,0);
		tag_tree(v[x].ch[2],tag(0,val));
	}
	void modify(int x,int y,int val) {
		expose(x,y);
		tag_chain(y,tag(0,val));
	}
	void add(int x,int val) {
		makeroot(root);
		access(x);
		v[x].val+=val;
		push_up(x,0);
		tag_tree(v[x].ch[2],tag(1,val));
	}
	void add(int x,int y,int val) {
		expose(x,y);
		tag_chain(y,tag(1,val));
	}
	data ask_sum(int x) {
		makeroot(root);
		access(x);
		return (v[v[x].ch[2]].tsum+data(v[x].val));
	}
	data ask_sum(int x,int y) {
		expose(x,y);
		return v[y].csum;
	}
}
bool check(int x,int y) {
	SATT::makeroot(SATT::root),SATT::access(y);
	SATT::splay(x,0);
	while(SATT::v[y].fa)y=SATT::v[y].fa;
	return x==y;
}
int u[100005],v[100005];
int main() {
	SATT::tot=n=getint();
	m=getint();
	SATT::init(n);
	int tt=0;
	for(int i=1; i<n; i++)u[i]=getint(),v[i]=getint();
	for(int i=1; i<=n; i++)SATT::v[i].val=getint(),SATT::push_up(i,0);
	for(int i=1; i<n; i++)SATT::link(u[i],v[i]);
	SATT::root=getint(),SATT::makeroot(SATT::root);
	for(int i=1; i<=m; i++) {
		int opt=getint();
		if(opt==0) {
			int x=getint(),val=getint();
			SATT::modify(x,val);
		}
		if(opt==1)SATT::root=getint();
		if(opt==2) {
			int x=getint(),y=getint(),val=getint();
			SATT::modify(x,y,val);
		}
		if(opt==3) {
			int x=getint();
			wrt(SATT::ask_sum(x).mi),pc('\n');
		}
		if(opt==4) {
			int x=getint();
			wrt(SATT::ask_sum(x).mx),pc('\n');
		}
		if(opt==5) {
			int x=getint(),val=getint();
			SATT::add(x,val);
		}
		if(opt==6) {
			int x=getint(),y=getint(),val=getint();
			SATT::add(x,y,val);
		}
		if(opt==7) {
			int x=getint(),y=getint();
			wrt(SATT::ask_sum(x,y).mi),pc('\n');
		}
		if(opt==8) {
			int x=getint(),y=getint();
			wrt(SATT::ask_sum(x,y).mx),pc('\n');
		}
		if(opt==9) {
			int x=getint(),y=getint();
			if(check(x,y))continue;
			SATT::cut(x),SATT::link(x,y);
		}
		if(opt==10) {
			int x=getint(),y=getint();
			wrt(SATT::ask_sum(x,y).sum),pc('\n');
		}
		if(opt==11) {
			int x=getint();
			wrt(SATT::ask_sum(x).sum),pc('\n');
		}
	}
	fwrite(wb,1,p2,stdout);
	return 0;
}
C++
//ayame保佑,夸哥保佑,狗妈保佑,MDR保佑,锉刀怪保佑,M99保佑,克爹保佑
#include<bits/stdc++.h>
using namespace std;
int p1=1000000,p2=0;
char buf[1000005],wb[1000005];
int gc() {
	if(p1>=1000000)fread(buf,1,1000000,stdin),p1=0;
	return buf[p1++];
}
//#define gc getchar
#define Loli true
#define Kon xor true
long long getint() {
	long long ret=0,flag=1;
	char c=gc();
	while(c<'0'||c>'9') {
		if(c=='-')flag=-1;
		c=gc();
	}
	while(c<='9'&&c>='0') {
		ret=(ret<<3)+(ret<<1)+c-'0';
		c=gc();
	}
	return ret*flag;
}
void pc(char x) {
	if(p2>=1000000)fwrite(wb,1,1000000,stdout),p2=0;
	wb[p2++]=x;
}
void wrt(long long x) {
	if(x<0)pc('-'),x=-x;
	int c[24]= {0};
	if(!x)return pc('0'),void();
	while(x)c[++c[0]]=x%10,x/=10;
	while(c[0])pc(c[c[0]--]+'0');
}
int n,m;
int a[100005],et[200005],rep[100005],sz[100005],ud[100005];
vector<int> vec[100005];
const int inf = 2e9 ;
struct data {
	int sum,mi,mx,sz;
	data() {
		sum=sz=0,mi=inf,mx=-inf;
	}
	data(int x,int s) {
		s==1?(sum=mi=mx=x,sz=1):(sum=0,mi=inf,mx=-inf,sz=0);
	}
	data(int a,int b,int c,int d) {
		sum=a,mi=b,mx=c,sz=d;
	}
	data operator+(data x) {
		return data(sum+x.sum,min(mi,x.mi),max(mx,x.mx),sz+x.sz);
	}
};
struct tag {
	int k,b;
	tag(int _k=1,int _b=0) {
		k=_k,b=_b;
	}
	bool unit() {
		return k!=1||b;
	}
	tag operator+(tag x) {
		return tag(k*x.k,b*x.k+x.b);
	}
	int calc(int x) {
		return k*x+b;
	}
};
data operator+(data a,tag b) {
	return a.sz?data(a.sum*b.k+a.sz*b.b,b.calc(a.mi),b.calc(a.mx),a.sz):a;
}
namespace ETT {
	int tot,root;
	struct node {
		int ch[2],fa,rev,id;
		int rep,sz,num,val;
		tag stag;
		data sum;
		int pre,suf;
		int d,dtag;
	} v[200005];
	bool isroot(const int x) {
		return v[v[x].fa].ch[0]!=x&&v[v[x].fa].ch[1]!=x;
	}
	void push_tag(const int x) {
		if(!x)return;
		v[x].d^=1,v[x].dtag^=1;
	}
	void push_sum(const int x,tag val) {
		if(!x)return;
		v[x].stag=(v[x].stag+val),v[x].sum=(v[x].sum+val);
		if(v[x].rep)v[x].val=val.calc(v[x].val);
	}
	void push_rev(const int x) {
		if(!x)return;
		swap(v[x].ch[0],v[x].ch[1]);
		swap(v[x].pre,v[x].suf);
		v[x].rev^=1;
	}
	void push_up(const int x) {
		v[x].pre=v[x].suf=x,v[x].num=v[x].id?1:0;
		v[x].sz=v[x].rep,v[x].sum=data(v[x].val,v[x].rep);
		if(v[x].ch[0])v[x].pre=v[v[x].ch[0]].pre,v[x].sum=(v[x].sum+v[v[x].ch[0]].sum),v[x].sz+=v[v[x].ch[0]].sz,v[x].num+=v[v[x].ch[0]].num;
		if(v[x].ch[1])v[x].suf=v[v[x].ch[1]].suf,v[x].sum=(v[x].sum+v[v[x].ch[1]].sum),v[x].sz+=v[v[x].ch[1]].sz,v[x].num+=v[v[x].ch[1]].num;
	}
	void push_down(const int x) {
		if(v[x].rev) {
			push_rev(v[x].ch[0]);
			push_rev(v[x].ch[1]);
			v[x].rev=0;
		}
		if(v[x].dtag) {
			push_tag(v[x].ch[0]);
			push_tag(v[x].ch[1]);
			v[x].dtag=0;
		}
		if(v[x].stag.unit()) {
			push_sum(v[x].ch[0],v[x].stag);
			push_sum(v[x].ch[1],v[x].stag);
			v[x].stag=tag();
		}
	}
	int build(const int l,const int r,const int prt,int *A) {
		int x=++tot,mid=(l+r)>>1;
		if(!ud[A[mid]]&&rep[A[mid]]==mid)rep[A[mid]]=x,ud[A[mid]]=1,v[x].rep=1;
		v[x].id=A[mid],v[x].val=v[x].rep?a[A[mid]]:0;
		if(l<mid)v[x].ch[0]=build(l,mid-1,x,A);
		else if(l==1)v[x].ch[0]=++tot,push_up(tot),v[tot].fa=x;
		if(mid<r)v[x].ch[1]=build(mid+1,r,x,A);
		else if(r==et[0])v[x].ch[1]=++tot,push_up(tot),v[tot].fa=x;
		push_up(x),v[x].fa=prt;
		return root=x;
	}
	void rot(const int x) {
		const int p=v[x].fa,g=v[p].fa;
		bool d=v[p].ch[1]==x;
		if(!isroot(p))v[g].ch[v[g].ch[1]==p]=x;
		v[p].ch[d]=v[x].ch[d^1];
		v[v[x].ch[d^1]].fa=p;
		v[x].ch[d^1]=p;
		v[x].fa=g;
		v[p].fa=x;
		push_up(p);
		push_up(x);
	}
	void pre_push_down(const int x) {
		if(!isroot(x))pre_push_down(v[x].fa);
		push_down(x);
	}
	int get_num(const int x,const int k) {
		if(!x||!k)return 0;
		push_down(x);
		if(v[v[x].ch[0]].num>=k)return get_num(v[x].ch[0],k);
		if(k==v[v[x].ch[0]].num+1)return x;
		return get_num(v[x].ch[1],k-v[v[x].ch[0]].num-1);
	}
	int get_rev_num(const int x,const int k) {
		if(!x||!k)return 0;
		push_down(x);
		if(v[v[x].ch[1]].num>=k)return get_rev_num(v[x].ch[1],k);
		if(k==v[v[x].ch[1]].num+1)return x;
		return get_rev_num(v[x].ch[0],k-v[v[x].ch[1]].num-1);
	}
	int get_rank(const int x,const int k) {
		if(!x||!k)return 0;
		push_down(x);
		if(v[v[x].ch[0]].sz>=k)return get_rank(v[x].ch[0],k);
		if(v[x].rep&&k==v[v[x].ch[0]].sz+v[x].rep)return x;
		return get_rank(v[x].ch[1],k-v[v[x].ch[0]].sz-v[x].rep);
	}
	void splay(const int x,const int f=0) {
		pre_push_down(x);
		if(!f)root=x;
		while(!isroot(x)&&v[x].fa!=f) {
			int p=v[x].fa,g=v[p].fa;
			if(!isroot(p)&&g!=f)rot(v[p].ch[0]==x^v[g].ch[0]==p?x:p);
			rot(x);
		}
	}
	int get_suf(int x) {
		splay(x),x=v[x].ch[0],push_down(x);
		while(v[x].ch[1])x=v[x].ch[1],push_down(x);
		return x;
	}
	int get_pre(int x) {
		splay(x),x=v[x].ch[1],push_down(x);
		while(v[x].ch[0])x=v[x].ch[0],push_down(x);
		return x;
	}
	int cut(int x,int y) {
		splay(x),(y=y>1?get_num(v[x].ch[1],2*(y-1)):x),splay(y,x);
		int l=get_suf(x),r=get_pre(y),ret=0;
		splay(l),splay(r,l),splay(r=v[v[r].ch[1]].pre,l);
		ret=v[r].ch[0],v[r].ch[0]=v[v[r].ch[0]].fa=0;
		push_up(r);
		return ret;
	}
	void link(int x,int y) {
		splay(x),splay(v[v[x].ch[1]].pre,x),splay(y),splay(y=v[y].suf);
		v[y].ch[1]=v[x].ch[1],v[y].fa=x,v[v[x].ch[1]].fa=y,v[x].ch[1]=y,root=v[root].fa;
		push_up(y),push_up(x);
	}
	void reverse(int l,int r,bool tag) {
		if(l==r)return v[l].d=tag?0:v[l].d,void();
		splay(l),splay(r,l);
		l=get_suf(l),r=get_pre(r);
		splay(l),splay(r,l),push_rev(v[r].ch[0]);
		if(tag)push_tag(v[r].ch[0]);
	}
	void reverse_sz(int l,int sz,bool tag) {
		splay(l),reverse((sz=sz>1?get_rev_num(v[l].ch[0],2*(sz-1)):l),l,tag);
	}
	int askdirection(const int x) {
		return splay(x),v[x].d;
	}
}
namespace LCT {
	int root;
	struct node {
		int ch[2],fa,rev;
		int pre,suf,sz,szl;
	} v[100005];
	bool isroot(const int x) {
		return v[v[x].fa].ch[0]!=x&&v[v[x].fa].ch[1]!=x;
	}
	void push_rev(const int x) {
		if(!x)return;
		swap(v[x].ch[0],v[x].ch[1]);
		swap(v[x].pre,v[x].suf);
		v[x].rev^=1;
	}
	void push_up(const int x) {
		v[x].pre=v[x].suf=x;
		v[x].sz=v[v[x].ch[0]].sz+v[v[x].ch[1]].sz+v[x].szl+1;
		if(v[x].ch[0])v[x].pre=v[v[x].ch[0]].pre;
		if(v[x].ch[1])v[x].suf=v[v[x].ch[1]].suf;
	}
	void push_down(const int x) {
		if(v[x].rev) {
			push_rev(v[x].ch[0]);
			push_rev(v[x].ch[1]);
			v[x].rev=0;
		}
	}
	void rot(const int x) {
		const int p=v[x].fa,g=v[p].fa;
		bool d=v[p].ch[1]==x;
		if(!isroot(p))v[g].ch[v[g].ch[1]==p]=x;
		v[p].ch[d]=v[x].ch[d^1];
		v[v[x].ch[d^1]].fa=p;
		v[x].ch[d^1]=p;
		v[x].fa=g;
		v[p].fa=x;
		push_up(p);
		push_up(x);
	}
	void pre_push_down(const int x) {
		if(!isroot(x))pre_push_down(v[x].fa);
		push_down(x);
	}
	void splay(const int x) {
		pre_push_down(x);
		while(!isroot(x)) {
			int p=v[x].fa,g=v[p].fa;
			if(!isroot(p))rot(v[p].ch[0]==x^v[g].ch[0]==p?x:p);
			rot(x);
		}
	}
	int access(int x) {
		int ret=0;
		for(int y=0; x; y=x,x=v[x].fa) {
			splay(x);
			v[x].szl-=v[y].sz,v[x].szl+=v[v[x].ch[1]].sz;
			if(ETT::askdirection(rep[v[x].pre]))ETT::reverse_sz(rep[v[x].pre],v[x].sz,1);
			if(y) {
				if(ETT::askdirection(rep[v[y].pre]))ETT::reverse_sz(rep[v[y].pre],v[y].sz,1);
				ETT::link(rep[x],ETT::cut(rep[v[y].pre],v[y].sz));
			}
			v[x].ch[1]=y,push_up(x),ret=x;
		}
		return ret;
	}
	void makeroot(const int x) {
		int r=0;
		if(x==root)return;
		access(x),splay(x),push_rev(x);
		ETT::reverse(rep[root],rep[x],0);
		ETT::splay(rep[root]),ETT::splay(r=ETT::v[rep[root]].suf,rep[root]),ETT::push_down(r);
		ETT::splay(r=ETT::v[ETT::v[r].ch[0]].suf,rep[root]),ETT::v[r].id=x;
		ETT::push_rev(ETT::v[r].ch[0]),ETT::push_tag(ETT::v[r].ch[0]);
		root=x;
	}
	int LCA(const int x,const int y) {
		access(x);
		return access(y);
	}
	int get_fa(const int x) {
		access(x),splay(x);
		return v[v[x].ch[0]].suf;
	}
	void modify(int x,int y,tag val) {
		makeroot(x),access(y),ETT::splay(rep[y]);
		ETT::v[rep[y]].val=val.calc(ETT::v[rep[y]].val),ETT::push_sum(ETT::v[rep[y]].ch[0],val);
	}
	void modify(int x,tag val) {
		if(!x)return;
		access(x),ETT::splay(rep[x]);
		ETT::v[rep[x]].val=val.calc(ETT::v[rep[x]].val);
		if(v[x].szl) {
			int y=ETT::get_num(ETT::v[rep[x]].ch[1],2*v[x].szl);
			ETT::splay(y,rep[x]),ETT::splay(y=ETT::v[ETT::v[y].ch[1]].pre,rep[x]);
			ETT::push_sum(ETT::v[y].ch[0],val);
		}
		ETT::push_up(ETT::v[rep[x]].ch[1]),ETT::push_up(rep[x]);
	}
	data ask(int x,int y) {
		data ret=data();
		makeroot(x),access(y),ETT::splay(rep[y]);
		ret=(ret+data(ETT::v[rep[y]].val,1));
		ret=(ret+ETT::v[ETT::v[rep[y]].ch[0]].sum);
		return ret;
	}
	data ask(int x) {
		data ret=data();
		access(x),ETT::splay(rep[x]),ret=(ret+data(ETT::v[rep[x]].val,1));
		if(v[x].szl) {
			int y=ETT::get_num(ETT::v[rep[x]].ch[1],2*v[x].szl);
			ETT::splay(y,rep[x]),ETT::splay(y=ETT::v[ETT::v[y].ch[1]].pre,rep[x]);
			ret=(ret+ETT::v[ETT::v[y].ch[0]].sum);
		}
		return ret;
	}
	void link(int x,int y) {
		access(x),splay(x);
		v[v[x].ch[0]].fa=0,v[x].ch[0]=0,push_up(x);
		access(y),splay(y),v[x].fa=y,v[y].szl+=v[x].sz,push_up(y);
		ETT::link(rep[y],ETT::cut(rep[x],v[x].sz)),ETT::splay(rep[x]);
		int z=ETT::get_num(ETT::v[rep[x]].ch[1],v[x].sz*2-1);
		ETT::splay(z),ETT::v[z].id=y,ETT::splay(rep[x]);
	}
}
void dfs(int x,int prt) {
	et[++et[0]]=x,rep[x]=et[0],sz[x]=1,LCT::v[x].fa=prt;
	for(int y:vec[x])if(y^prt)dfs(y,x),et[++et[0]]=x,sz[x]+=sz[y];
	LCT::v[x].szl=sz[x]-1,LCT::push_up(x);
}
int root;
int main() {
	n=getint(),m=getint();
	for(int i=1; i<n; i++) {
		int u=getint(),v=getint();
		vec[u].push_back(v),vec[v].push_back(u);
	}
	for(int i=1; i<=n; i++)a[i]=getint(),LCT::push_up(i);
	dfs(1,0),ETT::build(1,et[0],0,et),LCT::root=1,root=1;
	LCT::makeroot(root=getint());
	for(int i=1; i<=m; i++) {
		int opt=getint();
		if(opt==0) {
			int x=getint(),val=getint();
			LCT::makeroot(root),LCT::modify(x,tag(0,val));
		}
		if(opt==1)LCT::makeroot(root=getint());
		if(opt==2) {
			int x=getint(),y=getint(),val=getint();
			LCT::modify(x,y,tag(0,val));
		}
		if(opt==3)LCT::makeroot(root),wrt(LCT::ask(getint()).mi),pc('\n');
		if(opt==4)LCT::makeroot(root),wrt(LCT::ask(getint()).mx),pc('\n');
		if(opt==5) {
			int x=getint(),val=getint();
			LCT::makeroot(root),LCT::modify(x,tag(1,val));
		}
		if(opt==6) {
			int x=getint(),y=getint(),val=getint();
			LCT::modify(x,y,tag(1,val));
		}
		if(opt==7) {
			int x=getint(),y=getint();
			wrt(LCT::ask(x,y).mi),pc('\n');
		}
		if(opt==8) {
			int x=getint(),y=getint();
			wrt(LCT::ask(x,y).mx),pc('\n');
		}
		if(opt==9) {
			LCT::makeroot(root);
			int x=getint(),y=getint(),lca=LCT::LCA(x,y);
			if(lca!=x)LCT::link(x,y);
		}
		if(opt==10) {
			int x=getint(),y=getint();
			wrt(LCT::ask(x,y).sum),pc('\n');
		}
		if(opt==11)LCT::makeroot(root),wrt(LCT::ask(getint()).sum),pc('\n');
	}
	fwrite(wb,1,p2,stdout);
	return Loli Kon;
}