Introduction to Algorithms Learning-line tree (2)

Source: Internet
Author: User

Line segment Tree (1) http://www.cnblogs.com/fu11211129/p/4230000.html

1. Dynamic point interpolation and statistics for segment tree applications:

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

The application of Segment tree (1) is the interpolation and statistics of segments, we access one of the cover fields in the segment tree structure, cover equals 0 means that the area represented by the node is not completely overwritten, and cover is greater than or equal to 1 means that the area represented by the node has been completely overwritten. Using a line segment Tree (1) the diagram inside the blog explains:

As shown, the last time we counted is to find the node that cover is not zero, which corresponds to the node [3,4]. [5,6], [2], [4], [5], [7].), but note that [3,4] and [4] and [5,6] and [5] cannot be counted from repetition, so the length of the last statistic is [2]+[3,4]+[5,6]+[7]=6. So the total projection length of four bars on the wall is 6.

Now we can summarize some important parts of the interpolation: first, insert the action, such as inserting the [L, R] segment, the insertion action needs to be recursively inserted from the root node, and the recursive exit is to find a line tree node that can exactly match [L, R], that is to find a[i].left==l and a[ I].right==r, and the A[i].cover value is added at the same time by 1., recursive termination. Then delete the action, assuming that the deletion [L, R], the same is to be removed from the root node, but also to find a recursive [L, R] exactly match the segment tree node A[i], and will a[i]. Cover value minus 1. The last statistic, which is still the statistics of recursion starting from the root node, the recursive exit is either the found node A[i] satisfies a[i].cover>=0, then returns the segment length represented by the node. Either the leaf node (a[i].left==a[i].right) is found, and if the leaf node's cover is not 0, 1 is returned, otherwise zero is returned.

The above is a review of the section insertion, the following is the introduction of dynamic point interpolation and statistics. The so-called Dynamic point interpolation (in fact, I DIY, so I will use the figure to show it, we understand it well)

As shown, 1-8 represents the 8 slots connected to each other and then places any number of balls in any of these grasses, and then lets you find the number of balls in any segment [L, R]. For example, the number of small balls in the [2,3] segment is the number of balls in the 2,[1,6] segment is 6. To solve this problem, you can do well in the way, a method needless to say also know, violence, open an array a, and then update the value of the array: This approach is inefficient when the range of "slots" is too large. So this time the line tree again comes in handy. We still use the cover field to represent the number of small balls that a node shrinks Puma o the range. For example, the root node represents 1-8 slots of the number of balls.

# # # #第一步, create a line segment tree (http://www.cnblogs.com/fu11211129/p/4230000.html inside is introduced, here directly skip)

# # #第二步, Segment tree insertion (point interpolation)

1 voidUpdatepoint (intPosintStep) {2a[step].num++;3     if(A[step].left==a[step].right&&a[step].left==pos)return;//leaf node4     intMid= (a[step].left+a[step].right)/2;5     if(Pos<=mid) Updatepoint (pos,step*2);6     ElseUpdatepoint (pos,step*2+1);7}

We can see that the line segment tree point interpolation action is "all the way interpolation", it is not difficult to understand, such as I placed a ball in the 3rd slot, then obviously, node [3].num=1, but at the same time, node [3,4],[1,2,3,4],[1,2,3,4,5,6,7,8] The NUM value is also 1. So the segment tree point interpolation can understand the memory as: recursive downward interpolation.

# # #第三步, Segment tree statistics

1 intQueryintLintRintStep) {2     if(l==a[step].left&&r==a[step].right) {3         returnA[step].num;4     }5     Else{6         intMid= (a[step].left+a[step].right)/2, ans=0;7         if(R<=mid) Ans+=query (l,r,step*2);8         Else if(l>=mid+1) Ans+=query (l,r,step*2+1);9         Else{TenAns+=query (l,mid,step*2) +query (mid+1, r,step*2+1); One         } A         returnans; -     } -}

As can be seen, segment tree interpolation results are queried according to the section, so the exit of the recursive query is to find the matching node, and then return the corresponding cover value, corresponding code 2-4 lines. If the current node does not match, the recursive "Down" check [6-8], if the band query section in the left and right subtree have child results, then return two sub-results of the sum.

2. The maximum minimum value problem for segment tree applications:

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

1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 Const intMax_size= -;6 Const intinf=1<< -;7 structsegmenttree{8     intlc,rc,mi,mx;9}a[max_size*3];Ten voidBuildtree (intLintRintRT) { OneA[rt].lc=L; AA[rt].rc=R; -A[rt].mi=inf; -a[rt].mx=-inf; the     if(L==R)return; -     intMid= (a[rt].lc+a[rt].rc)/2; -Buildtree (l,mid,rt*2); -Buildtree (mid+1, r,rt*2+1); + } - voidInsertintPosintVintRT) { +     if(pos==a[rt].lc&&a[rt].lc==a[rt].rc) { AA[rt].mi=v; ata[rt].mx=v; -         return; -     }  -     intMid= (a[rt].lc+a[rt].rc)/2; -     if(pos<=mid) Insert (pos,v,rt*2); -     ElseInsert (pos,v,rt*2+1); inA[rt].mi=min (a[rt*2].mi,a[rt*2+1].MI); -A[rt].mx=max (a[rt*2].mx,a[rt*2+1].mx); to } + intQuery_min (intLintRintRT) { -     if(l==a[rt].lc&&r==a[rt].rc)returnA[RT].MI; the     if(a[rt].lc<a[rt].rc) { *         intMid= (a[rt].lc+a[rt].rc)/2, ret; $         if(R<=mid) Ret=query_min (l,r,rt*2);Panax Notoginseng         Else if(l>=mid+1) Ret=query_min (l,r,rt*2+1); -         ElseRet=min (Query_min (l,mid,rt*2), Query_min (mid+1, r,rt*2+1)); the         returnret; +     } A } the intMain () { +     intn,m,q,p,v; -      while(SCANF ("%d", &n)! =EOF) { $Buildtree (1N1); $scanf"%d",&m); -          for(intI=1; i<=m;i++){ -scanf"%d%d",&p,&v); theInsert (P,v,1); -         }Wuyiscanf"%d",&q); the          while(q--){ -             intb; Wuscanf"%d%d",&a,&b); -printf"%d\n", Query_min (A, B,1)); About         } $     } -}

Introduction to Algorithms Learning-line tree (2)

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.