Tree-like array summary

Source: Internet
Author: User

A tree-like array of necessary plots

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvd2luzgryzwftcw==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">

This figure represents the change in the array. Make c[1] = a[1], c[2] = a[1] + a[2], c[3] = a[3], c[4] = a[1] + a[2] + a[3] + a[4]..

。 Each c[i] value represents the corresponding I can control the interval, then assume that after changing a value, only need to change the C array to control the interval of c[i] will be able to

1. Tree-like array. Two most important operations, changes and queries

The first condition is God-like function

int c[100010];

int lowbit (int x)
{
Return x&-x;
This function enables the i-=lowbit (i) to jump to the node before I control the interval i + = lowbit (i) jumps to the node after the I control interval, which means that I can jump to the next control interval containing the position of I

Changes

void Add (int i,int n,int D)
{
while (i <= N)
{
C[i] + = D;
i + = Lowbit (i);
}
}

Inquire

int sum (int i)
{
int a = 0;
while (i)
{
A + = C[i];
I-= lowbit (i);
}
return A;
}

In this change, it is changed from front to back. The control interval in all C arrays is changed to include the value of I, and the query is followed by a forward search. Log (n) time to search for the entire value


2. Not only can you change a value in a tree array, you can also change all the values within the range

1. Assuming that each query is a value in the array, the tree-like array is more time-saving than the segment tree, for example. Suppose you want to change the value in [x, Y]. The ability to use array A to retain the initial situation, array B represents the situation of the change (B[i], from I to n the full number of +b[i]), then for [x, Y] can be expressed as b[x] + = 1, b[y+1] +-1; In a tree array written out by B array, for calculation I, by b[1] + b [2] + b[3].

。 B[i] represents all of the changes to I

2. Suppose the query is a period of interval and. Can also be written out using a tree array. As above, array a retains its initial condition. Array B represents the case of the change (B[i] represents all changes from I to n), then the sum of the interval [x, y] is calculated = Sy-sx. Take SX for example SX = initial value + changed value, changed value = b[1] * x + b[2] * (x-1) +: B[x]*1 = (x+1) * (B[1]+b[2].. +B[X])-(b[1]*1 + b[2]*2 +.

B[X]*X) The and of the b[i] in two tree-like arrays. The B[i]*i and

A 32-D tree-like array

Change and query operation of two-dimensional tree array

Changes

void Add (int i,int j,int n,int D)
{
int x, y;
for (x = i; x <= n; x + = Lowbit (x))
for (y = j; y <= n; y + = Lowbit (y))
C[x][y] + = D;
}

Inquire

int sum (int i,int j)
{
int a = 0, x, y;
for (x = i; x > 0; X-= Lowbit (x))
for (y = j; y > 0; y-=lowbit (y))
A + = C[x][y];
return A;
}

Principle the same dimension, just c[i][j in two dimensions] represents a rectangle from [to] [i,j] and

The same when changing a value, it is convenient to query out a range of and. Easily query the value of a number when changing an interval

3, three-dimensional tree-like array

Changes

int add (int i,int j,int k,int n,int D)
{
int x, y, Z;
for (x = i; x <= n; x + = Lowbit (x))
for (y = j; y <= n; y + = Lowbit (y))
for (z = k; z <= n; z + = Lowbit (z))
C[X][Y][Z] + = D;
}

Inquire

int sum (int i,int j,int k)
{
int a = 0, x, y, Z;
for (x = i; x > 0; X-= Lowbit (x))
for (y = j; y > 0; y-= Lowbit (y))
for (z = k; z > 0; z-= Lowbit (z))
A + = C[x][y][z];
return A;
}

Using the same method as above


4. Examples

1.poj2481. The order of S and E is guaranteed to be counted for all cows who are stronger than I in the case of I, and for the array of marks before I. Convenient to count the previous number in a tree-like array

2.poj2352. The problem of stars, like Ditto, makes every level smaller than it is counted before it

3.poj2299. How many times do you need to exchange your questions? can be translated into order from the first beginning each time it is before it. Then for the first I only need to exchange before I, and larger than I, the topic is converted to the problem of reverse order number, using a tree-like array to find the number of reverse order, can forward the number of smaller than it. To use (i-sum) to find a greater number before I, or to count directly backwards, a larger number than he

4.hdu1556. Each time you change a range of values, ask the value of a number

5.poj2182, the disorderly cows stand together. The given number indicates how many serial numbers before the ox are smaller than that of the bull, and then the number of the first bull is counted, then the ordinal of the I-ox is found in the a[i]+1, and the tree-like array is used to calculate the ordinal number of the current first. Use two points to find the first a[i]+1, the serial number is the number of cattle a[i].

(Note in order to prevent a sequence number from being used multiple times, locate the ordinal as far left as possible)

6.poj3468, tree-like array solution

7.poj2155, a two-dimensional tree array, changing the number of a rectangle, asking the size of a number

8.hdu3584. Three-dimensional tree-like array, ibid.

Tree-like array summary

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.