Line Segment tree

Source: Internet
Author: User

Different from the BST, a line segment tree maintains the interval information. The lower the height of the tree, the larger the range, and the last layer is the single point information. The value of a line segment tree lies in its maintenance interval information. If it cannot be effectively used, a line segment tree is a waste tree.

 

I. Single Point update

The line segment tree is divided into single-point update and segment update based on the node update method. Single point update is the simplest structure of a line segment tree. Usually consists of four operations: push_up, build, update, and query. The push_up code is the simplest, but it is the core of the Line Segment tree. It updates up the range information. In other words, if effective push_up is not designed, the line segment tree is a waste tree.

 
#include "iostream"#include "string"#include "vector"#include "cstring"#include "fstream"#include "cstdio"using namespace std;#define M 50001#define lson l,mid,root<<1#define rson mid+1,r,root<<1|1int sum[M<<2];void PushUp(int root){    sum[root]=sum[root<<1]+sum[root<<1|1];}void build(int l,int r,int root){    if(l==r)    {        scanf("%d",&sum[root]);        return;    }    int mid=(l+r)>>1;    build(lson);    build(rson);    PushUp(root);}void update(int p,int value,int l,int r,int root){    if(l==r)    {        sum[root]=value;        return;    }    int mid=(l+r)>>1;    if(p<=mid) update(p,value,lson);    else update(p,value,rson);    PushUp(root);}int qurry(int L,int R,int l,int r,int root){    if(L<=l&&r<=R) return sum[root];    int mid=(l+r)>>1;    int ret=0;    if(L<=mid) ret+=qurry(L,R,lson);    if(R>mid) ret+=qurry(L,R,rson);    return ret;}
Basic operations for Single Point update

The above line segment tree style comes from the HDU's God called notonlysuccess. His line segment tree summary is well written, but the personal homepage often hangs. Orz.

In the age of mass data, line tree must first discard the bad habit of using cin. In Windows, the CIN time for non-sync operations is 10 times that of scanf, and 2 ~ 3 times. 100% times out. If sync is disabled in Linux, CIN is faster than scanf.

The push_up operation can be roughly divided into the following categories.

1. interval summation
 sum[root]=sum[root<<1]+sum[root<<1|1]
 

@ Exercise questions

HDU 1166, the question of the sum template of the Line Segment tree, which can be queried after update.

Xcoj 1019, castrated the update line segment tree, in fact, it is better to use a tree array. There are duplicates in the pitfall.

 

2. interval rmq (maximum range query)

RMQ[root]=max(RMQ[root<<1],RMQ[root<<1|1]);

 

@ Exercise questions

