复习一遍
#include<cstdio>
#include<cstring>
#include<iostream>
#define N 150000
#define For(i,x,y) for(int i=x;i<=y;++i)
#define lson(x) (x<<1)
#define rson(x) ((x<<1)+1)
using namespace std;
typedef long long LL;
int n,m,r,M;
struct SegTree{
LL l[N*4],r[N*4],w[N*4],sum[N*4];
void upd(int x){sum[x]=(sum[lson(x)]+sum[rson(x)]+w[x]*(r[x]-l[x]+1))%M;}
void build(int p,int L,int R){
l[p]=L;r[p]=R;w[p]=0;
if(L==R)return;
int MID=(L+R)>>1;
build(lson(p),L,MID);
build(rson(p),MID+1,R);
}
void ins(int p,int x,int v){
int L=l[p],R=r[p];int MID=(L+R)>>1;
if(L==R){
w[p]+=v;w[p]%=M;upd(p);return;
}
if(x<=MID)ins(lson(p),x,v);
else ins(rson(p),x,v);
upd(p);
}
void add(int x,int L,int R,int v){
int mid=(l[x]+r[x])>>1;
if(l[x]==L&&r[x]==R){
w[x]+=v;w[x]%=M;upd(x);return;
}
if(R<=mid)add(lson(x),L,R,v);
if(L>mid)add(rson(x),L,R,v);
if(R>mid&&L<=mid){
add(lson(x),L,mid,v);
add(rson(x),mid+1,R,v);
}
upd(x);
}
int getsum(int x,int L,int R){
int mid=(l[x]+r[x])>>1;int tans=w[x]*(R-L+1)%M;
if(l[x]==L&&r[x]==R)
return sum[x];
if(R<=mid)return (getsum(lson(x),L,R)+tans)%M;
if(L>mid)return (getsum(rson(x),L,R)+tans)%M;
if(R>mid&&L<=mid)
return (getsum(lson(x),L,mid)+getsum(rson(x),mid+1,R)+tans)%M;
}
}T;
struct Graph{
struct E{
int next,to;
}edge[N*2];
int head[N];int cnt;
bool vis[N];LL v[N],size[N],son[N],top[N],id[N],deep[N],fa[N],xid[N];
int sign;
void adde(int u,int v){
edge[++cnt].to=v;edge[cnt].next=head[u];head[u]=cnt;
edge[++cnt].to=u;edge[cnt].next=head[v];head[v]=cnt;
}
void dfs1(int u){
size[u]=1;int S=0;
for(int i=head[u];i;i=edge[i].next){
int v=edge[i].to;
if(vis[v])continue;vis[v]=1;
deep[v]=deep[u]+1;fa[v]=u;
dfs1(v);
size[u]+=size[v];
if(!S||size[v]>size[S])S=v;
}
son[u]=S;
}
void dfs2(int u,int tp){
id[u]=++sign;
top[u]=tp;
if(!son[u]){xid[u]=sign;return;}
vis[son[u]]=1;
dfs2(son[u],tp);
for(int i=head[u];i;i=edge[i].next){
int v=edge[i].to;if(vis[v])continue;
vis[v]=1;dfs2(v,v);
}
xid[u]=sign;
}
void prework(){
cin>>n>>m>>r>>M;
For(i,1,n)scanf("%d",&v[i]);
For(i,1,n-1){
int u,v;scanf("%d%d",&u,&v);
adde(u,v);
}
vis[r]=1;
dfs1(r);
memset(vis,0,sizeof(vis));
vis[r]=1;
dfs2(r,r);
T.build(1,1,n);
For(i,1,n)T.ins(1,id[i],v[i]);
}
void add1(int u,int v,int w){
if(deep[top[u]]<deep[top[v]])swap(u,v);
while(top[u]!=top[v]){
T.add(1,id[top[u]],id[u],w);
u=fa[top[u]];
if(deep[top[u]]<deep[top[v]])swap(u,v);
}
if(deep[u]<deep[v])swap(u,v);
T.add(1,id[v],id[u],w);
}
int get(int u,int v){
int ans=0;
if(deep[top[u]]<deep[top[v]])swap(u,v);
while(top[u]!=top[v]){
ans=(ans+T.getsum(1,id[top[u]],id[u]))%M;
u=fa[top[u]];
if(deep[top[u]]<deep[top[v]])swap(u,v);
}
if(deep[u]<deep[v])swap(u,v);
ans=(ans+T.getsum(1,id[v],id[u]))%M;
return ans%M;
}
void add2(int u,int w){
T.add(1,id[u],xid[u],w);
}
int get2(int u){
return T.getsum(1,id[u],xid[u])%M;
}
}G;
int main(){
G.prework();
For(t,1,m){
int opt;
scanf("%d",&opt);
int u,v,w;
if(opt==1){
scanf("%d%d%d",&u,&v,&w);
G.add1(u,v,w);
}
if(opt==2){
scanf("%d%d",&u,&v);printf("%d\n",G.get(u,v));
}
if(opt==3){
scanf("%d%d",&u,&w);G.add2(u,w);
}
if(opt==4){
scanf("%d",&u);printf("%d\n",G.get2(u));
}
}
return 0;
}