Initial analysis of tree array Learning Series 1-original czyuan
In fact, learning tree array said White is to see that figure, that tree array and the relationship between the general array, understand the basic is no problem, it is recommended that the following Tutorial: http://www.topcoder.com/tc? Module = static & d1 = tutorials & D2 = binaryindexedtrees
The following section analyzes 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 above three functions can be said to be the "housekeeping skills" of the tree array. The efficiency of the tree array is reflected in these three functions. Now we will analyze the three functions.
Lowbit (X) is used to obtain 2 ^ P (where p is the rightmost position of 1 in the binary representation of X). For example, the binary representation of 6 is 110, the rightmost 1 is 1, so lowbit (6) = 2 ^ 1 = 2.
Update (x, c) is to change the value of X to C. If the General array changes x itself, but in the tree array, X + lowbit (x), X + lowbit (x ))),...) The point of this path must change C, so that the sum can be efficiently summed later.
Getsum (x) is the result of (1 ,... X-lowbit (x), x-lowbit (x), x), and, in other words, it is equivalent to finding the sum from a [1] to a [X.
The efficiency of the tree array is: Unlike the General array, the general array is a subscript that is constantly added to traverse, while the tree array is constantly updated by adding 2 ^ P, so the efficiency is (logn) level.
The most basic function of a tree array is to calculate the number of points smaller than a certain point X (here the comparison is an abstract concept, which can make the size of the number, the size of the coordinates, quality ).
For example, given an array a [5] = {2, 5, 3, 4, 1 }, evaluate B [I] = the number of places on the left where I is less than or equal to a [I. for example, B [5] = {0, 1, 1, 2, 0}, which is the most orthodox tree Array application, directly traversing the array, get getsum (A [I]) for each position, and then modify the tree array update (A [I], 1. When the range of numbers is large, discretization is required, that is, sorting the order and re-numbering. For example, if a [] = {10000000, 10,200 0, 20,300}, after discretization, a [] = {5, 1, 4, 2, 3 }.
But let's have a question. What if we need B [I] = the number where I is greater than or equal to the number of a [I] on the left? Of course we can reverse the number when discretization, but is there a more direct way? The answer is yes. In almost all the tutorials, the three functions of the tree array are written in that way, but we can think about the problem of changing X to increase and sum to decrease. Can we turn it back, the answer is yes.
Void Update (int x, int C)
{
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 changed the cyclic statements in the two functions. Now, every time we want to modify the value of point X, we need to modify the values (1 ,... X-lowbit (x), x-lowbit (x), x) paths, and the sum is calculated (x, x + lowbit (x ), X + lowbit (x ))),...) The point of this path. Isn't the sum of vertices greater than or equal to X?
Therefore, we can modify the path for increasing X, the path for decreasing X, the path for decreasing X, and the path for increasing x according to the needs of the question.
If you have mastered the above methods, you can solve most of the tree array problems ~~
Why is it a majority of problems? The example shown below will subvert the concepts you have created earlier.
Poj 2155
Http://acm.pku.edu.cn/JudgeOnline/problem? Id = 2155
Questions from the building instructor ~~ The original question is two-dimensional. We will first simplify it into one-dimensional discussions.
Two operations are required for the question:
1. Change all values in a (A, B.
2. Evaluate the value of a vertex (.
The online version of the problem is almost directly copied Baidu encyclopedia tree array to explain the http://baike.baidu.com/view/1420784.htm (this version I understand for a long time ...)
My understanding is as follows:
Obviously, this is the opposite of the tree array operation. Let's think about the two functions Update () in the tree array to modify the value of a vertex (specifically a path, incrementing or decreasing), getsum () summation interval (1, x) the value of the vertex. As we have analyzed above, these two functions are essentially the same and can be exchanged. In other words, changing a vertex x and a summation interval (1, x) can be exchanged, that is, we can use getsum (x, c) to modify (1, x) update (X) is used to calculate the value of a vertex in this interval. The writing of the two functions is exactly the same as that of the original function (or the function name is changed), but the idea has changed. (It is hard to understand here because it is just a thought change, but the program is basically unchanged from the original)
Back to the original question, we need to make the point + C in the range (a, B), only the point + C in the range (1, B), and the interval (1, a-1) within the point-C can be. If it is 2D, modify the matrix (x1, Y1) to (X2, Y2), I .e. (X2, Y2) + C, (x1-1, Y2)-C, (X2, Y1, y1-1)-C, (x1-1, y1-1) + C can be.
Conclusion: Through the above analysis, we can find that the update () and getsum () functions are the same. We can use up () and down () to replace them. Up () is the path for increasing X, and down () is the path for decreasing X.
Up () and down () have four combinations:
1. Up () indicates modifying the value of a single point, and down () indicates finding the range and.
2. Down () indicates modifying the value of a single point, and up () indicates finding the range and.
3. Up () indicates the change interval, and down () indicates the value of single point.
4. Down () indicates modifying the interval, and up () indicates the value of single point.
The values 1 and 2 are larger or smaller than the given values, while the values 3 and 4 are the same. use either of them.
The above is my understanding of the tree array. Please give me some advice ~~
The next article will summarize the issues that appear in the tree array in OJ (link:
Http://hi.baidu.com/czyuan%5Facm/blog/item/af6fe8a9177f7ef51e17a2ea.html )~~
Tree array Learning Series 2-OJ topic overview-czyuan original
In the previous article, we made some analysis on the tree array (see the initial analysis of tree array Learning Series 1-czyuan original http://hi.baidu.com/czyuan_acm/blog/item/49f02acb487f06f452664fbc.html). This article mainly summarizes the questions about the tree array of various OJ.
Note: As lowbit (0) = 0, this will lead to an infinite loop on the path where x increments. When any tree array may show 0, we add one to all, this avoids the trouble caused by zero ~~
Simple:
Poj 2299 ultra-quicksort
Http://acm.pku.edu.cn/JudgeOnline/problem? Id = 2299
To calculate the number of reverse orders, you can use the classic sort order, which is also a basic tree array question.
[This is the conclusion of the reverse TMD order ~~]
Poj 2352 stars
Http://acm.pku.edu.cn/JudgeOnline/problem? Id = 2352
The question is to find the number of stars in the lower left of each star. Since the Y axis has been sorted, we can directly create a one-dimensional tree Array Based on the X axis, and then calculate the number smaller than the first one, the template can be set directly ~~
[]
Poj 1195 mobile phones
Http://acm.pku.edu.cn/JudgeOnline/problem? Id = 1195
For a two-dimensional tree array, change Update () and getsum () to two-dimensional.
For example, change the update () function:
Void Update (int x, int y, int D)
{// Note: When I = 0, 0 + lowbit (0) = 0, it will lead to an endless loop!
Int I, J;
For (I = x; I <maxn; I + = lowbit (I) // note that maxn is the size of the tree.
{
For (j = y; j <maxn; j + = lowbit (j ))
{
Tree [I] [J] + = D;
}
}
}
Already [AC] the first line of the Two-dimensional array, by the way, the corresponding method of the matrix is learned!
Eg ::
If (A = 1 & B = 1)
Printf ("% d \ n", sum (c, d ));
Else if (a = 1 & B! = 1)
Printf ("% d \ n", sum (c, d)-sum (C, B-1 ));
Else if (! = 1 & B = 1)
Printf ("% d \ n", sum (c, d)-sum (A-1, d ));
Else
{
Printf ("% d \ n", sum (c, d)-sum (A-1, d)-sum (C, B-1) + sum (A-1, B-1 ));
}
Poj 2481 cows
Http://acm.pku.edu.cn/JudgeOnline/problem? Id = 2481
Sort E from small to small. If e is equal to S, then it will be the same as poj 2352 stars ~~
Poj 3067 Japan
Http://acm.pku.edu.cn/JudgeOnline/problem? Id = 3067
First, sort by the first coordinate from big to small. If it is equal, sort by the Second coordinate from large to small. Then it is the same as cows and stars...
【
55555555 is there no idea if you are swollen with such a good question. I have thought of it ......
Returns the number of reverse orders .......
It will be converted, He meow .....
]
[In fact, there is no need to store data in reverse order. This update (x +) has a feature that will allow you to handle all kinds of troubles, similar to the stars processing ,,,,,
Sorry, now I found that one will actually be ac .......]
[In any case, you must consider whether update (x, 1) or update (x + )]
Poj 2029 GET requests persimmon trees
Http://acm.pku.edu.cn/JudgeOnline/problem? Id = 2029
O (N ^ 2) Enumeration start point, and then calculate the points in the two-dimensional tree array.
Hoj 2275 Number Sequence
Http: // 202.118.224.210/judge/show. php? Contestid = 0 & amp; proid = 2275
The two one-dimensional tree arrays are recorded on the left side of the array, which is smaller than the left side and larger than the right side of the array ~~
Hoj 1867 manager's troubles
Http: // 202.118.224.210/judge/show. php? Contestid = 0 & amp; proid = 1867
First, the screen method is used to obtain the prime number, and then update (x, 1) if it is changed from a non-prime number to a prime number. If it is changed from a prime number to a non-prime number, update (x,-1.
Sgu 180 Inversions
Http://acm.sgu.ru/problem.php? Contest = 0 & problem = 180
Classic tree array + discretization. Note that long ~~ is used for the result ~~
Spoj 1029 matrix summation
Https://www.spoj.pl/problems/MATSUM/
Basic Two-Dimensional tree array...
Moderate:
Poj 2155 Matrix
Http://acm.pku.edu.cn/JudgeOnline/problem? Id = 2155
For details about the classic tree array, see the previous article (preliminary analysis of tree array Learning Series 1-original czyuan )~~
Poj 3321 apple tree
Http://acm.pku.edu.cn/JudgeOnline/problem? Id = 3321
The difficulty of this question is not a tree array, but if the whole tree is mapped to an array. We can use DFS () to change the timestamp, and use begin [I] to represent the first point of the subtree traversal with I as the root, end [I] indicates the last vertex that is traversed by the subtree with the root of I.
For example, the data is:
5
1 2
2 5
2 4
1 3
Then begin [] = {1, 2, 5, 4, 3}, end [] = {5, 4, 5, 4, 3}, subscript starts from 1.
For each vertex corresponding to a range (begin [I], end [I]), if you want to change the status of vertex A, you only need to update (begin [a]), the Apple Tree of the subtree, that is, getsum (begin [a])-getsum (end [a] + 1), (note: here, the sum is the sum of the path X increments .)
Poj 1990 moofest
Http://acm.pku.edu.cn/JudgeOnline/problem? Id = 1990
The difficulty of this question is to use two one-dimensional tree arrays to record the number of cows whose horizontal coordinates are smaller than it, and the sum of the horizontal coordinates of cattle whose horizontal coordinates are smaller than it before it.
Sort by volume, and the formula is:
Ans + = 1ll * cow [I]. volumn * (count * X-pre + total-Pre-(I-count) * X );
Cow [I]. volumn is the volume that the cow can hear.
Count is the number of cows whose abscissa is smaller than I.
PRE is the sum of the abscissa of the ox whose abscissa is smaller than that before the I ox.
Total indicates the sum of X coordinates of the first I-1 points.
It can be calculated based on the two parts where the x-axis is smaller than the x-axis and the X-axis is greater than the x-axis.
HDU 3015 disharmony trees
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3015
The same method as the question. After discretization according to its requirements, sort by height in descending order and apply the method of the two tree arrays above.
Hoj 2430 counting the algorithms
Http: // 202.118.224.210/judge/show. php? Contestid = 0 & amp; proid = 2430
This is actually a greedy question, from left to right or from right to left, find the same as it can be deleted. Scan and record the location of the first and second appearance. Then, from right to left, you only need to change the tree array of the left position, because the right side won't be used again.
Tju 3243 blocked road
Http://acm.tju.edu.cn/toj/showp3243.html
This is mainly because if we determine whether to connect, We can first use J = getsum (B)-getsum (A-1), if J is equal to (B-A) or getsum (N) -J is equal to (N-(B-A), so that a and B are connected.
Spoj 227 ordering the soldiers
Http://www.spoj.pl/problems/ORDERS/
This is exactly the opposite of the normal tree array question. Given the number of vertices in front of array B [I] that are smaller than a [I], evaluate the array.
Let's first think about simple practices, such as B [] = {0, 1, 2, 0, 1 }, we use the array C [I] to indicate the number of remaining numbers smaller than or equal to I. At the beginning, C [] = {1, 2, 3, 4, 5}, and the subscript starts from 1.
We scan the array B [] from the right to the left, B [5] = 1, indicating that the number of this point is 4th of the remaining number, that is, four smaller than or equal to it, that is to say, we need to find the smallest J in line with C [J] = 4 (here we can think about why it is the smallest, not the largest, which is quite understandable), and C [] is ordered, so we can use binary to find J, with the complexity of O (logn), but now the problem is the complexity of every update of C [] to O (N). Here we think of a tree array, c [I] indicates that there are still less than or equal to the number of I. Isn't this exactly the housekeeping skill of the tree array ~~ Therefore, the complexity of processing each location is O (logn * logn), and the total complexity is O (n * logn ).
HDU 2852 Kiki's k-Number
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2852
This is similar to the above question. It only requires the number k greater than a. Then we use getsum (A) to find the number less than or equal to, therefore, we need to calculate the number k + getsum (A), and the delete operation only needs to determine whether getsum (a)-getsum (A-1) is 0, 0 indicates that a does not exist.
Difficulties:
Poj 2464 brownie points II
Http://acm.pku.edu.cn/JudgeOnline/problem? Id = 2464
This question can also be done in binary mode. Here we will introduce the tree array approach. First, there are N points. After each point, the X and Y axes can be performed to cut the plane into four parts: BL, Tl, TR, and BR, the problem is that we can quickly calculate the number of points in the four parts.
In this way, we can first pre-process, first sort by Y coordinates, and find the number of points on the right and right of each point leftpoint [], rightpoint [], complexity is O (n ), similarly, we sort by X coordinates to find the number of uppoint [] and downpoint [] vertices at the top and bottom of each point. Also obtain the number of vertices greater than the I y coordinate lagey []. Note: subscript ing is required because the subscript of the two sorting points is different.
Then sort by X coordinates from small to large, and sort by Y coordinates from small to large if X coordinates are equal. We can place Y coordinates in a tree array.
For vertex I, obtain getsum (Y [I]), that is, the number of BL, and then update (Y [I]). Because it is the I point, it indicates that there is an I-1 point before
TL = I-1-leftpoint [I]-BL;
Tr = largey [I]-Tl-uppoint [I];
BR = N-bl-Tl-tr-leftpoint [I]-rightpont [I]-uppoint [I]-downpont [I]-1;
In this way, we can find the number of points in the four parts, and then determine whether the current optimization is available. If yes, we can update the number ~~
UV 11610 reverse prime
Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & page = show_problem & category = 78 & problem = 2657 & mosmsg = Submission + received + with + ID + 7313177
A comprehensive tree-based array of questions, using many knowledge points in the tree-based array, including discretization and binary search.
1. First, screen the prime number according to the requirement of the question and find all the reverse Prime.
2. discretization of the reverse Prime, with only about 78500. Tree [I] records the number of vertices smaller than I in a tree array.
When the q a operation is executed, the smallest J of the binary search makes getsum (j) equal to ++ A (because a may be 0, so the unified one plus one ). This step is similar to spoj 227 ordering the soldiers above.
3. When performing the d a operation, find the value B after discretization of A, and then update (B,-1.
After doing this, the running time is more than S, which is quite surprising, because it is less than S. Here, I would like to express my special thanks to the guidance of liuzhe. In fact, the reverse prime in the question is obtained by an inverted Prime Number of 10 ^ 6, so the requirement is 7 digits, and the last digit must be 0, we can divide each value by 10, so the number range is reduced by 10 times, and the speed is improved a lot.
Finally, I added some optimizations and ran 0.056 S, ranking 3rd in the ranking ~~
3 7313367 czyuan 0.056 C ++
Czyuan original, reprinted please indicate the source.