C. Propagating tree
Time Limit:20 Sec
Memory limit:256 MB
Topic Connection
Http://codeforces.com/contest/383/problem/C
Descriptioniahub likes trees very much. Recently he discovered an interesting tree named propagating tree. The tree consists of n nodes numbered from 1 to n, and each node I had an initial value AI. The root of the tree is node 1.
This tree has a special Property:when a value Val was added to a value of node I, the value-val are 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 T He child of node I and so on. Look a example explanation to understand better how it works.
This tree supports the 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 the 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 a integers vi and UI (1≤vi, ui≤n), meaning that there was an edge between nodes V I and UI.
Each of the next m lines contains a query in the format described above. It is guaranteed, the following constraints, and all Queries:1≤x≤n, 1≤val≤1000.
Output
For each query of type B (print the Value of node X) You must print the answer to the query on a separate line. The queries must is answered in the order given in the input.
Sample 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
Sample Output
3
3
0
HINT
Test instructions
Give a tree with N nodes and 11 as the root node, each node has its weight, now do m operations, operations are divided into Add and query, when a node's weight added Val, then its child node weights to add-B.
Exercises
DFS sequence + Tree array
Divided into two trees to do
http://blog.csdn.net/keshuai19940722/article/details/18967661
Code
//Qscqesze#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>#include<map>#include<stack>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineMAXN 2000001#defineMoD 1000000007#defineEPS 1e-9intNum;Charch[ -];Const intinf=0x3f3f3f3f; inline ll read () {intx=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}//**************************************************************************************structnode{intL,r,v,d;} NODE[MAXN];intN,m;vector<int>E[MAXN];intbit[2][MAXN];intCNT;voidAddintXintValint*b) { while(x<=n*2) {B[x]+=Val; X+ = (x& (-x)); }}int Get(intXint*b) { intans=0; while(x>0) {ans+=B[x]; X-= (x& (-x)); } returnans;}voidDfsintXintFaintd) {NODE[X].L=cnt++; NODE[X].D=D; for(intI=0; I<e[x].size (); i++) { if(e[x][i]==FA)Continue; DFS (E[x][i],x,1-d); } NODE[X].R=cnt++;}intMain () {n=read (), m=read (); for(intI=1; i<=n;i++) NODE[I].V=read (); for(intI=1; i<n;i++) { intA=read (), b=read (); E[a].push_back (b); E[b].push_back (a); } CNT=1; DFS (1,-1,0); for(intI=0; i<m;i++) { intop=read (); if(op==1) { intA=read (), b=read (); Add (NODE[A].L,B,BIT[NODE[A].D]); Add (NODE[A].R+1,-B,BIT[NODE[A].D]); } Else { intA=read (); printf ("%d\n", node[a].v+Get(NODE[A].L,BIT[NODE[A].D])-Get(node[a].l,bit[1-NODE[A].D])); } }}
Codeforces Round #225 (Div. 1) C. Propagating tree Dfs sequence + tree array