树链剖分

树链剖分

强烈安利一个树链剖分入门讲解代码 

(手动感谢这位神仙QWQ,窝才刚入门啊QAQ)

(最水的一篇博客,不接受反驳)

P3384 【模板】树链剖分

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<cstdlib>
#include<queue>

using namespace std;

inline int read()
{
    int ans=0;
    char last=' ',ch=getchar();
    while(ch<'0'||ch>'9') last=ch,ch=getchar();
    while(ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar();
    if(last=='-') ans=-ans;
    return ans;
}

const int maxn=1e5+10;
int n,m,r,mod;
int w[maxn],wt[maxn];
int fa[maxn],son[maxn],size[maxn],dfn[maxn],dep[maxn],top[maxn],tim=0;
int sum[maxn<<2],laz[maxn<<2];
int head[maxn],cnt=0;
struct node{
    int nxt,to;
}edge[maxn<<2];
inline void addedge(int a,int b)
{
    edge[++cnt].to =b;edge[cnt].nxt =head[a];head[a]=cnt;
    edge[++cnt].to =a;edge[cnt].nxt =head[b];head[b]=cnt;
}

inline void dfs1(int u,int f)
{
    fa[u]=f;
    son[u]=0;
    size[u]=1;
    for(int i=head[u];i;i=edge[i].nxt )
    {
        int v=edge[i].to ;
        if(v!=f){
            dfs1(v,u);
            size[u]+=size[v];
            if(size[v]>size[son[u]]) son[u]=v;
        }
    }
}

inline void dfs2(int u,int f)
{
    dfn[u]=++tim;
    dep[u]=dep[f]+1;
    top[u]=(u==son[f]?top[f]:u);
    wt[tim]=w[u];
    if(son[u]!=0) dfs2(son[u],u);
    for(int i=head[u];i;i=edge[i].nxt )
    {
        int v=edge[i].to ;
        if(v!=f&&v!=son[u])
          dfs2(v,u);
    }
}

inline void build(int k,int l,int r)
{
    if(l==r){
        sum[k]=wt[l];
        sum[k]%=mod;
        return ;
    }
    int mid=(l+r)>>1;
    build(k<<1,l,mid);
    build(k<<1|1,mid+1,r);
    sum[k]=(sum[k<<1]+sum[k<<1|1])%mod;
}

inline void pushdown(int k,int l,int r)
{
    int mid=(l+r)>>1;
    laz[k<<1]+=laz[k];
    laz[k<<1|1]+=laz[k];
    sum[k<<1]=(sum[k<<1]+laz[k]*(mid-l+1))%mod;
    sum[k<<1|1]=(sum[k<<1|1]+laz[k]*(r-mid))%mod;
    laz[k]=0;
}

long long query(int k,int l,int r,int x,int y)
{
    if(x<=l&&r<=y) return sum[k]%mod;
    int mid=(l+r)>>1;
    long long res=0;
    if(laz[k]!=0) pushdown(k,l,r);
    if(x<=mid) res+=query(k<<1,l,mid,x,y);
    if(mid<y) res+=query(k<<1|1,mid+1,r,x,y);
    return res;
}

inline void modify(int k,int l,int r,int x,int y,int v)
{
    if(x<=l&&r<=y)
    {
        laz[k]+=v;
        sum[k]+=v*(r-l+1);
        return ;
    }
    else
    {
        int mid=(l+r)>>1;
        if(laz[k]!=0) pushdown(k,l,r);
        if(x<=mid) modify(k<<1,l,mid,x,y,v);
        if(mid<y) modify(k<<1|1,mid+1,r,x,y,v);
        sum[k]=(sum[k<<1]+sum[k<<1|1])%mod;
    }
}

inline void chrode(int x,int y,int v)
{
    v%=mod;
    while(top[x]!=top[y])
    {
        if(dep[top[x]]<dep[top[y]]) swap(x,y);
        modify(1,1,n,dfn[top[x]],dfn[x],v);
        x=fa[top[x]];
    }
    if(dep[x]>dep[y]) swap(x,y);
    modify(1,1,n,dfn[x],dfn[y],v);
}

int qrode(int x,int y)
{
    int ans=0;
    while(top[x]!=top[y])
    {
        if(dep[top[x]]<dep[top[y]]) swap(x,y);
        ans=(ans+query(1,1,n,dfn[top[x]],dfn[x]))%mod;
        x=fa[top[x]];
    }
    if(dep[x]>dep[y]) swap(x,y);
    ans=(ans+query(1,1,n,dfn[x],dfn[y]))%mod;
    return ans%mod;
}

inline void chson(int x,int v)
{
    modify(1,1,n,dfn[x],dfn[x]+size[x]-1,v);
}

int qson(int x)
{
    return query(1,1,n,dfn[x],dfn[x]+size[x]-1)%mod;
}

int main()
{
    n=read();m=read();r=read();mod=read();
    for(int i=1;i<=n;i++) w[i]=read();
    int k,x,y,z;
    for(int i=1;i<n;i++){
        x=read(),y=read();
        addedge(x,y);
    } 
    
    dfs1(r,0);
    dfs2(r,0);
    build(1,1,n);
    
    for(int i=1;i<=m;i++){
        k=read();
        if(k==1){
            x=read();y=read();z=read();
            chrode(x,y,z);  
        }
        if(k==2){
            x=read();y=read();
            printf("%d\n",qrode(x,y));     
        }
        if(k==3){
            x=read();z=read();
            chson(x,z);     
        }
        if(k==4){
            x=read();
            printf("%d\n",qson(x));        
        }
    }

    return 0;
}

 

posted @ 2019-09-22 10:21  晔子  阅读(141)  评论(0编辑  收藏  举报