Tree[n] = a[n-2^k+1] + ... + a[n]
Single-point update, interval evaluation: The tree array represents the and of the interval.
Const intMAXN =100010;intN,TREE[MAXN];intLowbit (inti) {returnI & (i);}voidUpdate (intIintx) { while(I <= N) {Tree[i] = Tree[i] + x; i + = Lowbit (i); }}intQuery (intN) {intsum =0; while(N >0) {sum + = Tree[n]; N-= Lowbit (n); }returnsum;}//Initializememset(Tree,0,sizeof(Tree));scanf("%d", &n); for(inti =1; I <= N; ++i) {intXscanf("%d", &x); Update (i,x);} Single point update, the value of point I is added to the v:update (i,v) interval, and the interval (U,V) is evaluated, and: Query (v)-Query (U-1)
Interval update, single-point evaluation: A tree array represents the change of a single element
ConstintMAXN =100010;intN,TREE[MAXN];intLowbit (inti) {returnI & (i); }voidUpdata (intIintx) { while(I <= N) {Tree[i] = Tree[i] + x; i = i + lowbit (i); } }intQuery (intN) {int sum=0; while(N >0) {sum+ = Tree[n]; n = n-lowbit (n); }return sum; } interval update, increase the element of the interval (u,v) to D:update (u,d), update (v+1,-D) Single-point evaluation to find the value of element I: Query (i)
To find the number of reverse order:
Array Tree[i] Indicates whether the number I has occurred in the sequence, if the number I already exists in the sequence, tree[i] = 1, otherwise tree[i] = 0. A sequence of elements with a value of a as subscript a and an assignment of 1 into a tree array, in order from left to right, is a i-query (a). Summing up all the results is the number of reverse order.
Const intMAXN =100010;intN,TREE[MAXN];intLowbit (inti) {returnI & (i);}voidUpdate (intIintx) { while(I <= N) {Tree[i] = Tree[i] + x; i = i + lowbit (i); }}intQuery (intN) {intsum =0; while(N >0) {sum + = Tree[n]; n = n-lowbit (n); }returnsum;}//reverse order Number:memset(Tree,0,sizeof(Tree)); for(inti =1; I <= N; ++i) {Cin>> A; Update (A,1); Ans + = i-query (a);}
Two-dimensional tree-like array:
ConstintMAXN =1010;intTree[maxn][maxn],n; BOOL MARK[MAXN][MAXN];intLowbit (inti) {returnI & (i); }voidUpdate (intXintYintNUM) { for(inti = x; I <= MAXN; i + = Lowbit (i)) for(intj = y; J <= Maxn; J + = Lowbit (j)) Tree[i][j] + = num; }intQuery (intXintY) {int sum=0; for(inti = x; i >0; I-= Lowbit (i)) for(intj = y; J >0; J-= Lowbit (j))sum+ = Tree[i][j];return sum; The single-point update adds D:update (x,y,d) to the elements on the coordinates (x, y), the interval evaluation, the upper-left corner (X2,Y2) to the lower-right corner (X1,y2), and: Query (X1,Y1)-Query (x1,y2-1)-Query (x2-1, y1) + Query (x2-1, y2-1)
Tree-like array "Templates"