Segment Tree Special-poj 3667 Hotel (interval merge template)

Source: Internet
Author: User

Test instructions: to a N and M, for N rooms, m operations, the type of operation there are 2 kinds, a number of consecutive non-rented rooms have d of the smallest leftmost room number, the other operation of the room number from the X to x+d-1 back.


Analysis: This is a typical application of interval merging, basically as long as the condom template is good


Here is an explanation of the interval merge:

Up function: When the interval of this time is modified, in order to maintain the correctness of the interval value above, the value of the above interval is updated.

Down function: Pass the delay mark and modify the value of the interval below to maintain the correctness interval of the following interval

(It is important to note that the UP function needs to be updated only when it is needed, and the down is added even if it is just a traversal with a deferred tag operation. ) )

Update function: Maintenance of the value of the segment tree after the end of the two operations

Query function: Find the smallest room number that satisfies the condition


In addition, some explanations for this problem:

The lsum array represents the maximum value of the left continuous interval of the RT interval of this segment

The rsum array represents the maximum value of the right continuous interval of the RT interval for this segment

The msum array represents the maximum number of successive intervals in the interval, either left or right, or the intermediate continuous

The lazy array represents the sloth tag, 1 indicates no lazy mark, no need to change, 0 means the interval is used, 1 indicates that the interval is recycled


#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include < algorithm> #include <vector> #pragma comment (linker, "/stack:1024000000,1024000000") using namespace std; const int MAXN = 5e4+5;int Msum[maxn<<2],lsum[maxn<<2],rsum[maxn<<2];int lazy[maxn<<2];void    Up (int l,int r,int RT) {int mid= (L+R) >>1;    lsum[rt]=lsum[rt<<1];    rsum[rt]=rsum[rt<<1|1];    if (lsum[rt<<1]==mid-l+1) lsum[rt]+=lsum[rt<<1|1];    if (Rsum[rt<<1|1]==r-mid) rsum[rt]+=rsum[rt<<1]; Msum[rt]=max (Rsum[rt<<1]+lsum[rt<<1|1],max (msum[rt<<1],msum[rt<<1|1));}        void down (int l,int r,int RT) {if (lazy[rt]!=-1) {int mid= (L+R) >>1;        lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];        msum[rt<<1]=lsum[rt<<1]=rsum[rt<<1]=lazy[rt]?mid-l+1:0;        msum[rt<<1|1]=lsum[rt<<1|1]=rsum[rt<<1|1]=lazy[rt]?r-mid:0;   Lazy[rt]=-1; }}void Build (int l,int R,int RT) {lazy[rt]=-1;    msum[rt]=lsum[rt]=rsum[rt]=r-l+1;    if (l==r) return;    int mid= (L+R) >>1;    Build (l,mid,rt<<1); Build (mid+1,r,rt<<1|1);}        void update (int l,int r,int k,int l,int r,int RT) {if (l<=l&&r<=r) {lazy[rt]=k;        msum[rt]=lsum[rt]=rsum[rt]=k?r-l+1:0;    Return    } down (L,R,RT);    int mid= (L+R) >>1;    if (l<=mid) update (L,R,K,L,MID,RT&LT;&LT;1);    if (r>mid) update (L,R,K,MID+1,R,RT&LT;&LT;1|1); Up (L,R,RT);}    int query (int len,int l,int R,int rt) {if (l==r) return l;    Down (L,R,RT);    int mid= (L+R) >>1;    if (Msum[rt<<1]>=len) return query (len,l,mid,rt<<1);    else if (Rsum[rt<<1]+lsum[rt<<1|1]>=len) return mid-rsum[rt<<1]+1; else return query (len,mid+1,r,rt<<1|1);}    int main () {int n,m;    scanf ("%d%d", &n,&m);    Build (1,n,1);        while (m--) {int k,x,d;        scanf ("%d", &k);           if (k==1) { scanf ("%d", &d);            if (d>msum[1]) printf ("0\n");                else{int l=query (d,1,n,1);                printf ("%d\n", L);            Update (l,l+d-1,0,1,n,1);            }} else {scanf ("%d%d", &x,&d);        Update (x,x+d-1,1,1,n,1); }    }}





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Segment Tree Special-poj 3667 Hotel (interval merge template)

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.