Thoroughly understand the two-dimensional tree-like array _ Tree array

Source: Internet
Author: User
Two, the tree-like array can be extended to two-dimensional.
Problem: A large matrix made up of numbers that can perform two kinds of operations
1 to a number in the Matrix plus an integer (can be positive and negative)
2 Query a matrix of all the numbers and requirements for each query, output results.

A one-dimensional tree array can easily be extended to two-dimensional, in two-dimensional cases: array a[][] The tree array is defined as:

C[x][y] =∑a[i][j], of which,
X-lowbit (x) + 1 <= i <= x,
Y-lowbit (y) + 1 <= J <= y.

For example, look at the composition of c[][].
Set the original two-dimensional array as:
A[][]={{a11,a12,a13,a14,a15,a16,a17,a18,a19},
{A21,a22,a23,a24,a25,a26,a27,a28,a29},
{A31,a32,a33,a34,a35,a36,a37,a38,a39},
{a41,a42,a43,a44,a45,a46,a47,a48,a49}};
Then it corresponds to the two-dimensional tree-like array c[][].

Remember:
B[1]={a11,a11+a12,a13,a11+a12+a13+a14,a15,a15+a16,...} This is a one-dimensional tree array of the first row
B[2]={a21,a21+a22,a23,a21+a22+a23+a24,a25,a25+a26,...} This is a one-dimensional, tree-like array of the second row
B[3]={a31,a31+a32,a33,a31+a32+a33+a34,a35,a35+a36,...} This is a one-dimensional, tree-like array of the third row
B[4]={a41,a41+a42,a43,a41+a42+a43+a44,a45,a45+a46,...} This is a one-dimensional, tree-like array of line fourth
So:
C[1][1]=a11,c[1][2]=a11+a12,c[1][3]=a13,c[1][4]=a11+a12+a13+a14,c[1][5]=a15,c[1][6]=a15+a16,...
This is a[][] the first row of the one-dimensional tree array

C[2][1]=a11+a21,c[2][2]=a11+a12+a21+a22,c[2][3]=a13+a23,c[2][4]=a11+a12+a13+a14+a21+a22+a23+a24,
C[2][5]=a15+a25,c[2][6]=a15+a16+a25+a26,...
This is the a[][] array of the first row and the second row after adding the tree-like array

C[3][1]=a31,c[3][2]=a31+a32,c[3][3]=a33,c[3][4]=a31+a32+a33+a34,c[3][5]=a35,c[3][6]=a35+a36,...
This is a[][] the third row of one-dimensional tree-like array

C[4][1]=a11+a21+a31+a41,c[4][2]=a11+a12+a21+a22+a31+a32+a41+a42,c[4][3]=a13+a23+a33+a43,...
This is the a[][] array of the first row + the second line + the third row and the fourth row after the tree array


Have you figured out the rule of a two-dimensional tree-like array c[][]? With a closer look, you will find that:


(1) In two-dimensional cases, if the A[i][j]=delta is modified, the corresponding two-dimensional tree-like array update function is:
[Java] View plain copy print?                private void Modify (int i, int j, int delta) {A[i][j]+=delta; for (int x = i; x< a.length x + + lowbit (x)) for (int y = j; y <A[i].length; y = = lowbit (y)) {c[x]                    [y] + = Delta; }       }

(2) In a two-dimensional case, the function of finding the sum of the elements of the ∑a[i][j] (the first I row and the first J column) is
[java]  View plain  copy  print?  int sum (int i, int j) {      int result = 0;       for (int x = i; x > 0; x -=  Lowbit (x))  {        for (int y = j; y >  0; y -= lowbit (y))  {             result += C[x][y];        }      }     return result;  }       sun (1,1) =c[1][1];   Sun (1,2) =c[1][2]; sun (1,3) =c[1][3]+c[1][2];...    sun (2,1) =c[2][1];  sun (2,2) =C[2 ][2]; sun (2,3) =c[2][3]+c[2][2];...    sun (3,1) =c[3][1]+c[2][1]; sun (3,2) =C[3][2]+C[2][ 2];  

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.