Poj 3667 Hotel (merge of line tree intervals & splay solution)

Source: Internet
Author: User
Hotel
Time limit:3000 Ms   Memory limit:65536 K
Total submissions:6224   Accepted:2546

Description

The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shores of Lake Superior. bessie, ever the competent travel agent, has named the bullmoose Hotel on famed Cumberland street as their vacation
Residence. This immense hotel hasN(1 ≤N≤ 50,000) rooms all located on the same side of an extremely long hallway (all the better to see the lake, of course ).

The cows and other visitors arrive in groups of sizeDi(1 ≤Di≤ N) and approach the front desk to check in. Each groupIRequests a setDiContiguous rooms from canmuu, the moose staffing
The counter. He assigns them some set of consecutive room numbersR..R+Di-1 if they are available or, if no contiguous set of rooms is available, politely suggests alternate lodging. canmuu always chooses the valueRTo
Be the smallest possible.

Visitors also depart the hotel from groups of contiguous rooms. CheckoutIHas the parametersXIAndDiWhich specify the vacating of roomsXI..XI+Di-1
(1 ≤XIN-Di+ 1). Some (or all) of those rooms might be empty before the checkout.

Your job is to assist canmuu by processingM(1 ≤M<50,000) checkin/Checkout requests. The hotel is initially unoccupied.

Input

* Line 1: two space-separated integers:NAndM
* Lines 2 ..M+ 1: LineI+ 1 contains request expressed as one of two possible formats: (a) two space separated integers representing a check-in request: 1 andDi(B) Three space-separated integers representing a check-out:
2,XI, AndDi

Output

* Lines 1...: For each check-in request, output a single line with a single integerR, The first room in the contiguous sequence of rooms to be occupied. If the request cannot be satisfied, output 0.

Sample Input

10 61 31 31 31 32 5 51 6

Sample output

14705

Source

Usaco 2008 February gold

Question: http://poj.org/problem? Id = 3667

Question: The hotel number is 1 ~ Room N may now have M wave of people renting a house, and every wave of people may have to set a continuous d room. If yes, the number should be as small as possible. If not, say 0, there may also be d people who want to check out. Their room is continuous x ~ X + D-1...

Analysis: It is relatively simple to merge the segments of a line segment tree... First of all, of course, we need to record the longest continuous value of a range. This is a convenient value. Then we need to consider how a range is transferred from its two subintervals, the longest value can certainly make the person with the longest value in the connected value range. Of course, merging the two subintervals may lead to a longer value, and this longer value must be in the middle, we need to record the longest continuous value of the Left subinterval from the right end to the left end, and record the longest continuous value of the right subinterval from the left end to the right end. The new value is the sum of the two values, of course, since we are not sure which interval is the left interval, we need to record these three values for each interval. Of course, there is a delay sign, or we just need to record it.

2012-10-7:

Recently, I have been strengthening the splay. I didn't think how to use the splay. However, at that time, I didn't seem to understand the delay mark of the Line Segment tree. I thought about it today, in fact, you can simply copy the delay mark of the Line Segment tree...

Each node maintains five value ranges,

1. Val whether the current node is marked (room occupied) L

2. Delay mark of dly current node 3

3. Len the current node includes the maximum continuous empty room of the subnode

4. ll the entire subtree of the current node contains the interval represented by itself, starting from the leftmost, the longest continuous empty room

5. The entire subtree of the current node of RL contains the intervals represented by itself, starting from the rightmost and longest consecutive empty rooms.

YY for the rest, huh?

Code:

# Include <cstdio> # include <iostream> # define lson L, M, RT <1 # define rson m + 1, R, RT <1 | 1 using namespace STD; const int Mm = 55555; int dly [mm <2], mlen [mm <2], llen [mm <2], rlen [mm <2]; /** the mlen interval is the longest consecutive, the left side of llen is the longest consecutive, and the right side of rlen is the longest consecutive */void check (int rt, int Val, int Len) {llen [RT] = rlen [RT] = mlen [RT] = (val-1) * Len; dly [RT] = val;} void Pushdown (int rt, int L1, int l2) {check (RT <1, dly [RT], L1); check (RT <1 | 1, dly [RT], L2 ); dly [RT] = 0 ;} Void pushup (int rt, int L1, int l2) {mlen [RT] = max (rlen [RT <1] + llen [RT <1 | 1], max (mlen [RT <1], mlen [RT <1 | 1]); llen [RT] = llen [RT <1], rlen [RT] = rlen [RT <1 | 1]; If (llen [RT]> = L1) llen [RT] + = llen [RT <1 | 1]; If (rlen [RT]> = l2) rlen [RT] + = rlen [RT <1];} void build (int l, int R, int RT) {dly [RT] = 0; if (L = r) {mlen [RT] = llen [RT] = rlen [RT] = 1; return;} int M = (L + r)> 1; build (lson); Build (rson); pushup (RT, M-l + 1, r-m);} void UPD ATA (int l, int R, int Val, int L, int R, int RT) {If (L <= L & R> = r) {check (RT, val, R-l + 1); return;} int M = (L + r)> 1; if (dly [RT]) Pushdown (RT, m-l + 1, r-m); If (L <= m) updata (L, R, Val, lson); If (r> m) updata (L, r, Val, rson); pushup (RT, M-l + 1, r-m);} int query (int s, int L, int R, int RT) {If (llen [RT]> = s) return l; int M = (L + r)> 1, RET; If (dly [RT]) Pushdown (RT, m-l + 1, r-m); If (mlen [RT <1]> = s) ret = query (S, lson ); else if (rlen [RT <1] + L Len [RT <1 | 1]> = s) ret = m-rlen [RT <1] + 1; else ret = query (S, rson ); pushup (RT, M-l + 1, r-m); return ret;} int main () {int I, j, k, n, m; while (~ Scanf ("% d", & N, & M) {build (1, n, 1); While (M --) {scanf ("% d", & K, & I); If (k = 2) {scanf ("% d", & J); updata (I, I + J-1, N, 1);} else if (mlen [1]> = I) {J = query (I, 1, n, 1 ); printf ("% d \ n", J); updata (J, J + I-1, N, 1);} else puts ("0 ");}} return 0 ;}

