Time：2021-10-11

# Treearray summary

## preface

1. In the past three weeks, I mainly reviewed the line segment tree and tree array.However, the line segment tree can’t write after a black problem of pressure position collapses.
2. On a whim, I decided to write a summary of the review content during this period.The konjaku has limited strength. Please understand if some practices are more difficult
3. This summary assumes that you haveBasic contents of tree array

## [i] single point modification, interval and query

1. The most classic questions to be maintained with a tree array. ($$P.S.$$It is also possible to use a line segment tree, but the constant is large and the code amount is large,Not elegant)
2. It belongs to the basic content of tree array, which will not be explained here.

## [II] modify the same value in the interval and query at a single point

1. It needs to be solved by difference. After the difference, the prefix sum is the value of a single point.
2. At the same time, the difference represents the difference from the previous oneDifference, so we only need to modify one head and one tail (for interval)$$[a,b]$$ ，$add(a,1),add(b+1,-1)$ )
3. $$O(n)$$Pretreatment,$$O(m \log n)$$Modify + query
4. The delay marking of the segment tree can achieve the same efficiency, but as mentioned above,Not elegant

## [III] modify the same value in the interval and query the interval

1. Although the line segment tree has a large number of codes, butProfessional people do professional thingsIn most cases, it’s better to let the line segment tree do it (the tree array is difficult to write)
2. $$But$$In some special cases, tree arrays are much easier to use.

### ExamplesThe tree outside the school gate

• A rare tree array problem of interval modification interval query.
• Because the query is the number of trees, the difference is difficult to solve.
• According to the meaning of the question, it can be transformed into this problem: query the number of intersections between an interval and the existing interval.
• In this way, for each existing interval, there are three cases: the left endpoint is in the interval, the right endpoint is in the interval, and both the left and right endpoints are in the interval. But the third situation is more difficult to solve.
• Therefore, we consider changing the definition:Find the number of no intersection with the query interval
• This is very easy to solve. There are two cases: the left end point is on the right side of the interval and the right end point is on the left side of the interval. The tree array optimizes the position and prefix of the left and right end points.
• However, because it is a prefix sum, the left endpoint on the right side of the interval will be converted into a total minus the left endpoint on the left side of the interval and within the interval.
• The answer is total – (total – (left endpoint is on the left of the interval + left endpoint is in the interval) – (right endpoint is on the left of the interval) = left endpoint is on the left of the right endpoint of the query interval + right endpoint is on the left of the left endpoint of the query interval.

### Example [sdoi2009]HH’s Necklace

• According to the conventional idea, this problem must be online query, but it is difficult to maintain (at least I didn’t think of it).
• But this question has one characteristic:No modification
• Then this question can be done offline (many questions can cheat a lot of points offline, and this question can get full points).
• We can press the query intervalRight endpointSort in ascending order, and then update the number of flowers on the left of each position at the right endpoint each time, and then query the interval.
• However, the number of flower species is still difficult to maintain because it is impossible to quickly determine or query whether the words at one location and another are the same within the allowable range of space.
• This is the advantage of offline. Because it is updated to the right endpoint of the current query, it must be an interval extending from the right endpoint to the left. As long as the existence of each flower closest to the right is guaranteed,Pull out the others, you can ensure that it is not heavy or leaking.

## [iv] finding reverse order pairs in tree array

1. This is also the classic use of tree arrays$$CDQ$$The necessary knowledge of divide and conquer. Of course, due to the problem of scope, it has to be discretized most of the time.
2. This is also the basic content and will not be explained.

### ExamplesMatch queue

• To minimize the total square difference, Feichang is easy to think of two piles of matches with the same size as a pair.Yi Zheng, readers are welcome to prove it themselves.
• Since only about two can be exchanged at a time, it is similar to bubble sorting. But the simulated bubble sort is obviously t out.
• The essence of bubble sorting is to findReverse order pairnumber.
• The idea of this problem is to discretize first, then sort according to the position order of a row of matches (the most abstract place), and finally use the tree array to find the reverse order pairs.

### Little practiceThree dimensional partial order (flowers bloom on the street)

• Template, do it yourself.The question is that the author hasn’t done it yet.

## [v] tree array + binary search space

1. In some cases, we hope to find a vacancy quickly. comparison$$O(n)$$Find,$$O(\log n^2)$$It looks better（$$P.S.$$as$$DFS$$Try not to use such questions,$$DFS$$Due to the time complexity, the data range is relatively small, and the tree array + bisection is slow due to the constant problem).
2. Specific methods:
Inline int find (int p) {// find the existing point of current ranking p
int l=1;
int r=n;
while(l<=r){
int mid=(l+r)/2;
else l=mid+1;
}
return l;
}

### Example [show2013]shuffle the cards

