bzoj 4034 树上操作 (树链剖分) or (dfs序+树状数组)

 

4034: [HAOI2015]树上操作

Time Limit: 10 Sec  Memory Limit: 256 MB
Submit: 8477  Solved: 3012
[Submit][Status][Discuss]

Description

有一棵点数为 N 的树,以点 1 为根,且树点有边权。然后有 M 个

操作,分为三种:

操作 1 :把某个节点 x 的点权增加 a 。

操作 2 :把某个节点 x 为根的子树中所有点的点权都增加 a 。

操作 3 :询问某个节点 x 到根的路径中所有点的点权和。

Input

第一行包含两个整数 N, M 。表示点数和操作数。接下来一行 N 个整数,表示树中节点的初始权值。接下来 N-1 

行每行三个正整数 fr, to , 表示该树中存在一条边 (fr, to) 。再接下来 M 行,每行分别表示一次操作。其中

第一个数表示该操作的种类( 1-3 ) ,之后接这个操作的参数( x 或者 x a ) 。

Output

对于每个询问操作,输出该询问的答案。答案之间用换行隔开。

Sample Input

5 5
1 2 3 4 5
1 2
1 4
2 3
2 5
3 3
1 2 1
3 5
2 1 2
3 3

Sample Output

6
9
13

HINT

 对于 100% 的数据, N,M<=100000 ,且所有输入数据的绝对值都不会超过 10^6 。

 

算是很裸的模版题,就是要开longlong坑了我好多发

树链剖分 代码一:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn=2e5+5;
const int inf=2e9;
int head[maxn],w[maxn],siz[maxn],mx[maxn],top[maxn],son[maxn],dep[maxn],fa[maxn],tid[maxn],rnk[maxn],tot,cnt,n,q;
struct edge{int to,next;}e[maxn];
inline void addedge(int u,int v)
{
    cnt++;e[cnt].next=head[u];head[u]=cnt;e[cnt].to=v;
}
struct segment
{
    ll sum[maxn<<2],add[maxn<<2];
    inline void pushdown(int o,int m)
    {
    	if(add[o])
    	{
    		add[o<<1]+=add[o];add[o<<1|1]+=add[o];
    		sum[o<<1]+=add[o]*(m-(m>>1));
    		sum[o<<1|1]+=add[o]*(m>>1);
    		add[o]=0;
    	}
    }
    inline void build(int o,int l,int r)
    {
    	add[o]=0;
        if(l==r){sum[o]=w[rnk[l]];return ;}
        int m=(l+r)>>1;
        build(o<<1,l,m);build(o<<1|1,m+1,r);
        sum[o]=sum[o<<1]+sum[o<<1|1];
    }
    inline ll query2(int o,int l,int r,int ql,int qr)// sum
    {
        if(l>qr||r<ql) return 0;
        if(ql<=l&&r<=qr)return sum[o];
        pushdown(o,r-l+1);
        int m=(l+r)>>1;ll res=0;
        if(ql<=m)res+=query2(o<<1,l,m,ql,qr);
        if(m<qr)res+=query2(o<<1|1,m+1,r,ql,qr);
        return res;
        //return query2(o<<1,l,m,ql,qr)+query2(o<<1|1,m+1,r,ql,qr);
    }
    inline void update(int o,int l,int r,int ql,int qr,ll c)
    {
        if(ql<=l&&r<=qr){add[o]+=c;sum[o]+=1LL*c*(r-l+1);return;}
        pushdown(o,r-l+1);
        int m=(l+r)>>1;
        if(ql<=m)update(o<<1,l,m,ql,qr,c);
        if(m<qr) update(o<<1|1,m+1,r,ql,qr,c);
        sum[o]=sum[o<<1]+sum[o<<1|1];
    }
}st;
inline void dfs1(int u,int fat)
{
    son[u]=-1;siz[u]=1;
    for(int i=head[u];i;i=e[i].next)
    {
        if(!dep[e[i].to])
        {
            dep[e[i].to]=dep[u]+1;
            fa[e[i].to]=u;dfs1(e[i].to,u);
            siz[u]+=siz[e[i].to];
            if(son[u]==-1||siz[e[i].to]>siz[son[u]])son[u]=e[i].to;
        }
    }
}
inline void dfs2(int u,int t)
{
    top[u]=t;tot++;
    tid[u]=mx[u]=tot;rnk[tot]=u;
    if(son[u]==-1)return;
    dfs2(son[u],t);mx[u]=max(mx[u],mx[son[u]]);
    for(int i=head[u];i;i=e[i].next)
        if(e[i].to!=son[u]&&e[i].to!=fa[u])dfs2(e[i].to,e[i].to),mx[u]=max(mx[u],mx[e[i].to]);
}
inline ll querysum(int x,int y)
{
    ll res=0,fx=top[x],fy=top[y];
    while(fx!=fy)
    {
        if(dep[fx]>=dep[fy])res+=st.query2(1,1,n,tid[fx],tid[x]),x=fa[fx];
        else res+=st.query2(1,1,n,tid[fy],tid[y]),y=fa[fy];
        fx=top[x];fy=top[y];
    }
    if(x!=y)
    {
        if(tid[x]<tid[y]) res+=st.query2(1,1,n,tid[x],tid[y]);
        else res+=st.query2(1,1,n,tid[y],tid[x]);
    } else res+=st.query2(1,1,n,tid[x],tid[y]);
    return res;
}
int main()
{
    scanf("%d%d",&n,&q);for(int i=1;i<=n;i++)scanf("%d",&w[i]);
    for(int i=1;i<n;i++)
    {
        int u,v;scanf("%d%d",&u,&v); addedge(u,v);addedge(v,u);
    }
    dep[1]=1;dfs1(1,-1);dfs2(1,1);st.build(1,1,n);
    while(q--)
    {
    	int op;scanf("%d",&op);
    	if(op==1){int x;ll a;scanf("%d%lld",&x,&a);st.update(1,1,n,tid[x],tid[x],a);}
    	if(op==2){int x;ll a;scanf("%d%lld",&x,&a);st.update(1,1,n,tid[x],mx[x],a);}
    	if(op==3){int x;scanf("%d",&x);printf("%lld\n",querysum(1,x));}
    }
    return 0;
}

