zoukankan      html  css  js  c++  java
  • 树状数组维护子树和

    题目链接

    Problem

    (已知有 n 个节点,有 n−1 条边,形成一个树的结构)

    (给定一个根节点 k,每个节点都有一个权值,节点i的权值为 vi)

    (给 m 个操作,操作有两种类型:)

    (1space aspace x :表示将节点 a 的权值加上 x)

    (2space a :表示求 a 节点的子树上所有节点的和(包括 a 节点本身))

    Solution

    (有这样一个结论:某节点和其所有子树结点的时间戳dfn是连续的)

    (我们用一个dfn时间戳,在进入时为 time1,遍历完所有子树结点之后为 time2)

    (那么该节点与其所有子树结点的范围为一个连续的time1-time2,这个时候再用树状数组维护即可)

    (即通过dfn时间戳(dfs序),将树形转化为连续线形段)

    #include<bits/stdc++.h>
    #define IOS ios::sync_with_stdio(0); cin.tie(0);
    using namespace std;
    typedef long long ll;
    const int maxn = 1e6+10;
    ll val[maxn];
    ll valu[maxn];
    vector<int>E[maxn];
    int dfn[maxn],ed[maxn];
    int cnt;
    int n,m,k;
    int lowbit(int x){
        return x&-x;
    }
    void add(int x,ll w){
        while(x<=n){
            val[x] += w;
            x += lowbit(x);
        }
    }
    ll query(int x){
        ll res = 0;
        while(x){
            res += val[x];
            x -= lowbit(x);
        }
        return res;
    }
    void dfs(int p,int fa){
        dfn[p] = cnt++; add(dfn[p],valu[p]);
        for(int i=0;i<(int)E[p].size();i++){
            int v = E[p][i];
            if(v!=fa){
                dfs(v,p);
            }
        }
        ed[p] = cnt-1;
    }
    int main(){
        IOS
        cin>>n>>m>>k;
        for(int i=1;i<=n;i++) cin>>valu[i];
        for(int i=1;i<n;i++){
            int u,v;
            cin>>u>>v;
            E[u].push_back(v);
            E[v].push_back(u);
        }
        cnt = 1;
        dfs(k,0);
        for(int i=1;i<=m;i++){
            int op,p;
            cin>>op>>p;
            if(op==1){
                ll w;
                cin>>w;
                add(dfn[p],w);
            }else{
                cout<<query(ed[p])-query(dfn[p]-1)<<endl;
            }
        }
        return 0;
    }
    
  • 相关阅读:
    Valid Number
    ZigZag Conversion
    KMP
    [OJ#40]后宫佳丽
    [OJ#39]左手右手
    [COJ0968]WZJ的数据结构(负三十二)
    [COJ0970]WZJ的数据结构(负三十)
    [BZOJ2815][ZJOI2012]灾难
    [BZOJ1923][Sdoi2010]外星千足虫
    [BZOJ4034][HAOI2015]树上操作
  • 原文地址:https://www.cnblogs.com/Tianwell/p/12733086.html
Copyright © 2011-2022 走看看