A tree array (bit) is a data structure with query and modify complexity of log (n), which is mainly used to query the sum of all the elements between any two bits, and its programming is simple and easy to implement. And it can be easily extended to two dimensions. Let's look at a very bare two-dimensional tree-like array problem:
In a "Hit mole" game, the mole will occasionally drill out of the hole, but not from the hole into the (mole really bold ...) )。 The openings are in a square of size n (n<=1024). This square is in a planar Cartesian coordinate system, the lower left corner is (0,0), and the upper-right corner is (n-1,n-1). The location of the hole is the whole point, that is, the horizontal ordinate is an integer. And Superbrother will occasionally want to know the total number of moles in a certain range. This is your mission.
There are multiple lines for each input file.
The first line, a number n, represents the mole's range.
Each subsequent line has a number m at the beginning, indicating a different operation:
M=1, then followed by 3 number x,y,k (0<=x,y<n), indicating a new K mole at the point (x, y);
M=2, then followed by 4 number x1,y1,x2,y2 (0<=x1<=x2<n,0<=y1<=y2<n), indicating the number of moles in the inquiry rectangle (x1,y1)-(X2,Y2);
M=3, said the teacher came, can not play. Make sure the number is in the last line of the input.
The number of inquiries will not exceed 10000, and the number of moles will not exceed maxlongint.
Simply abstract the question. It is:
- Enter 1 o'clock to add a number k to a position in a two-dimensional matrix.
- Enter 2 o'clock to find the total size and output of the element in the sub-matrix of [(x1,y1);(X2,y2)] in the matrix.
- Enter 3 o'clock to exit.
This problem, the input data size may be very large (although in fact not so, the author lazy to randomly generate data). and is dynamic modification, obviously cannot use prefix and, so, the tree array is our first choice. However, the tree-like array would have been one-dimensional, how to extend it to two-dimensional. In fact, it is very simple, the method is similar to the one-dimensional tree array of the original array, and then the corresponding relationship between one-dimensional tree array is composed of two dimensions:
C[1][1]=a[1][1],c[1][2]=a[1][1]+a[1][2],c[1][3]=a[1][3],c[1][4]=a[1][1]+a[1][2]+a[1][3]+a[1][4],c[1][5]=a[1][5 ],C[1][6]=A[1][5]+A[1][6],...
C[2][1]=a[1][1]+a[2][1],c[2][2]=a[1][1]+a[1][2]+a[2][1]+a[2][2],c[2][3]=a[1][3]+a[2][3],c[2][4]=a[1][1]+a[1][2 ]+A[1][3]+A[1][4]+A[2][1]+A[2][2]+A[2][3]+A[2][4], c[2][5]=a[1][5]+a[2][5],c[2][6]=a[1][5]+a[1][6]+a[2][5]+a[2] [6],...
C[3][1]=a[3][1],c[3][2]=a[3][1]+a[3][2],c[3][3]=a[3][3],c[3][4]=a[3][1]+a[3][2]+a[3][3]+a[3][4],c[3][5]=a[3][5 ],C[3][6]=A[3][5]+A[3][6],...
C[4][1]=a[1][1]+a[2][1]+a[3][1]+a[4][1],c[4][2]=a[1][1]+a[1][2]+a[2][1]+a[2][2]+a[3][1]+a[3][2]+a[4][1]+a[4][2 ],C[4][3]=A[1][3]+A[2][3]+A[3][3]+A[4][3],... (Too much, I'll write to 3.)
......
By observing it can be found that the first line is itself, the second line is the first line plus its own, the third row is itself, the fourth line is the first to second line plus itself. This is the same as a one-dimensional tree array. Therefore, it is easy for us to write changes and query functions.
void modfily (int x,int y,int data) { x+=1;y+=1; for (int i=x;i<=n;i+=lowbit (i)) for (int j=y;j<=n;j+=lowbit (j)) C[i][j]+=data;} int sum (int x,int y) { x+=1;y+=1; int result=0; For (Int. i=x;i>0;i-=lowbit (i)) for (int j=y;j>0;j-=lowbit (j)) Result+=c[i][j]; return result;}
This is the core of the two-dimensional tree array, the code and one-dimensional similar, exceptionally simple. You may have questions about why I,j is going to add 1. In fact, this is my understanding after the pit, if i,j=0, Lowbit will always be 0, the program will fall into the dead loop, direct tle. We can also see from these two functions that the query and modification time complexity of the two-dimensional tree array is log (n) ².
Data structure part solved, how to find the number of a sub-matrix and it? Drawing a picture we can find the formula: sum (x2, y2)-sum (x1-1, y2)-sum (x2, y1-1) + sum (x1-1, y1-1)
By the way, the AC code for this example is attached:
#include <cstdio> #include <cstdlib>using namespace Std;int m=0,n,x,y,k,x1,y1,c[1026][1026];void modfily ( int,int,int); int sum (int,int); inline int lowbit (int x) {return x& (-X);} int main (void) {scanf ("%d", &n); for (int i=0;i<n;++i) for (int j=0;j<n;++j) c[i][j]=0; while (m!=3) {scanf ("%d", &m); if (m==1) {scanf ("%d%d%d", &x,&y,&k); Modfily (X,Y,K); } if (m==2) {scanf ("%d%d%d%d", &x,&y,&x1,&y1); printf ("%d\n", ABS (SUM (x1,y1)-sum (x-1,y1)-sum (x1,y-1) +sum (x-1,y-1))); }} return 0;} void modfily (int x,int y,int data) {x+=1;y+=1; for (int i=x;i<=n;i+=lowbit (i)) for (int j=y;j<=n;j+=lowbit (j)) C[i][j]+=data;} int sum (int x,int y) {x+=1;y+=1; int result=0; For (Int. i=x;i>0;i-=lowbit (i)) for (int j=y;j>0;j-=lowbit (j)) Result+=c[i][j]; return result;}
Two-dimensional tree-like array--superbrother hit Mole (Vijos1512)