zoukankan      html  css  js  c++  java
  • AC日记——Propagating tree Codeforces 383c

    C. Propagating tree
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Iahub likes trees very much. Recently he discovered an interesting tree named propagating tree. The tree consists of n nodes numbered from 1 to n, each node i having an initial value ai. The root of the tree is node 1.

    This tree has a special property: when a value val is added to a value of node i, the value -val is added to values of all the children of node i. Note that when you add value -val to a child of node i, you also add -(-val) to all children of the child of node i and so on. Look an example explanation to understand better how it works.

    This tree supports two types of queries:

    • "x val" — val is added to the value of node x;
    • "x" — print the current value of node x.

    In order to help Iahub understand the tree better, you must answer m queries of the preceding type.

    Input

    The first line contains two integers n and m (1 ≤ n, m ≤ 200000). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000). Each of the next n–1 lines contains two integers vi and ui (1 ≤ vi, ui ≤ n), meaning that there is an edge between nodes vi and ui.

    Each of the next m lines contains a query in the format described above. It is guaranteed that the following constraints hold for all queries: 1 ≤ x ≤ n, 1 ≤ val ≤ 1000.

    Output

    For each query of type two (print the value of node x) you must print the answer to the query on a separate line. The queries must be answered in the order given in the input.

    Examples
    input
    5 5
    1 2 1 1 2
    1 2
    1 3
    2 4
    2 5
    1 2 3
    1 1 2
    2 1
    2 2
    2 4
    output
    3
    3
    0
    Note

    The values of the nodes are [1, 2, 1, 1, 2] at the beginning.

    Then value 3 is added to node 2. It propagates and value -3 is added to it's sons, node 4 and node 5. Then it cannot propagate any more. So the values of the nodes are [1, 5, 1,  - 2,  - 1].

    Then value 2 is added to node 1. It propagates and value -2 is added to it's sons, node 2 and node 3. From node 2 it propagates again, adding value 2 to it's sons, node 4 and node 5. Node 3 has no sons, so it cannot propagate from there. The values of the nodes are[3, 3,  - 1, 0, 1].

    You can see all the definitions about the tree at the following link: http://en.wikipedia.org/wiki/Tree_(graph_theory)

     

    思路;

      dfs序同时处理深度,然后搞搞就a了;

    来,上代码:

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    #define maxn 200005
    #define LL long long
    
    using namespace std;
    
    struct EdgeType {
        int v,e;
    };
    struct EdgeType edge[maxn<<1];
    
    struct TreeNodeType {
        int l,r,dis,mid,flag;
    };
    struct TreeNodeType tree[maxn<<2];
    
    int if_z,n,m,dis[maxn],head[maxn];
    int cnt,f[maxn],li[maxn],ri[maxn];
    int id[maxn],dis_[maxn],type,x,ans;
    
    bool deep[maxn];
    
    char Cget;
    
    inline void in(int &now)
    {
        now=0,if_z=1,Cget=getchar();
        while(Cget>'9'||Cget<'0')
        {
            if(Cget=='-') if_z=-1;
            Cget=getchar();
        }
        while(Cget>='0'&&Cget<='9')
        {
            now=now*10+Cget-'0';
            Cget=getchar();
        }
        now*=if_z;
    }
    
    void search_1(int now,int fa)
    {
        deep[now]=!deep[fa],f[now]=fa;
        id[now]=++cnt,li[now]=cnt,dis_[cnt]=dis[now];
        if(!deep[now]) dis_[cnt]*=-1;
        for(int i=head[now];i;i=edge[i].e)
        {
            if(edge[i].v==f[now]) continue;
            search_1(edge[i].v,now);
        }
        ri[now]=cnt;
    }
    
    void tree_build(int now,int l,int r)
    {
        tree[now].l=l,tree[now].r=r;
        if(l==r)
        {
            tree[now].dis=dis_[l];
            return ;
        }
        tree[now].mid=(l+r)>>1;
        tree_build(now<<1,l,tree[now].mid);
        tree_build(now<<1|1,tree[now].mid+1,r);
    }
    
    void tree_do(int now,int l,int r)
    {
        if(tree[now].l==l&&tree[now].r==r)
        {
            if(type==1)
            {
                tree[now].flag+=x;
                if(l==r) tree[now].dis+=x;
            }
            else ans+=tree[now].dis;
            return ;
        }
        if(tree[now].flag)
        {
            tree[now<<1].flag+=tree[now].flag;
            tree[now<<1|1].flag+=tree[now].flag;
            if(tree[now<<1].l==tree[now<<1].r) tree[now<<1].dis+=tree[now].flag;
            if(tree[now<<1|1].l==tree[now<<1|1].r) tree[now<<1|1].dis+=tree[now].flag;
            tree[now].flag=0;
        }
        if(l>tree[now].mid) tree_do(now<<1|1,l,r);
        else if(r<=tree[now].mid) tree_do(now<<1,l,r);
        else
        {
            tree_do(now<<1,l,tree[now].mid);
            tree_do(now<<1|1,tree[now].mid+1,r);
        }
    }
    
    int main()
    {
        in(n),in(m);int u,v;
        for(int i=1;i<=n;i++) in(dis[i]);
        for(int i=1;i<n;i++)
        {
            in(u),in(v);
            edge[++cnt].v=v,edge[cnt].e=head[u],head[u]=cnt;
            edge[++cnt].v=u,edge[cnt].e=head[v],head[v]=cnt;
        }
        cnt=0,search_1(1,0);
        tree_build(1,1,n);
        while(m--)
        {
            in(type),in(u);
            if(type==1)
            {
                in(x);
                if(!deep[u]) x*=-1;
                tree_do(1,li[u],ri[u]);
            }
            else
            {
                ans=0;
                tree_do(1,li[u],li[u]);
                if(!deep[u]) ans*=-1;
                printf("%d
    ",ans);
            }
        }
        return 0;
    }
  • 相关阅读:
    python sendmail
    linux find命令
    10块钱去买酒,2块一瓶,2空瓶换一瓶,4盖换一瓶,问能喝到什么时候
    【转】通过经纬度坐标计算距离的方法(经纬度距离计算)
    Android应用开发相关下载资源(2015/3/20更新)
    使用Entity Framework访问MySQL数据库
    VS2013+EF6.1+Mysql配置心路历程
    SQL-LINQ-Lambda语法对照实例
    百度统计今晚更新了,统计的数据报表格式都大变样了,少不了我的功劳吧!跟类似yoyooho.com的小伙伴说再见啦
    跟踪那些像www.yoyooho.com非友链带来的百度统计数据解析【第三篇】
  • 原文地址:https://www.cnblogs.com/IUUUUUUUskyyy/p/6550819.html
Copyright © 2011-2022 走看看