树链剖分

· · 个人记录

#include<bits/stdc++.h>
using namespace std;
#define re register
#define ll long long
#define endl '\n'
#define lc u<<1
#define rc u<<1|1

inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch-'0');ch=getchar();}return x*f;}inline void write(int x){if(x<0)x*=-1,putchar('-');if(x>9)write(x/10);putchar(x%10+'0');return;}inline int max(int x,int y){return (x<y)?y:x;}inline int min(int x,int y){return (x<y)?x:y;}

const int N=100010;

ll n,m,a,b,root,p;
ll w[N];
vector<ll> e[N];
ll d[N],f[N],son[N],size[N];
ll top[N],id[N],nw[N],cnt;

void dfs1(ll u,ll fa)
{
    f[u]=fa;d[u]=d[fa]+1;size[u]=1;
    for(int auv:e[u])
    {
        if(auv==fa)continue;
        dfs1(auv,u);
        size[u]+=size[auv];
        if(size[son[u]]<size[auv])son[u]=auv;
    }
}

void dfs2(ll u,ll t)
{
    top[u]=t,id[u]=++cnt,nw[cnt]=w[u];
    if(!son[u])return;
    dfs2(son[u],t);
    for(int auv:e[u])
    {
        if(auv==f[u]||auv==son[u])continue;
        dfs2(auv,auv);
    }
}

struct tree{
    ll l,r,add,sum;
}tr[N*4];

void pushup(ll u){
    tr[u].sum=tr[lc].sum+tr[rc].sum;
}

void pushdown(ll u)
{
    if(tr[u].add)
    {
        tr[lc].sum+=tr[u].add*(tr[lc].r-tr[lc].l+1);
        tr[rc].sum+=tr[u].add*(tr[rc].r-tr[rc].l+1);
        tr[lc].add+=tr[u].add;
        tr[rc].add+=tr[u].add;
        tr[u].add=0;
    }
}

void build(ll u,ll l,ll r)
{
    tr[u]={l,r,0,nw[r]};
    if(l==r)return;
    ll mid=l+r>>1;
    build(lc,l,mid),build(rc,mid+1,r);
    pushup(u);
}

void update(ll u,ll l,ll r,ll k)
{
    if(l<=tr[u].l&&r>=tr[u].r)
    {
        tr[u].add+=k;
        tr[u].sum+=k*(tr[u].r-tr[u].l+1);
        return;
    }
    pushdown(u);
    ll mid=tr[u].l+tr[u].r>>1;
    if(l<=mid) update(lc,l,r,k);
    if(r>mid) update(rc,l,r,k);
    pushup(u);
}

void update_path(ll u,ll v,ll k)
{
    while(top[u]!=top[v])
    {
        if(d[top[u]]<d[top[v]])swap(u,v);
        update(1,id[top[u]],id[u],k);
        u=f[top[u]];
    }
    if(d[u]<d[v])swap(u,v);
    update(1,id[v],id[u],k);
}

void update_tree(ll u,ll k)
{
    update(1,id[u],id[u]+size[u]-1,k);
}

ll query(ll u,ll l,ll r)
{
    if(l<=tr[u].l&&r>=tr[u].r)return tr[u].sum;
    pushdown(u);
    ll mid=tr[u].l+tr[u].r>>1;
    ll res=0;
    if(l<=mid) res+=query(lc,l,r);
    if(r>mid) res+=query(rc,l,r);
    return res;
}

ll query_path(ll u,ll v)
{
    ll res=0;
    while(top[u]!=top[v])
    {
        if(d[top[u]]<d[top[v]])swap(u,v);
        res+=query(1,id[top[u]],id[u]);
        u=f[top[u]];
    }
    if(d[u]<d[v]) swap(u,v);
    res+=query(1,id[v],id[u]);
    return res;
}

ll query_tree(ll u)
{
    return query(1,id[u],id[u]+size[u]-1);
}

signed main()
{
    n=read(),m=read(),root=read(),p=read();
    for(ll i=1;i<=n;i++)
        w[i]=read();
    for(ll i=0;i<n-1;i++)
    {
        ll x=read(),y=read();
        e[x].push_back(y);
        e[y].push_back(x);
    }
    dfs1(root,0);
    dfs2(root,root);
    build(1,1,n);
    while(m--)
    {
        ll t,u,v,k;
        t=read(),u=read();
        if(t==1){
            v=read(),k=read();
            update_path(u,v,k);
        }
        else if(t==3){
            k=read();
            update_tree(u,k);
        }
        else if(t==2){
            v=read();
            printf("%lld\n",query_path(u,v)%p);
        }
        else printf("%lld\n",query_tree(u)%p);  
    }
    return 0;
}