From: http://blog.csdn.net/svitter
The tree array is used to calculate the value in the interval and change the value of a single element. Note that the tree array starts from array subscript 1.
Basic Algorithms:
Obtain all lowbit values to prevent repeated calculation.
void getLowbit(){ for(int i = 0; i < 1000; i++) lowbit[i] = i & (-i);}
Evaluate the range and 1 ~ I:
int Sum(int i){ int sum = 0; while(i > 0) { sum += C[i]; i = i - lowbit[i]; } return sum;}
Change the I value of an element:
void Change(int i, int inc){while(i <= n){C[i] += inc;i = i + lowbit[i];}}
Cn method:
sum(n) - sum(n - lowbit(n));
Question: poj3321
A. Apple tree contains multiple apples, one branch and one apple, and the number of apples at or above a node is calculated. You can add or delete apple. (If there is an apple, add it. If not, delete it. At the beginning, there were all apples ).
Test data: the first row is N, which indicates the number of branches. Then enter the branch relationship. The second behavior is m, which indicates the number of operations. Then, enter Q or C and add a number, indicating the sum or the tree is deleted or added.
Train of Thought: the sparse graph is stored in an adjacent table. STL _ sparse graph, and tree _ uses a vector adjacent table for storage. Calculate the sum between segments and change only one element. You can use a tree array. Take the root as 1 and the upstream node as the other side. Use deep search to count the number of apples.
C [I] is sum (I)-sum (I-lowbit [I]), that is, I-(I-lowbit [I]); C [time];
Q: Use start [N] and end [N] to store the access time. Use (end [N]-start [N] + 1) /2 obtain the final result (except for the Apple Node Selected, the other Apple went twice ).
C: Modify the apple node by combining the modify of the tree array. The query function does not calculate the value of apple.
#include <iostream>#include <stdio.h>#include <string.h>#include <vector>using namespace std;#define MY_MAX 220000typedef vector<int> VCT_INT;vector <VCT_INT> G(MY_MAX/2);int C[MY_MAX];int lowbit[MY_MAX];bool HasApple[MY_MAX];int start[MY_MAX];int end[MY_MAX];int nCount = 0;void DFS(int v){ start[v] = ++ nCount; for(int i = 0; i < G[v].size(); i++) DFS(G[v][i]); end[v] = ++ nCount;}int QuerySum(int p){ int nSum = 0; while(p > 0) { nSum += C[p]; p -= lowbit[p]; } return nSum;}void Modify(int p, int val){ while(p <= nCount) { C[p] += val; p += lowbit[p]; }}void getLowbit(){ for(int i = 1; i < MY_MAX; i++) lowbit[i] = i & (-i);}int main(){ int n, m; scanf("%d", &n); int x, y, i, j ,k; int a, b; getLowbit(); //build map for(i = 0; i < n - 1; i++) { scanf("%d%d", &a, &b); G[a].push_back(b); } nCount = 0; DFS(1); for(i = 1; i <= n; i++) HasApple[i] = 1; for(i = 1; i <= nCount; i++) C[i] = i - (i - lowbit[i]); scanf("%d", &m); while(m--) { char cmd[10]; scanf("%s%d", cmd, &a); if(cmd[0]== 'C') { if(HasApple[a]) { Modify(start[a], -1); Modify(end[a], -1); HasApple[a] = 0; } else { Modify(start[a], 1); Modify(end[a], 1); HasApple[a] = 1; } } else { int t1 = QuerySum(end[a]); int t2 = QuerySum(start[a]-1); printf("%d\n", (t1-t2)/2); } } return 0;}
Tree array _ poj tree Array