样例RE 19pts求调

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

yintaocheng @ 2023-11-13 15:53:47


#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read()
{
    char c;
    bool f=false; 
    int s=0;
    c=getchar();
    while(!isdigit(c))
    {
        f=(c=='-');
        c=getchar();
    }
    while(isdigit(c))
    {
        s=(s<<3)+(s<<1)+c-'0';
        c=getchar();
    }
    return f?-s:s;
}
void write(int x)
{
    if(x<0)
    {
        putchar('-');
        x=-x;
    }
    if(x>9)
    {
        write(x/10);
    }
    putchar(x%10+'0');
}
const int N=100010;
int n,m,r,mod;
struct Edge
{
    int to,nxt;
}edge[N<<1];
int head[N<<1],cnt=0;
inline void init()
{
    for(int i=0;i<2*N;i++)
    {
        edge[i].to=-1;
    }
    memset(head,-1,sizeof(head));
    cnt=0;
}
inline void addedge(int u,int v)
{
    edge[cnt].to=v;
    edge[cnt].nxt=head[u];
    head[u]=cnt++;
}
int w[N],w_new[N];
int tree[N<<2],tag[N<<2];
inline void addtag(int p,int pl,int pr,int d)
{
    tag[p]+=d;
    tree[p]+=(pr-pl+1)*d;
    tree[p]%=mod;
}
inline int ls(int x)
{
    return x<<1;
}
inline int rs(int x)
{
    return x<<1|1;
}
inline void push_up(int p)
{
    tree[p]=(tree[ls(p)]+tree[rs(p)])%mod;
}
inline void push_down(int p,int pl,int pr)
{
    if(tag[p])
    {
        int mid=(pl+pr)>>1;
        addtag(ls(p),pl,mid,tag[p]);
        addtag(rs(p),mid+1,pr,tag[p]);
        tag[p]=0;
    }
}
void build(int p,int pl,int pr)
{
    tag[p]=0;
    if(pl==pr)
    {
        tree[p]=w_new[pl];
        tree[p]%=mod;
        return;
    }
    int mid=(pl+pr)>>1;
    build(ls(p),pl,mid);
    build(rs(p),mid+1,pr);
    push_up(p);
}
void update(int p,int pl,int pr,int d,int l,int r)
{
    if(l<=pl&&pr<=r)
    {
        addtag(p,pl,pr,d);
        return;
    }
    push_down(p,pl,pr);
    int mid=(pl+pr)>>1;
    if(l<=mid)
        update(ls(p),pl,mid,d,l,r);
    if(r>=mid+1)
        update(rs(p),mid+1,pr,d,l,r);
    push_up(p);
}
int query(int p,int pl,int pr,int l,int r)
{
    if(l<=pl&&pr<=r)return tree[p]%=mod;
    push_down(p,pl,pr);
    int res=0;
    int mid=(pl+pr)>>1;
    if(l<=mid)
        res+=query(ls(p),pl,mid,l,r);
    if(r>mid)
        res+=query(rs(p),mid+1,pr,l,r);
    return res;
}
int son[N],id[N],fa[N],dep[N],siz[N],top[N];
void dfs1(int x,int father)
{
    dep[N]=dep[father]+1;
    fa[x]=father;
    siz[x]=1;
    for(int i=head[x];~i;i=edge[i].nxt)
    {
        int y=edge[i].to;
        if(y!=father)
        {
            fa[y]=x;
            dfs1(y,x);
            siz[x]+=siz[y];
            if(!son[x]||siz[son[x]]<siz[y])
                son[x]=y;
        }
    }
}
int num=0;
void dfs2(int x,int topx)
{
    id[x]=++num;
    w_new[num]=w[x];
    top[x]=topx;
    if(!son[x])
        return;
    dfs2(son[x],topx);
    for(int i=head[x];~i;i=edge[i].nxt)
    {
        int y=edge[i].to;
        if(y!=fa[x]&&y!=son[x])
            dfs2(y,y);
    }
}
inline void update_range(int x,int y,int z)
{
    int ans=0;
    while(top[x]!=top[y])
    {
        if(dep[top[x]]<dep[top[y]])
            swap(x,y);
        update(1,1,n,z,id[top[x]],id[x]);
        x=fa[top[x]];
    }
    if(dep[x]>dep[y])
        swap(x,y);
    update(1,1,n,z,id[x],id[y]);
}
inline int query_range(int x,int y)
{
    int ans=0;
    while(top[x]!=top[y])
    {
        if(dep[top[x]]<dep[top[y]])
            swap(x,y);
        ans+=query(1,1,n,id[top[x]],id[x]);
        ans%=mod;
        x=fa[top[x]];
    }
    if(dep[x]>dep[y])
        swap(x,y);
    ans+=query(1,1,n,id[x],id[y]);
    return ans%mod;
}
inline void update_tree(int x,int k)
{
    update(1,1,n,k,id[x],id[x]+siz[x]-1);
}
inline int query_tree(int x)
{
    return query(1,1,n,id[x],id[x]+siz[x]-1)%mod;
}
signed main()
{
    init();
    n=read();
    m=read();
    r=read();
    mod=read();
    for(int i=1;i<=n;i++)
        w[i]=read();
    for(int i=1;i<n;i++)
    {
        int u=read(),v=read();
        addedge(u,v);
        addedge(v,u);
    }
    dfs1(r,0);
    dfs2(r,r);
    build(1,1,n);
    while(m--)
    {
        int k,x,y,z;
        k=read();
        if(k==1)
        {
            x=read();
            y=read();
            z=read();
            update_range(x,y,z);
        }
        else if(k==2)
        {
            x=read();
            y=read();
            write(query_range(x,y));
            putchar('\n');
        }
        else if(k==3)
        {
            x=read();
            y=read();
            update_tree(x,y);
        }
        else
        {
            x=read();
            write(query_tree(x));
            putchar('\n');
        }
    }
    return 0;
}

by A_Cabbage @ 2023-11-13 16:27:33

@yintaocheng

你的dfs1函数里:

dep[N]=dep[father]+1;

by yintaocheng @ 2023-11-13 16:32:25

@A_Cabbage 又多一个逆天笔误


|