HDU 1754, line tree rmq template question, query operation remember to change.
HDU 2795 is an interesting question. It uses rmq to store things. If the size is less than or equal to the maximum value in the left subtree interval, the data goes to the left. The left subtree cannot be placed. Consider the right subtree. The highlight is the integration of update and query, which is also a major difficulty of the non-bare line segment tree. We generally think that this is the line segment tree. First, check whether there are obvious updates and queries, however, this question is hidden. 3. interval overwrite (hash) is a special class problem. push_up is not required, but it depends on the discrete data structure.
@ Exercise question poj 2828: queue insertion problem. Maintain the remaining capacity in one interval. In order, the position after the team is added (the original position may be included in the team ). I thought about it in sequence. If there was someone at that position, the original person would be moved back, But what if there was someone next? The entire post-shift is required, but if there is a vacant space and it cannot be a whole post-shift, it is too troublesome to fill out the vacant space. Then, let's look at the problem solution to understand that it should be processed in reverse order. After reverse order, update. As long as there is no overlap, it is the final position. If there is overlap, use the remaining capacity of the pos-left and right intervals to determine the new empty position. Because, in reverse order, as long as the position is empty, it is the final position, and there is no need to consider the adjacent point offset. During the maintenance (update) interval capacity,-1 is required each time to a range (100% will be inserted in this range ). Due to the reverse order, the position of each insertion should be recorded and output in the final order. 2. The update operation for segment update has changed, and the specified L. R interval is updated. Because the number of update points may be dozens of times the number of single-point updates, if the bottom-up push_up is adopted for each point, the update will inevitably end when the update volume is too large. So the lazy idea is used for segment update, that is, it is not updated for the time being. It makes a tag that can be superimposed and updated, and then it is updated when it has to be updated, however, this operation is called push_down. The use of lazy tags makes the structure of the Line Segment tree very complex, especially when there are multiple operations (such as addition, subtraction, multiplication, division, and mixing). Because of the Operation merging problems, it is difficult to design lazy tags.
Void build (int l, int R, int root) {Col [root] =-1; // multiple groups of data, marked to clear} void Pushdown (INT root, int m) {If (COL [root]! =-1) {Col [root <1] = Col [root <1 | 1] = Col [root]; sum [root <1] = Col [root] * (m-(M> 1 )); sum [root <1 | 1] = Col [root] * (M> 1); Col [root] =-1 ;}} void Update (INT l, int R, int C, int L, int R, int root) {If (L <= L & R <= r) {Col [root] = C; sum [root] = C * (R-l + 1); return;} Pushdown (root, R-l + 1); int mid = (L + r)> 1; if (L <= mid) Update (L, R, C, lson); If (r> mid) Update (L, R, C, rson ); pushup (Root);} int query (int l, int R, int L, int R, int root) {If (L <= L & R <= r) return sum [root]; Pushdown (root, R-l + 1); // The Pushdown here is very important // The Same Below spof}
Basic operations for segment update 1. interval summation

@ Exercise questions HDU 1698. The sum is bare. Pay attention to calculating the entire range when push_down and update modify sum [root.
In poj 3468, the lazy tag changes from overwrite to overlay. Note that + = Col [root] is used when the Pushdown submark is left and right, instead of overwrite. The same applies to sum.
Xcoj 1025, Anhui oi province, and the operators with different priorities bring great trouble to Pushdown this question. The whole process of MOD makes people suffer. First of all, the multiplication mark Mul has a higher priority than the addition mark add, so it must be the sum [root] = sum [root] * Mul [root] + Add [root] * range, when updating the mark between the left and right of ADD, multiply the mark by Mul and add it. when updating the mark, multiply it directly. Mul flag resetting is 1, add flag resetting is 0 2. range coverage (hash) @ exercise question poj2528. First of all, we must know that the leaf node capacity of the regular line segment tree is about 10 ^ 6, which reaches 10 ^ 7, therefore, we need to "compress" discrete intervals ".

In layman's terms, discretization is the compression interval, which maps the original long interval to the new short interval, but the coverage relationship before and after the compression remains unchanged. For example:

There is a number axis from 1 to 10 (the length is 9). Given four intervals [2, 4] [3, 6] [8, 10] [6, 9], the overwrite relationship means that the latter overwrites the former, the color of each interval is 1 2 3 4 in sequence.

Now we extract the eight endpoints of the four intervals, 2 4 3 6 8 6 9

Delete the same endpoint. If the same endpoint is 6, the remaining two are 4 3 6 8 10 9.

Sort them in ascending order. The values are 2, 3, 4, 6, 8, 9, and 10.

Then create a ing

2 3 4 6 8 9 10

When there are too many other users

1 2 3 4 5 6 7

The new four intervals are [1, 3] [2, 4] [5, 7] [4, 6], and the overwrite relationship is not changed. The new number axis is 1 to 7, that is, the length of the original number axis is compressed from 9 to 6. Obviously, the line segment tree of [] is more space-saving than the line segment tree, the search speed is faster, but the results are consistent.

Note that the same endpoint must be removed before sorting. This reduces the number of elements involved in sorting and saves time.


After compressing the interval, update the Region ID of each poster ). However, a lot of modifications were made during query.
void query(int *col,bool *Hash,int l,int r,int root){    if (col[root])    {        if (!Hash[col[root]]) cnt++;        Hash[col[root]] = true;        return;    }    PushDown(col,root,r-l+1);    if (l==r) return ;    int mid = (l + r) >> 1;    query(col,Hash,lson);    query(col,Hash,rson);}
Query operations
Note that the lazy mark retained in the line segment tree is used for hash. First, you must understand that the leaf node must be marked, but if it is only for the leaf node hash, the line segment tree has no meaning. Fortunately, there are a large number of uneliminated line lazy tags on the upper layer of the leaf node. as long as these tags are used, we do not need to hash each leaf node. This is the magic of the line tree.

Line Segment tree

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.