Getting started with a tree-like array

Source: Internet
Author: User


Portal: https://www.topcoder.com/community/data-science/data-science-tutorials/binary-indexed-trees/#introduction

Here is some analysis of the tree array:

inline int lowbit (int x)
{
return x& (-x);
}

void Update (int x, int C)
{
int i;
For (i = x; I < maxn; i + = Lowbit (i))
tree[i] + = c;
}

int Getsum (int X)
{
int i;
int temp=0;
For (i = x; i >= 1; i-= lowbit (I))
Temp + = tree[i];
Return temp;
}

The following analysis is now performed on three functions:

Lowbit (x) is the 2^p (where p is the rightmost 1 position of the binary representation of x), as the binary representation of 6 is 110, and the rightmost 1 is 1, so lowbit (6) = 2^1 = 2.

Update (x, C) is to change the value of the X point c, if the general array is changed by x itself, but in the tree array (x, x+lowbit (x), x+lowbit (x+lowbit (x))),...) The point of this path has to be changed to c, and this is done in order to be able to sum it efficiently later.

Getsum (x), is for (1, ... x-lowbit (x-lowbit (x))), x-lowbit (x), x) The sum of the points of this path, in other words, a general array a[1] to a[x].

The most basic function of a tree array is to find the number of points that are smaller than a point x (the comparison here is an abstract concept, which can make the size of the number, the size of the coordinates, the size of the mass, etc.).

For example, a given array of a[5] = {2, 5, 3, 4, 1}, b[i] = The number of digits to the left of the position I is less than or equal to a[i]. such as b[5] = {0, 1, 1, 2, 0}, This is the most orthodox tree-like array of applications, straight through the array, each M (a[i]), then modify the tree-like array update (a[i], 1). When the range of the number is larger, discretization is required, that is, first order, then Renumber. such as a[] = {10000000, 10, 2000, 20, 300}, then discretized a[] = {5, 1, 4, 2, 3}.

But let's think of a question, what if we ask for b[i] = number of numbers that are greater than or equal to a[i] on the left of position i? Of course we can disperse the inverted number, but there is no more direct method? Almost all of the three functions of the tree array in the tutorial are written in that way, but we can think about what the change is: x is constantly increasing, the sum is that X is decreasing, and whether we can reverse it, the answer is Yes.

void Update (int x, int c)
{
Span style= "font-size:18px;" >int i;
for (i = x; i >= 1; i-= lowbit (i))
{
tree[i] + = c;
}
}

int Getsum (int X)
{
int i;
int temp (0);
For (i = x; I < maxn; i + = Lowbit (i))
{
Temp + = tree[i];
}
Return temp;
}
We just swapped the loop statements in the two functions, and now each time we want to change the value of the point x, we need to modify (1, ... x-lowbit (x))), x-lowbit (x), x), and the sum becomes (x, x+lowbit (x), x+lowbit (x+lowbit (x))),...) The point of this path. And That's not exactly the sum of points greater than or equal to x?

So we can either modify the x-enlarged path, sum x the path of reduction, or modify the path of x-reduction, summing the path of x-enlargement, and decide which to use according to the need of the TOPIC.

Note: both are compared to the left. If you need to compare to the right, only reverse input is Required.

Getting started with a tree array

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.