熟练剖粪模板求调

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

H2ptimize @ 2024-07-16 19:10:03

10pts,7 WA 3 RE

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int MAXN=1e5+10;

int n,m,root,P,a[MAXN],wt[MAXN],fa[MAXN],dep[MAXN],sz[MAXN],hv[MAXN],top[MAXN],dfn[MAXN],cnt,rk[MAXN];
vector<int>G[MAXN];

struct SegTree
{
    #define lc (x<<1)
    #define rc (x<<1|1)
    #define mid ((l+r)>>1)

    struct Node
    {
        int sum,lazy;
    }T[MAXN*4];

    void pushdown(int x,int l,int r)
    {
        if(!T[x].lazy)return;
        (T[lc].sum+=T[x].lazy*(mid-l+1))%=P;
        (T[lc].lazy+=T[x].lazy)%=P;
        (T[rc].sum+=T[x].lazy*(r-mid))%=P;
        (T[rc].lazy+=T[x].lazy)%=P;
        T[x].lazy=0;
    }

    void build(int x,int l,int r)
    {
        if(l==r)
        {
            T[x].sum=wt[x]%P;
            return;
        }
        build(lc,l,mid);
        build(rc,mid+1,r);
        (T[x].sum=T[lc].sum+T[rc].sum)%=P;
    }

    void upd(int x,int l,int r,int al,int ar,int k)
    {
        if(al<=l&&ar>=r)
        {
            (T[x].sum+=k*(r-l+1))%=P;
            (T[x].lazy+=k)%=P;
            return;
        }
        pushdown(x,l,r);
        if(al<=mid)upd(lc,l,mid,al,ar,k);
        if(ar>mid)upd(rc,mid+1,r,al,ar,k);
        (T[x].sum=T[lc].sum+T[rc].sum)%=P;
        return;
    }

    int query(int x,int l,int r,int al,int ar)
    {
        int ans=0;
        if(al<=l&&ar>=r)return T[x].sum;
        pushdown(x,l,r);
        if(al<=mid)(ans+=query(lc,l,mid,al,ar))%=P;
        if(ar>mid)(ans+=query(rc,mid+1,r,al,ar))%=P;
        return ans;
    }

    #undef lc
    #undef rc
    #undef mid
}T;

void dfs1(int u,int pre)
{
    sz[u]=1;
    for(int v:G[u])
    {
        if(v==pre)continue;
        dep[v]=dep[u]+1;
        fa[v]=u;
        dfs1(v,u);
        sz[u]+=sz[v];
        if(!hv[u]||sz[v]>sz[hv[u]])hv[u]=v;
    }
}

void dfs2(int u,int tp)
{
    top[u]=tp;
    cnt++;
    dfn[u]=cnt;
    rk[cnt]=u;
    wt[cnt]=a[u];
    if(!hv[u])return;
    dfs2(hv[u],tp);
    for(auto v:G[u])
    {
        if(v!=hv[u]&&v!=fa[u])dfs2(v,v);
    }
}

void lca1(int u,int v,int w)
{
    while(top[u]!=top[v])
    {
        if(dep[top[u]]<dep[top[v]])swap(u,v);
        T.upd(1,1,n,dfn[top[u]],dfn[u],w);
        u=fa[top[u]];
    }
    if(dep[u]>dep[v])swap(u,v);
    T.upd(1,1,n,dfn[u],dfn[v],w);
}

int lca2(int u,int v)
{
    int ret=0;
    while(top[u]!=top[v])
    {
        if(dep[top[u]]<dep[top[v]])swap(u,v);
        (ret+=T.query(1,1,n,dfn[top[u]],dfn[u]))%=P;
        u=fa[top[u]];
    }
    if(dep[u]>dep[v])swap(u,v);
    (ret+=T.query(1,1,n,dfn[u],dfn[v]))%=P;
    return ret;
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m>>root>>P;
    for(int i=1;i<=n;i++)cin>>a[i];
    for(int i=1;i<n;i++)
    {
        int u,v;
        cin>>u>>v;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dep[root]=1;
    dfs1(root,0);
    dfs2(root,root);
    T.build(1,1,n);
    while(m--)
    {
        int op;
        cin>>op;
        switch(op)
        {
            case 1:
            {
                int x,y,z;
                cin>>x>>y>>z;
                lca1(x,y,z);
                break;
            }
            case 2:
            {
                int x,y;
                cin>>x>>y;
                cout<<lca2(x,y)<<'\n';
                break;
            }
            case 3:
            {
                int x,z;
                cin>>x>>z;
                T.upd(1,1,n,dfn[x],dfn[x]+sz[x]-1,z);
                break;
            }
            case 4:
            {
                int x;
                cin>>x;
                cout<<T.query(1,1,n,dfn[x],dfn[x]+sz[x]-1)<<'\n';
                break;
            }
        }
    }
    return 0;
}

by okidd @ 2024-07-16 19:35:44

这么离谱的测评结果别调了,重写吧


by dsy081101 @ 2024-07-16 19:46:30

@H2ptimize 你的build写错了

递归结束时T[x].sum要设为wt[l] 而不是 wt[x]


by H2ptimize @ 2024-07-16 19:52:02

@dsy081101 dalao /bx,已关


by dsy081101 @ 2024-07-16 19:54:41

@H2ptimize QwQ骗到关注了


by GY程袁浩 @ 2024-07-22 09:47:28

@dsy081101 %%%


by dsy081101 @ 2024-07-22 11:04:47

@GY程袁浩 /kk


|