dfs序 树状数组 代码二:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll maxn=2e5+5;
std::vector<ll> G[maxn];
ll c[maxn][2],dfn[maxn],w[maxn],st[maxn],ed[maxn],dep[maxn],cnt,n,m;
inline ll lowbit(ll x){return x&-x;}
inline void update(ll id,ll i,ll val){while(i<=n){c[i][id]+=val;i+=lowbit(i);}}
inline ll query(ll id,ll i){ll res=0;while(i){res+=c[i][id];i-=lowbit(i);}return res;}
inline void dfs(ll u,ll fa)
{
    st[u]=++cnt;
    for(std::vector<ll>::iterator v=G[u].begin();v!=G[u].end();v++)
    {
        if(*v==fa)continue;
        dep[*v]=dep[u]+1;
        dfs(*v,u);
    }
    ed[u]=cnt;
}
int main()
{
    scanf("%lld%lld",&n,&m);
    for(ll i=1;i<=n;i++)scanf("%lld",&w[i]);
    for(ll i=1;i<n;i++)
    {
        ll u,v;scanf("%lld%lld",&u,&v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dfs(1,0);
    for(ll i=1;i<=n;i++)update(1,st[i],w[i]),update(1,ed[i]+1,-w[i]);
    while(m--)
    {
        ll op,x,y;scanf("%lld%lld",&op,&x);
        if(op==1)
        {
            scanf("%lld",&y);update(1,st[x],y);update(1,ed[x]+1,-y);
        }
        if(op==2)
        {
            scanf("%lld",&y);
            update(0,st[x],y);update(1,st[x],-dep[x]*y+y);
            update(0,ed[x]+1,-y);update(1,ed[x]+1,dep[x]*y-y);
        }
        if(op==3) printf("%lld\n",query(0,st[x])*dep[x]+query(1,st[x]));
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值