Tree-like array learning data 1

Source: Internet
Author: User

11-D Tree-like array

1 What is a tree-like array
A tree array is a data structure with query and modification complexity of log (n), assuming array A[1..N], then querying A[1]+...+a[n], which is a log level and an online data structure.

2 tree-like array effect
We often encounter dynamic continuous and query problems, given N elements A[1~n], let's ask sum[l,r] = A[l]+...+a[r], or change the value of a[i].

Assuming that the data is very small, then we can take advantage of violence, this time change a[i] complexity is O (1), but the sum of the complexity of O (n), if the sum of M is O (n*m), but when M is very large, this method is obviously not able to meet the requirements of efficiency. This time we introduced a tree-like array, the sum and update of the tree array are O (Logn), so greatly reduced the complexity.


3 Specific analysis

1 The tree-like array is the first to a[] and c[] empty, and then assume that there is n number of n times the update () operation is to establish a tree array, so the total time complexity is O (NLOGN).

2 set the original array to A[1..N], the tree array is C[1..N], where c[k] = a[k-(2^t) +1] + ... + a[k]. e.g. c[6] = A[5] + a[6].

Suppose A is a counted array, and C is a tree array (count)

        0000 0001:c1 = A1
        0000 0010:C2 = A1 + A2
        0000 0011:c3 = A3
        0000 0100:c4 = A1 + A2 + A3 + A4
     &nbs p;  0000 0101:c5 = A5
        0000 0110:c6 = A5 + A6
    &NB sp;   0000 0111:c7 = A7
        0000 1000:c8 = A1 + A2 + A3 + A4 + A5 + A6 + A7 + A8
        ...
        0001 0000:c16 = A1 + A2 + A3 + A4 + A5 + A6 + A7 + a8+ A9 + A10 + A11 + A12 + A1 3 + A14 + a15+ A16


3 that is, the k is represented as a binary 1***10000, then c[k] is a[1***00001] + a[1***00010] + ... + a[1***10000] This number of the and.


4 Set a function lowbit (k) to obtain a minimum of 0 bits of K, it is easy to find, according to the above representation, from a[1] to a[k] The sum of all the numbers is
SUM[K] = C[k] + c[k-lowestbit (k)] + c[k-lowestbit (k)-lowestbit (K-lowestbit (k))] + ... So you can find out sum[k in Logk time].


5 when an element in an array changes, the C value that needs to be changed is C[k],c[k+lowestbit (k)], C[k+lowestbit (k) +lowestbit (K+lowestbit (k))] ... This complexity is LOGN (n is the maximum range)


6 If the topic requires sum[l, R] = Sum[r]-sum[l-1]
SUM[L-1] = A[1]+a[2]+...+a[l-1]
SUM[R] = A[1]+a[2]+...+a[l]+...+a[r]
SUM[R]-SUM[L-1] = A[l]+a[l+2]+...+a[r]


7 The index of the tree array is strictly starting from 1, so if 0 is present, pay attention to add 1. Because Lowbit (0) is 0, it enters an infinite loop if it appears as 0, and each element in the tree array contains at least one value of its own.



22-D tree-like array

12-D Tree Array It's just that every dimension is a tree-like array.

Problem: A large matrix of numbers that can be manipulated in two ways
1 Add an integer to a number in the matrix (can be positive negative)
2 Query the and of all the numbers in a sub-matrix, ask for each query, output the result.

A 21-D tree array can easily be extended to two dimensions, in two-dimensional cases: The tree array of array a[][] is defined as:
C[x][y] =∑a[i][j], wherein, x-lowbit (x) + 1 <= i <= x, Y-lowbit (y) + 1 <= J <= y.

3 Examples: Take a 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}};
So it corresponds to a two-dimensional tree-like array c[][]?

