POJ 3667 (segment tree interval merger) open the room.

Source: Internet
Author: User

http://poj.org/problem?id=3667

The hotel has n room number 1 to n are vacant, then M asked, when the input of the first 1, the representative to live in x consecutive rooms, enter the minimum number of room number, if not

Output 0. When the first number is 2, the room from the X number to the Y number is empty, no output

Related to the interval to think of a line tree can be solved, just like the color of the room to live with the number of non-living mark on the line, the problem is that the length of the interval is not

The specific interval, the processing is more troublesome to find the appropriate interval, so the introduction of LEN1 to indicate the maximum length available for a range, Len2 represents the maximum length available from the left end of the interval,

The LEN3 represents the maximum length available from the right end of the interval, and the last update maintains

Code

1#include <iostream>2#include <cstdio>3 using namespacestd;4 structPoint {5     intL,r;6     intLen1,len2,len3;7     intMark;//lazy Judge8 };9Point tree[50001*4];Ten voidBuildintIintLeftintRight ) One { ATree[i].l=left,tree[i].r=Right ; -tree[i].len1=tree[i].len2=tree[i].len3=tree[i].r-tree[i].l+1;//The length starts with the interval length -tree[i].mark=0; the     if(Left==right)return; -     intMid= (left+right)/2; -Build (i*2, left,mid); -Build (i*2+1, mid+1, right); + } - voidUpdateintIintLeftintRightintval) + { A     if(tree[i].l==left&&tree[i].r==Right ) at     { -         inte; -tree[i].mark=Val; -         if(Val) e=0and//if occupied, the interval length is 0. -         Elsee=tree[i].r-tree[i].l+1; -tree[i].len1=tree[i].len2=tree[i].len3=e; in         return; -     } to     if(tree[i].mark!=-1) +     { -         intq=Tree[i].mark,e,r; thetree[i*2].mark=tree[i*2+1].mark=Tree[i].mark; *tree[i].mark=-1; $         if(q) e=0, r=0;Panax Notoginseng         Else -         { thee=tree[i*2].r-tree[i*2].l+1; +r=tree[i*2+1].r-tree[i*2+1].l+1; A         } thetree[i*2].len1=tree[i*2].len2=tree[i*2].len3=e; +tree[i*2+1].len1=tree[i*2+1].len2=tree[i*2+1].len3=R; -     } $     intMid= (TREE[I].R+TREE[I].L)/2; $     if(left>mid) Update (i*2+1, left,right,val); -     Else if(right<=mid) Update (i*2, left,right,val); -     Else the     { -Update (i*2, left,mid,val);WuyiUpdate (i*2+1, mid+1, right,val); the     } -     intTMP = MAX (tree[i*2].len1,tree[i*2+1].len1); WuTree[i].len1=max (tmp,tree[i*2].len3 + tree[i*2+1].len2); -tree[i].len2=tree[i*2].len2; Abouttree[i].len3=tree[i*2+1].len3; $     if(tree[i*2].len1 = = tree[i*2].r-tree[i*2].l+1)//To maintain the len2 and Len3 of the sub-range -tree[i].len2+=tree[i*2+1].len2; -     if(tree[i*2+1].len1 = = tree[i*2+1].r-tree[i*2+1].l+1) -tree[i].len3+=tree[i*2].len3; A     return ; + } the intFindintIintlen) - { $     if(tree[i].l==tree[i].r&&len==1)returnTREE[I].L; the     if(tree[i].mark!=-1) the     { the         intq=Tree[i].mark,e,r; thetree[i*2].mark=tree[i*2+1].mark=Tree[i].mark; -tree[i].mark=-1; in         if(q) e=0, r=0; the         Else the         { Aboute=tree[i*2].r-tree[i*2].l+1; ther=tree[i*2+1].r-tree[i*2+1].l+1; the         } thetree[i*2].len1=tree[i*2].len2=tree[i*2].len3=e; +tree[i*2+1].len1=tree[i*2+1].len2=tree[i*2+1].len3=R; -     } the     if(tree[i*2].len1>=len)returnFind (i*2, Len);Bayi     Else if(tree[i*2].len3+tree[i*2+1].len2>=len)return(tree[i*2].r-tree[i*2].len3+1); the     Else if(tree[i*2+1].len1>=len)returnFind (i*2+1, Len); the     Else return 0; - } - intMain () the { the     intn,m,x,y,z,w; the      while(~SCANF ("%d%d",&n,&m)) the     { -Build1,1, n); the          while(m--) the         { thescanf"%d",&x);94             if(x==1) the             { thescanf"%d",&y); theW=find (1, y);98printf"%d\n", W); About                 if(w) Update (1, w,w+y-1,1); -             }101             if(x==2)102             {103scanf"%d%d",&y,&z);104Update1, y,y+z-1,0); the             }106         }107     }108     return 0;109}

POJ 3667 (segment tree interval merger) open the room.

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.