Codeforces Round #225 (Div. 1) C tree-like array | | Segment Tree

Source: Internet
Author: User

see this question very happy ah, have the impression and have done before, seems to have done one recently, with time stamp for the interval to build a tree array, and then at the beginning I thought test instructions is, give X point plus Val, it all the nodes below add-val; So the first two tree arrays were built with plus and minus, and finally Minus is the answer, after writing the discovery and the case is not ah, read the topic also did not find read wrong, for that sentence I understand wrong, later read this:
http://blog.csdn.net/keshuai19940722/article/details/18967661
Take a closer look at the processing part, I thought the odd and even sex has the law, and later found that the wrong problem, the original is the X point plus Val, and it is directly connected to the child node plus-val, its child node of the child node plus Val, and so on ... Ha ha.. Cry

Similar to the previous type of topic, for this tree, DFS, while recording the current layer distance with the relationship, with odd even to represent, and then to each node by the DFS timestamp to establish the tree array to map up, the last odd and even separate, add in a tree array, minus in the other inside, and then At the end of the single point of the value of the time is the point of their own relationship to the root node, that is, the value of their own should be added, minus the corresponding other tree should be subtracted from the value, and then the beginning of the node itself has a value not added to the tree array, but also to add the original value, this is the answer

And then with the line tree to do a bit, but also to engage in time stamping, while recording the node distance root of the parity, and then also establish two line segment tree, an odd record processing, a record even processing, the results do not know where to write wrong, and changed for a long time, do not re-write a bit, really learn what forget what ...

Tree-like array:

