Codeforces Round #225 (Div. 2) E. Propagating tree

本文介绍了一种名为传播树的数据结构及其应用。传播树是一种特殊的树形结构,当更新某个节点时,其所有子节点都会受到影响。文章详细描述了如何通过深度优先搜索(DFS)建立两棵树来高效处理这类树上的查询操作,包括节点值的增加和查询当前节点值。
E. 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 a1a2, ..., 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.

Sample test(s)
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].


按dfs序建树,建俩棵,一棵处理奇数层的节点,一棵处理偶数层的。

/*
 *=====================
 *File Name:a.cpp
 *Author: qqspeed
 *Date: 2014年 07月 17日 星期四 14:58:11 CST
 *=====================
 */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

typedef long long ll;
#define rep(i,s,t) for(int i=s;i<t;i++)
#define red(i,s,t) for(int i=s-1;i>=t;i--)
#define ree(i,now) for(int i=head[now];i!=-1;i=edge[i].next)
#define clr(a,v) memset(a,v,sizeof a)
#define L t<<1
#define R t<<1|1
#define MID int mid=(l+r)>>1
#define max(a,b) (a<b?b:a)
//#define min(a,b) (a<b?a:b)
#define SQR(a) ((a)*(a))

inline int input(){
	int ret=0;bool isN=0;char c=getchar();
	while(c<'0' || c>'9'){
		if(c=='-') isN=1;
		c=getchar();
	}
	while(c>='0' && c<='9'){
		ret=ret*10+c-'0';
		c=getchar();
	}
	return isN?-ret:ret;
}

inline void output(int x){    
    if(x<0){    
        putchar('-');x=-x;    
    }    
    int len=0,data[11];    
    while(x){    
        data[len++]=x%10;x/=10;    
    }    
    if(!len)    data[len++]=0;    
    while(len--)   
        putchar(data[len]+48);    
    putchar('\n');  
}  


const int MAXN=200005;
int to[MAXN],rto[MAXN],last[MAXN],dep[MAXN],cnt;
int n,m,op,x,y;
int val[2][MAXN<<2],lazy[2][MAXN<<2],num[2][MAXN<<2];
int a[MAXN];

struct EDGE{
	int v,next;
}edge[MAXN<<1];
int head[MAXN],e;

inline void addEdge(int u,int v){
	edge[e].v=v;
	edge[e].next=head[u];
	head[u]=e++;
}

inline void dfs(int now,int pre){
	to[now]=(++cnt);
	rto[cnt]=now;
	dep[now]=dep[pre]+1;
	ree(i,now){
		int v=edge[i].v;
		if(v!=pre) dfs(v,now);
	}
	last[now]=cnt;
}

inline void push_up(int t,int kind){
	val[kind][t]=val[kind][L]+val[kind][R];
}

inline void push_down(int t,int kind){
	if(lazy[kind][t]){
		val[kind][L]+=lazy[kind][t]*num[kind][L];
		val[kind][R]+=lazy[kind][t]*num[kind][R];
		lazy[kind][L]+=lazy[kind][t];
		lazy[kind][R]+=lazy[kind][t];
		lazy[kind][t]=0;
	}
}

inline void build(int t,int l,int r,int kind){
	lazy[kind][t]=0;
	if(l==r){
		if(dep[rto[l]]%2 == kind){
			num[kind][t]=1;
			val[kind][t]=a[rto[l]];
		}
		else num[kind][t]=val[kind][t]=0;
	}	
	else{
		MID;
		build(L,l,mid,kind);
		build(R,mid+1,r,kind);
		push_up(t,kind);
		num[kind][t]=num[kind][L]+num[kind][R];
	}
}

inline void add(int t,int l,int r,int x,int y,int kind,int v){
	if(l>=x && r<=y){
		lazy[kind][t]+=v;
		val[kind][t]+=v*num[kind][t];
	}
	else{
		push_down(t,kind);
		MID;
		if(y<=mid) add(L,l,mid,x,y,kind,v);
		else if(x>mid) add(R,mid+1,r,x,y,kind,v);
		else{
			add(L,l,mid,x,mid,kind,v);
			add(R,mid+1,r,mid+1,y,kind,v);
		}
		push_up(t,kind);
	}
}

inline int query(int t,int l,int r,int x,int kind){
	if(l==r) return val[kind][t];
	push_down(t,kind);
	MID;
	if(x<=mid) return query(L,l,mid,x,kind);
	else return query(R,mid+1,r,x,kind);
}

int main(){
	n=input(),m=input();
	rep(i,1,n+1) a[i]=input();
	clr(head,-1),e=0;
	rep(i,1,n){
		x=input(),y=input();
		addEdge(x,y),addEdge(y,x);
	}
	dep[0]=cnt=0;dfs(1,0);
	build(1,1,n,0);
	build(1,1,n,1);	
	while(m--){
		op=input();
		if(op==1){
			x=input(),y=input();
			if(dep[x]%2){
				add(1,1,cnt,to[x],last[x],1,y);
				add(1,1,cnt,to[x],last[x],0,-y);
			}
			else{
				add(1,1,cnt,to[x],last[x],1,-y);
				add(1,1,cnt,to[x],last[x],0,y);
			}
		}		
		else{
			x=input();
			output(query(1,1,cnt,to[x],0)+query(1,1,cnt,to[x],1));
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值