#2#9 TLE #10 WA
求调【大悲】
#include<bits/stdc++.h>
#define ll long long
using namespace std;
struct node
{
ll l,r,pre,add;
}t[400010];
struct edge
{
ll to,next;
}e[100010];
ll n,m,r,p,cnt=0,cnt1=0,opt;
ll head[100010],c[100010];
ll fa[100010],dep[100010],siz[100010],son[100010];
ll id[100010],top[10010],w[100010];
void add(int from,int to)
{
cnt++;
e[cnt].next=head[from];
head[from]=cnt;
e[cnt].to=to;
}
void build(int x,int a,int b)
{
t[x].l=a;t[x].r=b;
if(a==b)
{
t[x].pre=w[a];
return ;
}
ll mid=a+b>>1;
build(x*2,a,mid);
build(x*2+1,mid+1,b);
t[x].pre=t[x*2].pre+t[x*2+1].pre;
return ;
}
void spread(int x)
{
if(t[x].add)
{
t[x*2].pre+=t[x].add*(t[x*2].r-t[x*2].l+1);
t[x*2+1].pre+=t[x].add*(t[x*2+1].r-t[x*2+1].l+1);
t[x*2].add+=t[x].add;
t[x*2+1].add+=t[x].add;
t[x].add=0;
}
}
void change(ll x,ll a,ll b,ll s)
{
if(a<=t[x].l&&b>=t[x].r)
{
t[x].add+=s;
t[x].pre+=s*(t[x].r-t[x].l+1);
return ;
}
spread(x);
int mid=t[x].l+t[x].r>>1;
if(a<=mid)
{
change(x*2,a,b,s);
}
if(b>mid)
{
change(x*2+1,a,b,s);
}
t[x].pre=t[x*2].pre+t[x*2+1].pre;
return ;
}
ll ask(ll x,ll a,ll b)
{
ll ans=0;
if(a<=t[x].l&&b>=t[x].r)
{
return t[x].pre;
}
spread(x);
int mid=t[x].l+t[x].r>>1;
if(a<=mid)
{
ans=(ans+ask(x*2,a,b))%p;
}
if(b>mid)
{
ans=(ans+ask(x*2+1,a,b))%p;
}
return ans%p;
}
void dfs1(int x,int f,int d)
{
dep[x]=d;fa[x]=f;siz[x]=1;son[x]=-1;
ll maxson=-1;
for(int i=head[x];i;i=e[i].next)
{
if(e[i].to==f)
{
continue;
}
dfs1(e[i].to,x,d+1);
siz[x]+=siz[e[i].to];
if(siz[e[i].to]>maxson)
{
maxson=siz[e[i].to];
son[x]=e[i].to;
}
}
}
void dfs2(int x,int tp)
{
cnt1++;
id[x]=cnt1;
w[cnt1]=c[x];top[x]=tp;
if(son[x]==-1)
{
return ;
}
dfs2(son[x],tp);
for(int i=head[x];i;i=e[i].next)
{
if(e[i].to==fa[x]||e[i].to==son[x])
{
continue;
}
dfs2(e[i].to,e[i].to);
}
return ;
}
ll lask(ll x,ll y)
{
ll ans=0;
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]])
{
swap(x,y);
}
ans=(ans+ask(1,id[top[x]],id[x]))%p;
x=fa[top[x]];
}
if(dep[x]>dep[y])
{
swap(x,y);
}
ans=(ans+ask(1,id[x],id[y]))%p;
return ans%p;
}
ll sask(int x)
{
ll ans=ask(1,id[x],id[x]+siz[x]-1);
return ans%p;
}
void lchange(ll x,ll y,ll k)
{
k%=p;
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]])
{
swap(x,y);
}
change(1,id[top[x]],id[x],k);
x=fa[top[x]];
}
if(dep[x]>dep[y])
{
swap(x,y);
}
change(1,id[x],id[y],k);
return ;
}
void schange(ll x,ll k)
{
k%=p;
change(1,id[x],id[x]+siz[x]-1,k);
return ;
}
int main()
{
cin>>n>>m>>r>>p;
for(int i=1;i<=n;i++)
{
cin>>c[i];
}
for(int i=1;i<=n-1;i++)
{
int x,y;
cin>>x>>y;
add(x,y);
add(y,x);
}
dfs1(r,0,1);
dfs2(r,r);
build(1,1,n);
for(int i=1;i<=m;i++)
{
cin>>opt;
if(opt==1)
{
int x,y,z;
cin>>x>>y>>z;
lchange(x,y,z);
}
if(opt==2)
{
int x,y;
cin>>x>>y;
cout<<lask(x,y)<<endl;
}
if(opt==3)
{
int x,z;
cin>>x>>z;
schange(x,z);
}
if(opt==4)
{
int x;
cin>>x;
cout<<sask(x)<<endl;
}
}
return 0;
}