Made a PDF with Office--
This is a one-dimensional case, if it is two-dimensional, you can think of each row of one-dimensional tree array as a node, and then the two-dimensional tree array as a one-dimensional tree array.
Good article:https://www.topcoder.com/community/data-science/data-science-tutorials/binary-indexed-trees/#prob
Two introductory questions: http://acm.hdu.edu.cn/showproblem.php?pid=1556
http://poj.org/problem?id=2155
For the first question HDU1556:
Test instructions: The initial array element value is 0, given the interval [x, y], the interval [x, y] is the value of each element +1, and the value of each element of the entire array is finally output.
Update interval [x, y], you can only update two points x,y+1 (will all rule x of the interval +1, all the jurisdictional y+1 1)
Query the point P, add the value of the range of P-jurisdictional to the line.
Code:
#include <iostream> #include <cstdio> #include <cstring>using namespace Std;int a[100005],n;int Lowbit (int x) { return x& (-X);} void update (int x,int v) { while (x && x<=n) { a[x]+=v; X+=lowbit (x); }} int query (int x) { int ret=0; while (x>0) { ret+=a[x]; X-=lowbit (x); } return ret;} int main () { int i,j,x,y; while (scanf ("%d", &n), N) { memset (a,0,sizeof (a[0]) * (n+2)); for (i=1;i<=n;i++) { scanf ("%d%d", &x,&y); Update (x,1); Update (Y+1,-1); } for (i=1;i<n;i++) printf ("%d", query (i)); printf ("%d\n", query (n)); } return 0;}
the problem is not to update the edge query, so you can do it in a simpler way. Because it is the update of the query, you can be updated each time the two endpoints marked, left and then unified update again on the line.
Code:
#include <iostream> #include <cstring> #include <cstdio>using namespace Std;int s[100005]; int main () { int n,x,y,i; while (scanf ("%d", &n), N) { memset (s,0,sizeof (s)); for (i=1;i<=n;i++) { scanf ("%d%d", &x,&y); s[x-1]--; s[y]++; } for (i=n-1;i>=1;i--) s[i]+=s[i+1]; for (i=1;i<n;i++) printf ("%d", s[i]); printf ("%d\n", S[n]); } return 0;}
for the second question POJ2155:
Test instructions: Given a matrix of n*n, the initial value of each element on the matrix is 0, there are two operations ① give a sub-matrix (the coordinates of the upper-left and lower-right corners), and the value of each element of the sub-matrix ② Query the value of an element
Since the answer for each query is only 0 and 1, you can count how many times each element has been modified, and if it is odd several times then the answer is 1, otherwise 0. Modifications and queries almost the same as the above, just one more dimension.
Code:
#include <iostream> #include <cstdio>using namespace Std;int n,a[1006][1006];void Clear () {for (int i=0;i <=n+1;i++) for (int j=0;j<=n+1;j++) a[i][j]=0;} int lowbit (int x) {return x& (-X);} void update (int x,int Y,int v) {for (Int. i=x;i<=n;i+=lowbit (i)) for (int j=y;j<=n;j+=lowbit (j)) A[i][j]+=v;} int query (int x,int y) {int ret=0;for (int i=x;i>0;i-=lowbit (i)) for (int j=y;j>0;j-=lowbit (j)) Ret+=a[i][j];return RET;} int main () {int Ncase,q,x1,x2,y1,y2;char str[2];scanf ("%d", &ncase), for (int i=1;i<=ncase;i++) {scanf ("%d%d", &N,&Q); Clear (); while (q--) {scanf ("%s", str), if (str[0]== ' C ') {scanf ("%d%d%d%d", &x1,&y1,&x2,&y2); Update (x1 , y1,1); update (x2+1,y1,-1); update (x1,y2+1,-1); update (x2+1,y2+1,1);} ELSE{SCANF ("%d%d", &x1,&y1);p rintf ("%d\n", query (x1,y1)%2);}} printf ("\ n");} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Getting started with a tree array