P3384 【模板】重链剖分/树链剖分

qhzx_FeS2_Butterfly @ 2023-02-17 10:01:13

https://www.luogu.com.cn/discuss/572979


by qhzx_FeS2_Butterfly @ 2023-02-17 10:26:52

仍然是没过

#include<bits/stdc++.h>
using namespace std;
vector<int> g[114514];
int n,p,cnt,w[114514],fa[114514],dep[114514],val[114514],son[114514],dfn[114514],rk[114514],top[114514];
long long lazy1[114514],lazy2[114514];
int lowbit(int x)
{
    return x&(-x);
}
void add(int l,int r,int x)
{
    x%=p;
    int add1=(long long)(l-1)*x%p,add2=(long long)r*x%p;
    for(int i=l;i<=n;i+=lowbit(i))
    {
        lazy1[i]+=x;
        lazy1[i]%=p;
        lazy2[i]+=add1;
        lazy2[i]%=p;
    }
    for(int i=r+1;i<=n;i+=lowbit(i))
    {
        lazy1[i]-=x;
        lazy1[i]%=p;
        lazy1[i]+=p;
        lazy1[i]%=p;
        lazy2[i]-=add2;
        lazy2[i]%=p;
    }
}
int cg(int x)
{
    int res=0;
    for(int i=x;i>0;i-=lowbit(i))
    {
        res+=((long long)x*lazy1[i]%p);
        res%=p;
        res-=lazy2[i];
        res%=p;
        res+=p;
        res%=p;
    }
    return res;
}
int query(int l,int r)
{
    int res=(cg(r)-cg(l-1))%p;
    return (res+p)%p;
}
void dfs1(int u,int f,int d)
{
    fa[u]=f;
    dep[u]=d;
    val[u]=1;
    son[u]=0;
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        if(f==v)continue;
        dfs1(v,u,d+1);
        val[u]+=val[v];
        if(val[v]>val[son[u]])son[u]=v;
    }
}
void dfs2(int u,int topfa)
{
    dfn[u]=++cnt;
    rk[cnt]=u;
    top[u]=topfa;
    if(w[u]!=0)add(dfn[u],dfn[u],w[u]);
    if(g[u].size()==0)return;
    dfs2(son[u],topfa);
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        if(v==fa[u]||v==son[u])continue;
        dfs2(v,v);
    }
}
void addpath(int u,int v,int k)
{
    while(top[u]!=top[v])
    {
        if(dep[top[u]]<dep[top[v]])swap(u,v);
        add(dfn[top[u]],dfn[u],k);
        u=fa[top[u]];
    }
    if(dep[u]>dep[v])swap(u,v);
    add(dfn[u],dfn[v],k);
}
int querypath(int u,int v)
{
    int res=0;
    while(top[u]!=top[v])
    {
        if(dep[top[u]]<dep[top[v]])swap(u,v);
        res+=query(dfn[top[u]],dfn[u]);
        res%=p;
        u=fa[top[u]];
    }
    if(dep[u]>dep[v])swap(u,v);
    res+=query(dfn[u],dfn[v]);
    res%=p;
    return res;
}
int main()
{
    int m,r,op,x,y,z;
    cin>>n>>m>>r>>p;
    for(int i=1;i<=n;i++)cin>>w[i];
    for(int i=1;i<n;i++)
    {
        cin>>x>>y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    dfs1(r,0,1);
    dfs2(r,r);
    for(int i=0;i<m;i++)
    {
        cin>>op>>x;
        if(op==1)
        {
            cin>>y>>z;
            z%=p;
            addpath(x,y,z);
        }
        if(op==2)
        {
            cin>>y;
            cout<<querypath(x,y)<<'\n';
        }
        if(op==3)
        {
            cin>>z;
            z%=p;
            add(dfn[x],dfn[x]+val[x]-1,z);
        }
        if(op==4)cout<<query(dfn[x],dfn[x]+val[x]-1)<<'\n';
    }
    return 0;
}

但是样例过了


|