Tree array problem set [constantly updated]

Source: Internet
Author: User
Tags sort

I. Rationale


As can be seen from the graph, the c[used in the tree array], each point has a certain jurisdictional range;

such as c[1]=a[1];

C[2]=A[1]+A[2];

C[3]=A[3];

C[4]=A[1]+A[2]+A[3]+A[4]; etc;

The key of this kind of structure is to get the value of a certain point or an interval efficiently by deleting and increasing the inner dynamic of an array.

For example, an array of a, change the value of a bit to be O (1), to find a k interval value O (k);

Such m operations are with O (m*k);

Obviously the data is very big, the efficiency is much worse;

And the tree-like array, which changes one, or asks for a certain interval, is O (LOGN); efficiency is greatly improved;



For the above figure how to calculate the interval and the key is to need several functions;

First of all, the simplest use of a tree array, modify a point in the above image, and seek a certain interval of the and;


The first function;

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

This function is mainly used to ask for a certain point of jurisdiction;

x& (-X); The origin of this thing is not said ... Ash is always wonderful.

If it is x+=x& (-X), it is the value of the parent node of the pity Dorado, such as x=4, and can get 8;

x-=x& (-X) is the jurisdictional point of the next interval of the jurisdictional interval of the x point;

For example, x=7, substituting 6, and constantly circulating to 0 to get 6 in turn ... 4.;

All of their jurisdictions are just 1 .... 7;

A second function

void update (int x,int num)
{
    while (x<=n)
     {
         d[x]+=num;
         X+=lowbit (x);
     }
}

This function is used to modify the tree-like array;

If the general algorithm is only modified pity Dorado, but the tree-like array must modify all the pity Dorado to be governed by the interval;

For example, the a[2 of a array] minus 1, (make n=16), then all 2 of the points under the jurisdiction of 4,8,16 should be subtracted 1;

is to call the function update (2,-1);
A third function

int getsum (int x)
{
    int s=0;
    while (x>0)
     {
         s+=d[x];
         X-=lowbit (x);
     }
    return s;
}

This function is to seek the interval and the. For example Getsum (7) words, is to beg a[1]+a[2]+...a[7];

The above is the most basic use, to learn the tree-like array must be the above process principle to understand;

Figure out the difference between the D array and the original a array, the beginner should draw a picture himself;


Two. Deformation application of tree-like array

<1> Each change is a point, the request is about a certain interval;

This is the best thing to do; for example, poj2352 stars; Ask for the number of dots in front of each point.

Only set array a[], first all is 0, then there is a point in turn to modify, and this statistic;

This one is the most basic upward modification, downward statistics; )


<2> each time the change is an interval, the value of the request is about a point;

Representative of the typical topic is HOJ1556 color the ball;

The problem is that each time a whole interval is modified, the last one is the number of changes to each point;

This needs to be the above function, slightly modified;

<span style= "FONT-SIZE:18PX;" >void Update (int x,int num)
{
    while (x>0)
     {
         d[x]+=num;
         X-=lowbit (x);
     }
} </span>

To change downward, add the interval after it;

And then revise it backwards to subtract the unnecessary modification intervals;

Use his parent node to record the number of staining per point;

Statistical time to

<span style= "FONT-SIZE:18PX;" >int getsum (int x)
{
    int s=0;
    while (x<=n)
     {
         s+=d[x];
         X+=lowbit (x);
     }
    return s;
} </span>

This modification of an interval, and the number of changes to find a point, the general is to modify downward, upward statistics;