Remember:
B[1]={a11,a11+a12,a13,a11+a12+a13+a14,a15,a15+a16,...} This is the first row of a one-dimensional tree array
B[2]={a21,a21+a22,a23,a21+a22+a23+a24,a25,a25+a26,...} This is a one-dimensional tree 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 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. This is the one-dimensional tree array of the first row of a[][]

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 This is the tree array of the first row and the second row of the a[][] 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 one-dimensional tree array of the third row of a[][

C[4][1] = A11 + A21 + A31 + A41, c[4][2] = A11 + A12 + A21 + A22 + a31 + a32 + A41 + A42, this is a[][] array first row + second row + third row + fourth row after tree-like array



3 Two types of operations for tree arrays

1 single point update, Interval summation

11-D tree Array, single-point update, Interval summation

For example, to update the point x, the value of the X point plus Val is called Add (X, Val), and the Getsum (x) for interval [1, x]

int lowbit (int x) {    return x& (-X);} int getsum (int x) {    int sum = 0;    while (x) {        sum + = treenum[x];        X-= Lowbit (x);    }    return sum;} void Add (int x, int val) {while    (x < MAXN) {         treenum[x] + = val;         x + = Lowbit (x);    }}
 22-D tree Array, single-point update, Interval summation

For example, to update the point (x, y), the value of the (x, y) point plus Val is called add (x, Y, Val), the rectangle [1, 1]-[x, Y] and is getsum (x, y)

If the area of the rectangle is getsum (x2, y2)-getsum (x1-1,y2)-getsum (x2,y1-1) +getsum (x1-1, y1-1)

int lowbit (int x) {    return x& (-X);} int getsum (int x, int y) {    int sum = 0;    for (int i = x; i > 0; I-= Lowbit (i)) for       (int j = y; j > 0; J-= Lowbit (j))           sum + = treenum[i][j];    return sum;} void Add (int x, int y, int val) {for    (int i = x; i < maxn; i + = Lowbit (i)) for       (int j = y; J < Maxn; J + = Lowbit (j))           Treenum[i][j] + = val;



2 interval Update, single point summation

11-D tree-like array

Change the interval [x, y], each number in the interval [x, y] plus Val, the value of the query point K

The interval [x, y] plus val equals point x plus val, point Y+1 minus Val, then the value of the k point equals [1,k] and

int lowbit (int x) {    return x& (-X);} int getsum (int x) {    int sum = 0;    while (x) {        sum + = treenum[x];        X-= Lowbit (x);    }    return sum;} void Add (int x, int val) {while    (x < MAXN) {         treenum[x] + = val;         x + = Lowbit (x);    }} void Solve () {    //Add Val    Add (x, Val) to each point of the interval [x, y];    Add (y+1,-val);    Computes the value of the point k    int num = getsum (k);}



22-D tree-like array

change rectangle [x1, y1]-[x2, Y2],[x1, y1]-[x2, Y2] all plus Val, the value of the query point (x, y)



Rectangle [x1, y1]-[x2, y2] inside each element plus a Val equivalent to the point (x1, y1) plus val, dot (x1, y2+1) minus Val, Dot (x2+1, y1) minus Val, Dot (x2+1, y2+1) plus VAL. then the value of the point (x, y) is asked for [1, 1]-[x, Y] and

int lowbit (int x) {    return x& (-X);} int getsum (int x, int y) {    int sum = 0;    for (int i = x; i > 0; I-= Lowbit (i)) for       (int j = y; j > 0; J-= Lowbit (j))           sum + = treenum[i][j];    return sum;} void Add (int x, int y, int val) {for    (int i = x; i < maxn; i + = Lowbit (i)) for       (int j = y; J < Maxn; J + = Lowbit (j))           Treenum[i][j] + = val; void Solve () {     //Rectangle [x1, y1]-[x2, y2] Each dot plus val     add (x1, Y1, Val);      Add (x2+1, y1,-val);      Add (x1, y2+1,-val);      Add (X2+1, Y2+1, Val);      Find the value of the point (x, y)     int num = getsum (x, y);}

5 Common Tricks

Assuming that the value of each point in the initialization array is 1, we know that for a one-dimensional tree array, we know treenum[i] = Lowbit (i). For a two-dimensional tree array treenum[i][j] = Lowbit (i) *lowbit (j)

void Init () {    //one-dimensional    memset (treenum, 0, sizeof (treenum));    for (int i = 1; i < MAXN; i++) {        num[i] =1;        Treenum[i] = lowbit (i);    }        Two-dimensional    memset (treenum, 0, sizeof (treenum));    for (int i = 1, i < MAXN; i++) {for       (int j = 1; j < Maxn; J + +) {           num[i][j] = 1;           TREENUM[I][J] = lowbit (i) *lowbit (j);}}    




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Tree-like array learning data 1

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.