Cf 383c propagating tree [idea + tree array]

Source: Internet
Author: User

Question:

To a tree

Two operations are provided:

1. add a value to a node, subtract the value from all son nodes of the node, and add the value to all son nodes of the node, subtract this value from all the previous child nodes until the bottom.

2. query the value on a node

Analysis:

Converts this problem to the interval summation of the tree array.

The following is an example after DFS processing. Each node processes two values: l, R, Layer 1, 2, 3 ..., the attribute with an odd number of layers is 0, and the attribute with an even number of layers is 1.

We can see that the two values of the node under the subtree are within the two ranges of the root node of the subtree.

The two data ranges of the sibling nodes of the Child root node are not included in the two data ranges of the Child root node.


We can see that only the nodes under the subtree are associated with the root node of the subtree, And the sibling node is not associated. This is consistent with the condition given by the question.

Consider maintaining two tree Arrays

When a node is added, if the node attribute is 0, the operation is performed on the tree array No. 0; otherwise, the operation is performed on the array No. 1.

The operations are as follows:

Add this value to element l of the array and subtract this value from element R of the array.


When you view the value of a node

The value to be added to this node is the sum of the first l items in the tree array of the attribute number, and the sum of the first l items in the tree array of the non-attribute number to be subtracted. Then, the original value is added and the output is


Why?

Because the number of the same attribute is plus, the effect of different attribute numbers is minus

Because the first l items and do not contain the influence of the sibling node, for example, here we need to get the value of Node 3, L [3] = 8, the first 8 items and medium, A positive (element 2) and a negative (element 7) on element 2 have been offset. The influence of the father node and the ancestor node is added, because the L value of Node 3 is between the two ranges of the father node and the ancestor node.

#include <iostream>#include <vector>#include <cstring>#include <cstdio>#include <cmath>#include <algorithm>#include <vector>using namespace std;#define pb push_backconst int NN=222222;int bit_2[2][NN*2+1];int ALL_bit=NN*2;int f[NN];int clsfy[NN];int sum_bit(int i,int bit[]){int s=0;while(i>0){s+=bit[i],i-= i & -i;}return s;}void add_bit(int i,int x,int bit[]){while(i<=ALL_bit){bit[i]+=x,i+= i & -i;}}vector<int> G[NN];int m=1;int l[NN],r[NN];void dfs(int now,int fa,int k){l[now]=m++;clsfy[now]=k;for(int i=0;i<G[now].size();i++){int to=G[now][i];if(to==fa) continue;dfs(to,now,1-k);}r[now]=m++;}int main(){#ifndef ONLINE_JUDGEfreopen("/home/rainto96/in.txt","r",stdin);#endifint n,m;cin>>n>>m;for(int i=1;i<=n;i++)cin>>f[i];for(int i=1;i<=n-1;i++){int x,y;cin>>x>>y;G[x].pb(y);G[y].pb(x);}dfs(1,-1,0);while(m--){int kind;cin>>kind;if(kind==1){int pos,val;cin>>pos>>val;add_bit(l[pos],val,bit_2[clsfy[pos]]);add_bit(r[pos],-val,bit_2[clsfy[pos]]);}else{int pos;cin>>pos;cout<<f[pos]+sum_bit(l[pos],bit_2[clsfy[pos]])-sum_bit(l[pos],bit_2[1-clsfy[pos]])<<endl;}}}


Cf 383c propagating tree [idea + tree array]

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.