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:

  • "1 x val" — val is added to the value of node x;
  • "2 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<<]; struct TreeNodeType {
int l,r,dis,mid,flag;
};
struct TreeNodeType tree[maxn<<]; 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=,if_z=,Cget=getchar();
while(Cget>''||Cget<'')
{
if(Cget=='-') if_z=-;
Cget=getchar();
}
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
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]*=-;
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)>>;
tree_build(now<<,l,tree[now].mid);
tree_build(now<<|,tree[now].mid+,r);
} void tree_do(int now,int l,int r)
{
if(tree[now].l==l&&tree[now].r==r)
{
if(type==)
{
tree[now].flag+=x;
if(l==r) tree[now].dis+=x;
}
else ans+=tree[now].dis;
return ;
}
if(tree[now].flag)
{
tree[now<<].flag+=tree[now].flag;
tree[now<<|].flag+=tree[now].flag;
if(tree[now<<].l==tree[now<<].r) tree[now<<].dis+=tree[now].flag;
if(tree[now<<|].l==tree[now<<|].r) tree[now<<|].dis+=tree[now].flag;
tree[now].flag=;
}
if(l>tree[now].mid) tree_do(now<<|,l,r);
else if(r<=tree[now].mid) tree_do(now<<,l,r);
else
{
tree_do(now<<,l,tree[now].mid);
tree_do(now<<|,tree[now].mid+,r);
}
} int main()
{
in(n),in(m);int u,v;
for(int i=;i<=n;i++) in(dis[i]);
for(int i=;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=,search_1(,);
tree_build(,,n);
while(m--)
{
in(type),in(u);
if(type==)
{
in(x);
if(!deep[u]) x*=-;
tree_do(,li[u],ri[u]);
}
else
{
ans=;
tree_do(,li[u],li[u]);
if(!deep[u]) ans*=-;
printf("%d\n",ans);
}
}
return ;
}

AC日记——Propagating tree Codeforces 383c的更多相关文章

  1. AC日记——825G - Tree Queries

    825G - Tree Queries 思路: 神题,路径拆成半链: 代码: #include <cstdio> #include <cstring> #include < ...

  2. AC日记——Cards Sorting codeforces 830B

    Cards Sorting 思路: 线段树: 代码: #include <cstdio> #include <cstring> #include <iostream> ...

  3. AC日记——Card Game codeforces 808f

    F - Card Game 思路: 题意: 有n张卡片,每张卡片三个值,pi,ci,li: 要求选出几张卡片使得pi之和大于等于给定值: 同时,任意两两ci之和不得为素数: 求选出的li的最小值,如果 ...

  4. AC日记——Success Rate codeforces 807c

    Success Rate 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> ...

  5. AC日记——T-Shirt Hunt codeforces 807b

    T-Shirt Hunt 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> ...

  6. AC日记——Magazine Ad codeforces 803d

    803D - Magazine Ad 思路: 二分答案+贪心: 代码: #include <cstdio> #include <cstring> #include <io ...

  7. AC日记——Broken BST codeforces 797d

    D - Broken BST 思路: 二叉搜索树: 它时间很优是因为每次都能把区间缩减为原来的一半: 所以,我们每次都缩减权值区间. 然后判断dis[now]是否在区间中: 代码: #include ...

  8. AC日记——Array Queries codeforces 797e

    797E - Array Queries 思路: 分段处理: 当k小于根号n时记忆化搜索: 否则暴力: 来,上代码: #include <cmath> #include <cstdi ...

  9. AC日记——Maximal GCD codeforces 803c

    803C - Maximal GCD 思路: 最大的公约数是n的因数: 然后看范围k<=10^10; 单是答案都会超时: 但是,仔细读题会发现,n必须不小于k*(k+1)/2: 所以,当k不小于 ...

随机推荐

  1. 转 消息队列之 RabbitMQ

    转 https://www.jianshu.com/p/79ca08116d57 消息队列之 RabbitMQ 预流 2017.05.06 16:03* 字数 4884 阅读 80990评论 18喜欢 ...

  2. 02等待单个线程返回WaitForSingleObject

    windows 多线程之等待线程返回 ​ 多线程编程中,有时我们需要等待某一线程完成了特定的操作之后再继续做其他事情,要实现这个目的,可以使用 Windows API 函数 WaitForSingle ...

  3. nginx基本安全优化

    一.调整参数隐藏nginx软件版本号信息 查看nginx版本信息: [root@nginx conf]# curl -I 192.168.200.102 HTTP/1.1 200 OK Server: ...

  4. 移动端H5前端性能优化指南:

    分享地址:https://isux.tencent.com/h5-performance.html

  5. Python: simple drawings

    import cv2; # OpenCV Python import numbers; import numpy as np; import math; import matplotlib; impo ...

  6. redis的字符串操作以及在django中的使用

    redis ----redis.MongoDB : 非关系型数据库 redis   存储在内存中 MongoDB 存储在硬盘中 l  简介 redis是一个key-value存储系统 , 支持持久化 ...

  7. 中国电信物联网平台入门学习笔记7:NB-IOT信号如何检测

    NB-IOT设备会因为信号的原因,数据发不出.但数据发不出的原因有很多,这么排除是NB-IOT信号的问题呢?那就需要NB-IOT信号检测装置. 网上的信号检测设备 作为一个常年蜗居在实验室的穷屌丝而言 ...

  8. UVa 1407 树形背包 Caves

    这道题可以和POJ 2486 树形背包DP Apple Tree比较着来做. 参考题解 #include <iostream> #include <cstdio> #inclu ...

  9. luogu4173 残缺的字符串

    there #include <algorithm> #include <iostream> #include <cstring> #include <cst ...

  10. selenium2.53用45以下的火狐别太高

    selenium2.53用45以下的火狐别太高在高的火狐需要selenium3