• This question is still a little too watery as a purple question
• The number given in this problem is medium. First consider simulation.
• Jump back each time the given number of cut cards (the cut cards are the cards at the top of the card). At the same time, if it exceeds n, jump from the beginning. However, because the dealer may cut the complete card many times, it is necessary to take the mold.Reduce workload.
p=(p+r)%(n-i+1)==0?n-i+1:(p+r)%(n-i+1);
\\P means the number of cards at the top of each round, and R means how many cards have been cut

### Little practiceIntervals

• Double experience
• There are many ways to solve this problem. You can use differential constraints, greedy + line segment tree, or greedy + tree array + binary to find space.
• Since the focus of this question is not on the maintenance of data structure, I won’t explain it much and leave it for readers’ consideration.
• $$P.S.$$This question also reminds us that the tree array is just a data structure. It is used to assist, not necessarily the main. There are certainly not as many naked tree array patterns as those secretly stuffed in one place,We should not think about optimizing with data structure just because the focus is not on data structure

## Two dimensional tree array

1. Two dimensional tree array is a tree array set of tree arrays, which is used to solve the problems of single point query, interval modification, single point query and so on in the case of two-dimensional plane.
2. The idea of transferring one-dimensional tree array to two-dimensional tree array is the same as that of transferring one-dimensional array to two-dimensional array, which is solved by two-layer loop.
void add(int x,int y,int c){
for(int i=x;i<=n;i+=i&-i)
for(int j=y;j<=m;j+=j&-j)
sz[i][j]+=c;
}
int ans=0;
for(int i=x;i<=n;i+=i&-i)
for(int j=y;j<=m;j+=j&-j)
sz[i][j]+=c;
return ans;
}

### Little practiceBoard 1Board 2

1. However, if the control range of two-dimensional array is too large, it obviously needs to be discretized. But even if it is discretized, the space has to be open * points. So in some cases we have to press dimension. (of course, line segment trees can also be done, called scanlines)

### Example [Show2007]Gardener’s troubles

1. This problem can’t open a two-dimensional array. It can only be pressed into one dimension.
2. Since there is no modification operation (or the modification operation is separated from the query operation), this question can also (can only) be forced offline, and one query can be divided into four queries.
3. Obviously, when pressed into one dimension, we can only find one dimension$$x$$Prefix and on, another dimension$$y$$It can only be expressed, and what we need to find is the prefix and sum of a point in two dimensions.
4. Obviously, we don’t want the point to be included in the rear position when there is only one dimension, as long as the rear position has not been built! Then press first$$y$$Sort dimensions in ascending order, and then by$$x$$Dimensions are sorted in ascending order and modified in order. (it can be understood as scan line…)
5. We have taken this question offline, and we will sort it together after the inquiry is divided into four. Of course, it must be noted that if the two coordinates of query and modification are the same, modification is preferred.

### Example [sdoi2009]Devout tomb owner

1. This question is very interesting. Because it requires not only prefixes, but also suffixes.

2. However, if we remember the number of each row and column in advance, the suffix will naturally be solved easily.

3. From the data range, it can be clearly seen that it is necessary to discretize + pressure dimension (if you know to use tree array).flee）。

4. But I believe many students see it as an open space and think that there are only a few trees in such a large place. The open space is tens of millions. Won’t it blow up at this time! And it can’t be discretized!Bengbu lives.

5. In fact, important restrictions are ignored, up, down, left and right$$k$$An evergreen tree, that is, only the rows and columns where the open space is locatedYou have to have evergreen trees

6. At the same time, there may be more than$$k$$Tree, just choose it. Obviously, the number of combinations$$\mathrm{C}_n^k$$

7. Then we’ll calculate the answer while scanning. Let the coordinates of the current tree be$$(x,y)$$, the coordinates of the last tree planting are$$(x,y’)$$, then$$y’+1 \to y-1$$Range, left and right$$k$$The number of plans is the same. Then we just need to calculate the range and choose up and down for each position$$k$$The number of schemes of a tree is maintained with a tree array to find intervals and.

8. Therefore, the content maintained by the tree array in this problem is no longer a few trees above, but a scheme tree at each position. Modify the scheme tree at a single point every time you plant a tree.

## [VII] summary

1. Tree array does not belong to the type that cannot be taught. Most deformations can be pushed out in the examination room, which requires your readers to think hard.

2. Most tree arrays can be replaced by line segment trees, and many functions can only be realized by line segment trees. This does not mean that the tree array is dead. It is still active with its beautiful implementation, concise code, small constants and so on.

3. After all, a tree array is just a data structure. Tree array, line segment tree, balanced tree, it’s useless to knock the board. It does not mix with other problems to optimize the algorithm. It is incomplete and incomplete. Can write important, but can do more important.

$$\cal {Made \ By \ YuGe}$$

## SQL exercise 20 – Modeling & Reporting

This blog is used to review and sort out the common topic modeling architecture, analysis oriented architecture and integration topic reports in data warehouse. I have uploaded these reports to GitHub. If you are interested, you can have a lookAddress:https://github.com/nino-laiqiu/TiTanI recorded a relatively complete development process in my hexo blog deployed on GitHub. You can […]