http://www.elijahqi.win/archives/645
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 nnodes 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)
题意:
给一棵树
给出两种操作:
1.在某个结点上加上一个值,在这个结点所有的儿子结点上减去这个值,在这个结点的所有孙子结点上加上这个值,在所有曾孙子结点上减去这个值,直到底。
2.查询某个结点上的值
分析:
把这个问题转化为树状数组的区间求和
样例经过dfs处理后如下,每个结点处理出了两个值l,r,层数1,2,3…,层数为奇数的属性为0,层数为偶数的属性为1
#include<cstdio>
#include<cstring>
#define N1 220000
int num;
struct node {
int l,r;
};
struct node1{
int x,y,next;
};
node a[N1];
int h[N1],l[N1],n,m,d[N1],f[2][N1],q;
node1 data[2*N1];
void dfs(int x,int fa,int y){
d[x]=y;
a[x].l=++num;
for (int i=h[x];i;i=data[i].next){
int y1=data[i].y;
if (y1==fa) continue;
dfs(y1,x,1-y);
}
a[x].r=num;
}
void add(int x,int z,int d){
while (x<=n){
f[d][x]+=z;
x+=x&(-x);
}
}
int check1(int x){
int sum=0;
while (x>0){
sum+=f[0][x];x-=x&(-x);
}
return sum;
}
int check2(int x){
int sum=0;
while (x>0){
sum+=f[1][x];x-=x&(-x);
}
return sum;
}
int main(){
freopen("cf383c.in","r",stdin);
freopen("cf383c.out","w",stdout);
scanf("%d%d",&n,&m);
for (int i=1;i<=n;++i) scanf("%d",&l[i]);
memset(h,0,sizeof(h));
num=0;
int x,y;
for (int i=1;i<n;++i){
scanf("%d%d",&x,&y);
data[++num].x=x;
data[num].y=y;
data[num].next=h[x];
h[x]=num;
data[++num].y=x;
data[num].x=y;
data[num].next=h[y];
h[y]=num;
}
num=0;
dfs(1,0,0);
// for (int i=1;i<=n;++i) printf("%d ",d[i]);
// printf("\n");
// for (int i=1;i<=n;++i) printf("%d %d\n",a[i].l,a[i].r);
for (int i=1;i<=m;++i){
scanf("%d",&q);
if (q==1){
scanf("%d%d",&x,&y);
add(a[x].l,y,d[x]);
add(a[x].r+1,-y,d[x]);
continue;
}
scanf("%d",&x);
int tmp1=check1(a[x].l);
//printf("%d ",tmp1);
int tmp2=check2(a[x].l);
//printf("%d ",tmp2);
//printf("\n");
//if (d[x]==0) printf("%d\n",l[x]+tmp1-tmp2);else printf("%d\n",l[x]+tmp2-tmp1);
}
//for (int i=1;i<=n;++i) printf("%d ",f[1][i]);
return 0;
}
//读入数据 dfs1 求出每个节点深度
// dfs2深搜序列
// dfs3深搜出每个节点操作区间
// 读入操作 根据dfs3结果处理操作 利用树状数组做查询
#include<cstdio>
int num;
void dfs1(int x,int fa,int y){
d[x]=y;
for (j=h[x];j;j=data[j].next){
int y1=data[j].y;
if (y1==fa) continue;
dfs1(y1,x,1-y);
}
}
void dfs2(int x,int fa,int y){
if (d[x]==y){
b[x]=++num;
}
for (int j=h[x];j;j=data[j].next){
int y1=data[j].y;
if (y1==fa) continue;
dfs2(y1,x,y);
}
}
void dfs3(int x,int fa){
c[x].a1=b[x];c[x].a2=b[x];c[x].b1=n+1;c[x].b2=0;
for (j=h[x];j;j=data[j].next){
int y1=data[j].y;
if (y1==fa) continue;
dfs3(y1,x);
}
//a2是所有孩子b2最大值
//c[x].b1 是所有孩子a1最小值
//c[x].b2 是所有孩子a2 最大值
}
int main(){
freopen("cf383.in","r",stdin);
freopen("cf383.out","w",stdout);
scanf("%d%d",&n,&m);
for (int i=1;i<=n;++i) scanf("%d",&l[i]);
memset(h,0,sizeof(h));
num=0;
int x,y;
for (int i=1;i<n;++i){
scanf("%d%d",&x,&y);
data[++num].x=x;
data[num].y=y;
data[num].next=h[x];
h[x]=num;
data[++num].y=x;
data[num].x=y;
data[num].next=h[y];
h[y]=num;
}
dfs1(1,-1,0);
//输出d
num=0;
dfs2(data[j].y,1,1);
dfs2(data[j].y,1,0);
//输出b数组
dfs3(1,-1);
//for 循环 1~m次操作
for (int i=1;i<=m;++i){
if (z==1){
scanf("%d%d",&x,&y);
add(c[x].a1,y);
add(c[x].a2+1,-y);
add(c[x].b1,-y);
add(c[x].b2+1,y);
}else{
scanf("%d",&x);
ans=get1(x);
printf("%d\n",ans+l[x]);
}
}
return 0;
}