Splay code:

#include<cstdio>#include<iostream>using namespace std;const int mm=55555;struct SplayTree{    int son[mm][2],far[mm],num[mm],val[mm],len[mm],ll[mm],rl[mm],dly[mm];    int rt,size;    void Link(int x,int y,int c)    {        far[x]=y,son[y][c]=x;    }    void Rotate(int x,int c)    {        int y=far[x];        PushDown(y);        PushDown(x);        Link(x,far[y],son[far[y]][1]==y);        Link(son[x][!c],y,c);        Link(y,x,!c);        PushUp(y);    }    void Splay(int x,int g)    {        for(PushDown(x);far[x]!=g;)        {            int y=far[x],cx=son[y][1]==x,cy=son[far[y]][1]==y;            if(far[y]==g)Rotate(x,cx);            else            {                if(cx==cy)Rotate(y,cy);                else Rotate(x,cx);                Rotate(x,cy);            }        }        PushUp(x);        if(!g)rt=x;    }    int Select(int k,int g)    {        int x=rt;        PushDown(x);        while(num[son[x][0]]!=k)        {            if(num[son[x][0]]>k)x=son[x][0];            else k-=num[son[x][0]]+1,x=son[x][1];            PushDown(x);        }        Splay(x,g);        return x;    }    void NewNode(int y,int &x)    {        x=++size;        far[x]=y,num[x]=1,ll[x]=rl[x]=len[x]=1;        val[x]=dly[x]=son[x][0]=son[x][1]=0;    }    void Make(int l,int r,int &x,int y)    {        if(l>r)return;        int m=(l+r)>>1;        NewNode(y,x);        Make(l,m-1,son[x][0],x);        Make(m+1,r,son[x][1],x);        PushUp(x);    }    void Prepare(int n)    {        NewNode(size=0,rt);        NewNode(rt,son[rt][1]);        val[1]=val[2]=1,len[1]=len[2]=0;        ll[1]=rl[1]=ll[2]=rl[2]=0;        Make(1,n,son[2][0],2);        Splay(2,0);    }    void Check(int x,int d)    {        if(!x)return;        dly[x]=d;        len[x]=ll[x]=rl[x]=(d<0)*num[x];    }    void PushDown(int x)    {        if(!dly[x])return;        val[x]=dly[x]>0?1:0;        Check(son[x][0],dly[x]);        Check(son[x][1],dly[x]);        dly[x]=0;    }    void PushUp(int x)    {        int a=son[x][0],b=son[x][1];        num[x]=1+num[a]+num[b];        len[x]=max(len[a],len[b]);        ll[x]=ll[a],rl[x]=rl[b];        if(!val[x])        {            len[x]=max(len[x],rl[a]+ll[b]+1);            if(len[a]>=num[a])ll[x]+=ll[b]+1;            if(len[b]>=num[b])rl[x]+=rl[a]+1;        }    }    int Come(int a)    {        if(len[rt]<a)return 0;        int x=rt,y,b=a,l=0;        PushDown(x);        while(x)        {            if(len[son[x][0]]>=b)x=son[x][0];            else if(rl[son[x][0]]+!val[x]==b)break;            else            {                if(!val[x]&&rl[son[x][0]]+ll[son[x][1]]+1>=b)b-=rl[son[x][0]]+1;                l+=num[son[x][0]]+1,x=son[x][1];            }            PushDown(x);        }        l+=num[son[x][0]]-a+1;        x=Select(l-1,0);        y=Select(l+a,rt);        Check(son[y][0],1);        Splay(y,0);        return l;    }    void Leave(int x,int a)    {        int l=Select(x-1,0);        int y=Select(x+a,rt);        Check(son[y][0],-1);        Splay(y,0);    }}spt;int i,j,k,n,m;int main(){    while(~scanf("%d%d",&n,&m))    {        spt.Prepare(n);        while(m--)        {            scanf("%d%d",&k,&i);            if(k==1)printf("%d\n",spt.Come(i));            else scanf("%d",&j),spt.Leave(i,j);        }    }    return 0;}

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.