For the two-dimensional case, d[i][j in d[][] This point also has his own jurisdictional range, from one dimension to two dimensions; it can be easily understood;

One-dimensional case is the jurisdiction of an interval, and the two dimensions are a rectangular jurisdiction, and each dimension can be considered independently;


Three. Topic analysis


One, two kinds of situations;

1, to up the statistics, downward modification, generally is to modify the value of an interval, looking for a bit of value;

HDU1556 color the ball;

This problem is the most basic of this kind of problem, the key is to understand the upward statistics, downward modification of how to operate and principle;

poj2155 Matrix

This is the problem of the building sky, is a very typical application of two-dimensional array of numbers; it is a two-dimensional version of hdu1556;

Remember to count up and down, and the two-dimensional case, the main thing is to understand the meaning of the values stored in that array (the sum of a rectangular region);

poj2299 Ultra-quicksort; (reverse order number);

This question can be made with a sort of merge, and here's a little bit more than that.

In order to find the inverse number, it is also a typical application of the tree array, and the number of numbers in front of the number is larger than that of him.

Can be compared with the number of a string in front of his small numbers of this question to think more about ...

poj3067 Japan

The problem is to sort x first, and then y to ask for the number that precedes him;

is also a basic application;



Two, 2, to be modified upward, downward statistics, generally is to modify the value of a position, looking for a period of the and;

poj2352 stars

This question is the most basic one-dimensional situation, which has just been said.

poj1195 Mobile Phone

is a two-dimensional situation. Change the size of a number on a position, and modify all points that govern the point;

Statistics, is the downward statistics;

poj2481 cows

This question to the Y by not descending order, X not ascending sort, basically is poj2352. But notice that there can be a complete overlap interval;

poj3321 Apple Tree;

The problem is to use a tree-like array, but the difficulty in that DFS how to ask for that time stamp, after finding out, nested in the tree array that part of the east. It's done.

poj1990 moofest;

The O (n^2) algorithm must be timed out, and the sum and number of the numbers preceding him are stored in the order, so the tree-like array;

poj2309

The small test sledgehammer of the tree array, to be done, to understand the words.

Can be used in a tree-like array of places, must be able to use line tree;

However, the tree-like array coding is simple, for a certain interval modification, evaluation, very efficient;

In addition, a tree-like array of gray often good URLs to vomit blood recommended ...

Http://www.topcoder.com/tc?module=Static&d1=tutorials&d2=binaryIndexedTrees#prob



Tree-like array, just beginning to learn the time to feel very magical, especially when seeking lowbit ... After writing a few questions, I think this thing is very useful. A time-out trap to note: x==0 situation

A tree array is a good choice when it comes to solving the problem of dynamic interval. Give a few examples:

(1): http://acm.pku.edu.cn/JudgeOnline/problem?id=2352 starts

The main idea is to give the coordinates of some stars in two-dimensional coordinates, to find the number of stars on the left, below, and below. Solve the problem when learning the "dimensionality" of the idea, first of all the stars in accordance with the y-coordinate from small to large, x from small to large sort. The tree array is set up in x coordinates. In this way, each time a star is counted, the previous star, as long as the x-coordinate is smaller than its, it must be in its left, lower, lower left. At this point, just use the tree-like array to get from the 1~x and both, after the statistics to remember to update the tree array (attached code) (note the case of coordinates 0, here is a timeout trap)

(2): http://acm.pku.edu.cn/JudgeOnline/problem?id=2155 Matrix

Two-dimensional tree array, the main idea is a two-dimensional rectangular plane, which has a number 0 or 1, the initial time is all 0, gives two operations, one is to change the value of a sub-range, 0 to 1 or 1 to 0. The other is to query whether a value on a coordinate is 0 or 1.

Data range: Coordinate n<=1000, operand t <=50000.

The method of solving this problem is very ingenious, first of all we consider that if you change the value of the interval, the interval of each point is changed once, then the time operation will need to n^2, it is too time-consuming. Imagine that if we were to ask for the number of operations of a certain point and turn the number of operations into the same as that for this point, then we could set up four Sentinels to mark the operation in a certain interval:

As shown in figure, when we want to change the values of the interval (3,3) ~ (6,5), respectively, in accordance with the above image to change the value of the matrix, then, the interval inside to (the number of points) and 1, and the interval outside to (the) point of the and 0, so that the problem of transformation in order to dynamically find two
, a two-dimensional tree array is used to solve both

(3): http://acm.pku.edu.cn/JudgeOnline/problem?id=3321

The main idea is to give a tree, the number of nodes <=100000, the nodes in the tree may have apples, two operations: one is to change a node of the Apple, has changed to No, there is no change. The second operation is to find the total number of apples for a node and a subtree rooted in that node. The number of operations reached 100000;

The problem involves transforming a tree structure into a linear structure, and then using a tree-like array to dynamically sum it. The transformation method for DFS once this tree, the first access and the last access of each node in the sequence number, at this time, the first access to a node and the last access number between the number, must be the node's child nodes, according to the access number to establish a tree array, when changing a node apple, Simply follow the number that the node first accesses to modify the value in the tree array, and when the query is queried, the and of the first access and the last access number are counted.

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.