int N;int M;int c[2][200000 * 2 + 55];typedef struct Node {int l,r,val;int now;}; Node node[200000 + 55];vector<int > g[200000 + 55];int cnt;void init () {memset (c,0,sizeof (c)); for (int i=0;i<2000 xx + 55;i++) g[i].clear ();} bool Input () {while (cin>>n>>m) {for (int i=1;i<=n;i++) Cin>>node[i].val;int tmp = N-1;while (tmp--) {int u,v;scanf ("%d%d", &u,&v); G[u].push_back (v); G[v].push_back (u);} return false;} return true;} int lowbit (int x) {return x& (-X);} void Add (int i,int val,int *AA) {while (I <= 2 * N) {aa[i] + = Val;i + lowbit (i);}} int get_sum (int i,int *aa) {int sum = 0;while (i > 0) {sum + = aa[i];i-= Lowbit (i);} return sum;} void Dfs (int u,int pre,int tot) {node[u].l = Cnt++;node[u].now = tot;for (int i=0;i<g[u].size (); i++) {int v = g[u][i];if (v = = Pre) Continue;dfs (v,u,tot^1);} NODE[U].R = cnt++;} void Cal () {cnt = 1;dfs (1,-1,0), while (m--) {int type;cin>>type;if (type = = 1) {int X,y;cin>>x>>y;//int t MP = Node[x].now;//int AA = node[X].l;//int BB = Node[x].r;add (Node[x].l,y,c[node[x].now]); Add (NODE[X].R + 1,-y,c[node[x].now]);} else {int X;cin>>x;//int AA = (get_sum (node[x].l,c[node[x].d])/*-get_sum (NODE[X].L-1,C[NODE[X].D]) */);//int BB = (Get_sum (node[x].l,c[node[x].d^1])/*-Get_sum (node[x].l-1,c[node[x].d^1]) */);//int cc = 0;int ans = get_sum (node[x]. L,c[node[x].now])-get_sum (node[x].l,c[node[x].now^1]); ans + = Node[x].val;cout<<ans<<endl;}}} void output () {}int main () {while (true) {init (); if (input ()) return 0;cal (); output ();} return 0;}


Segment Tree:

const int N = 200000 + 55;int n;int m;int nnum[n + 55];int le[n + 55],ri[n + 55],belong[n + 55];int Head[n + 55];typedef s truct Node {int l,r;ll sum;int lazy;}; Node Tree_even[n * 4 + 55],tree_odd[n * 4 + 55];typedef struct NODE {int fro,to;int nex;}; NODE edge[2 * N + 55];int tot;int cnt;void Add (int u,int v) {Edge[tot].fro = U;edge[tot].to = V;edge[tot].nex = Head[u];he Ad[u] = tot++;} void Dfs (int u,int pre,int d) {Le[u] = ++cnt;for (int i=head[u];i!=-1;i=edge[i].nex) {int v = edge[i].to;if (v = = Pre) contin Ue;dfs (v,u,d^1);} Belong[le[u]] = d;ri[le[u]] = cnt;} void push_up (int id,node *tree) {tree[id].sum = Tree[id<<1].sum + tree[id<<1|1].sum;} void Push_down (int id,node *tree) {if (Tree[id].lazy = = 0) return; Tree[id].sum + = (TREE[ID].R-TREE[ID].L + 1) * Tree[id]. Lazy;if (TREE[ID].L = = TREE[ID].R) {tree[id].lazy = 0;return;} Tree[id<<1].lazy + = Tree[id].lazy;tree[id<<1|1].lazy + = Tree[id].lazy;tree[id].lazy = 0;} void build (int l,int r,int id,node *tree) {TREE[ID].L = L; TREE[ID].R = R;tree[id].lazy = 0;if (L = = r) {tree[id].sum = 0ll;return;} int mid = (L + R) >>1;build (L,mid,id<<1,tree); Build (mid + 1,r,id<<1|1,tree);p ush_up (id,tree);} void update (int l,int r,int id,ll val,node *tree) {if (TREE[ID].L = = L && TREE[ID].R = r) {Tree[id].lazy + = Val;pu Sh_down (id,tree); return;} Push_down (id,tree); int mid = (tree[id].l + TREE[ID].R) >>1;if (r <= Mid) update (l,r,id<<1,val,tree); else if (L > Mid) Update (L,r,id<<1|1,val,tree), else {update (l,mid,id<<1,val,tree); Update (Mid + 1,r,id< <1|1,val,tree);} PUSH_UP (Id,tree);} ll query (int l,int r,int Id,node *tree) {if (TREE[ID].L = = L && TREE[ID].R = = r) {Push_down (id,tree); return Tree[id ].sum;} Push_down (id,tree); int mid = (tree[id].l + tree[id].r) >>1;ll ret = 0ll;if (R <= mid) ret + = query (l,r,id<<1,t REE), else if (L > Mid) ret + = query (L,r,id<<1|1,tree), else {ret + = query (l,mid,id<<1,tree); ret + = Query (mid + 1,r,id<<1|1,tree);} REturn ret;} void Init () {memset (tree_even,0,sizeof (Tree_even)); Memset (Tree_odd,0,sizeof (tree_odd)); Memset (Head,-1,sizeof ( Head); tot = 1;cnt = 0;} bool Input () {while (cin>>n>>m) {for (int i=1;i<=n;i++) cin>>nnum[i];for (int i=1;i<n;i++) {int U , V;cin>>u>>v;add (u,v); add (v,u);} return false;} return true;} void Cal () {DFS (1,-1,1); build (1,n,1,tree_even); build (1,n,1,tree_odd); while (m--) {int type;cin>>type;if (type = = 1) {int X,y;cin>>x>>y;int left = Le[x];int right = ri[left];if (belong[left]&1) update (Left,right,1,y, tree_odd); else update (Left,right,1,y,tree_even);} else {int X;cin>>x;int left = Le[x];ll ans;if (belong[left]&1) ans = query (left,left,1,tree_odd)-Query (left, Left,1,tree_even); Elseans = Query (left,left,1,tree_even)-Query (left,left,1,tree_odd); ans + = nnum[x];cout<< Ans<<endl;}}} void output () {}int main () {while (true) {init (); if (input ()) return 0;cal (); output ();} return 0;}


Codeforces Round #225 (Div. 1) C tree-like array | | Segment Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.