Introduction to Algorithms Learning-line tree (1)

Source: Internet
Author: User

1. Description of Segment Tree:

---------------

The segment tree is based on the line segment, and each node represents a line segment [A, b]. A segment of length 1 is called a meta segment. A non-tuple segment has two sub-nodes, the left node represents a segment of [A, (A + B)/2], and the right node represents the segment [((A + B)/2) +1,b]. If the number of the meta-segment is I, then his left child is numbered 2*i and the right child is 2*i+1. is a typical line-segment tree. , in general, we set the root node number to 1, so it is clear that [1,5] number is 2,[6,10] Number 3, other nodes and so on.

2. What is the use of segment tree:

-------------------

What do I know? Each node on a line segment tree has at least two fields: Left and right, representing the upper and lower bounds of an interval, respectively. But if this is the case, the line tree is also useless. In fact, when we use line-segment trees, we will add fields to the nodes of the tree depending on the problem. Some of the dynamic maintenance information is stored in these domains, which makes the segment tree extremely flexible and adaptable to different requirements. Let's take a look at the application of a segment tree:

The table was scattered (with several strips of wood, and the rear of the table was a wall. As shown (the virtual divide of the wall into 8 parts). Now a beam of parallel light is coming from the front of the table, and the shadow of the stick is projected onto the wall. What is the total width of the shadow? (It is assumed that the length of the stick is a positive integer, and any of the sticks are just the same on some "consecutive numbers corresponding to the wall", such as the bottom of the stick just corresponds to the digital 2,3,4. The most brute-force approach to this problem is to open an array, for example, to open an array a[1,... 8]. The array is then assigned the initial value a[i] = 0, indicating that the paragraph I wall has no shadow. The value of a array is then modified according to the range of shadows of each bar. For example, for the bottom of the bar, a[2]=a[3]=a[4]=1,1 indicates a shadow. The time complexity of this method is O (n*range). (n indicates the number of wood bars, and range indicates the width of the wall)

Now let's change the line tree to add a domain cover to each node in the segment tree. Cover=1 indicates that the corresponding interval of the node is completely covered, and cover=0 indicates that the corresponding interval of the node is not fully covered. Okay, now the segment tree node has 3 domains: Left,right,cover.

# # #第一步: Creating a line segment tree

1 Const intMax_size=32010;2 structsegmenttree{3     intLeft,right;4     intcover;5}a[max_size*3];6 intCnt[max_size];7 voidBuildtree (intLintRintStep) {8a[step].left=l;9a[step].right=R;TenA[step].cover=0; One     if(L==R)return; A     intMid= (a[step].left+a[step].right)/2; -Buildtree (l,mid,step*2); -Buildtree (mid+1, r,step*2+1); the}

# # #第二步: Perform the Insert action (we sequentially insert the shadow into the line segment tree) with the following code:

1 voidUpdatesegment (intLintRintStep) {2         if(l==a[step].left&&r==a[step].right) {3a[step].cover++;return;4         }5         if(A[step].left==a[step].right)return;6         intMid= (a[step].left+a[step].right)/2;7         if(R<=mid) updatesegment (l,r,step*2);8         Else if(l>=mid+1) Updatesegment (l,r,step*2+1);9         Else{TenUpdatesegment (l,mid,step*2); OneUpdatesegment (mid+1, r,step*2+1); A         } -}

The following four plots are inserted into the wooden bar [2,4], inserting a wooden bar [3,5], inserting a wooden bar [4,6] and inserting a wooden bar [7,7] corresponding to the segment tree state:

# # #第三步: Query (the length of the shaded section in the query segment tree)

1 intCountintStep) {2     if(A[step].cover)returna[step].right-a[step].left+1;3     if(A[step].left==a[step].right) {//leaf node4         if(A[step].cover)return 1;5         Else return 0;6     }7     returnCount (step*2) +count (step*2+1);8}

A cover=1 is labeled above the node to indicate that the shadow covers the area represented by the node. According to figure (d), we can get the final shadow length: [2]*1+[3, 4]*1+[5, 6]*1+[7]*1=1+2*1+2*1+1=6. Because the segment tree is inserted, the query operation takes an O (LG N), so the time complexity has a violent enumeration of O (n^2) down to O (NLGN).

# # #拓展功能: Delete

Back to the original state, what would happen if we took the strips [3,5] off? We found that the final part of the shadow was still 6. 。。。 Here is the code for the delete action:

1 voidDelintLintRintStep) {2     if(L==a[step].left&&r==a[step].right) {//fully cover3a[step].cover--;return;4     }5     if(A[step].left==a[step].right)return;6     intMid= (a[step].left+a[step].right)/2;7     if(R<=mid) del (l,r,step*2);8     Else if(l>=mid+1) del (l,r,step*2+1);9     Else{TenDel (l,mid,step*2); OneDel (mid+1, r,step*2+1); A     } -}

Assuming that we have inserted four bars in the case, the corresponding diagram (d), drawing the wooden bar [3,5], then the state of the segment tree is transferred as follows:

The above is the application of section dynamic modification and query of segment tree, followed by the introduction of other applications of segment tree, to be continue ...

Introduction to Algorithms Learning-line tree (1)

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.