1. 程式人生 > >POJ - 3667 Hotel 區間合併

POJ - 3667 Hotel 區間合併

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 has N (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 size Di (1 ≤ Di ≤ N) and approach the front desk to check in. Each group i requests a set of Di

 contiguous rooms from Canmuu, the moose staffing the counter. He assigns them some set of consecutive room numbers r..r+Di-1 if they are available or, if no contiguous set of rooms is available, politely suggests alternate lodging. Canmuu always chooses the value of rto be the smallest possible.

Visitors also depart the hotel from groups of contiguous rooms. Checkout i has the parameters Xi and Di which specify the vacating of rooms Xi ..Xi +Di-1 (1 ≤ Xi ≤ N-Di+1). Some (or all) of those rooms might be empty before the checkout.

Your job is to assist Canmuu by processing M (1 ≤ M < 50,000) checkin/checkout requests. The hotel is initially unoccupied.

Input

* Line 1: Two space-separated integers: N and M
* Lines 2..M+1: Line i+1 contains request expressed as one of two possible formats: (a) Two space separated integers representing a check-in request: 1 and Di (b) Three space-separated integers representing a check-out: 2, Xi, and Di

Output

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

Sample Input

10 6
1 3
1 3
1 3
1 3
2 5 5
1 6

Sample Output

1
4
7
0
5

題解: 1代表查詢連續的一段 2代表退房  算是一個區間合併的模板題吧  查詢的時候區間不符合之間返回就好了

 

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<stack>
using namespace std;
const int maxn=50010;
typedef long long ll;
struct node
{
    int l,r;
    int ml,mr,mx;
    int laz;
}tree[maxn<<2];
int n,m;
void build(int l,int r,int cur)
{
    tree[cur].laz=-1;
    tree[cur].l=l;
    tree[cur].r=r;
    if(l==r)
    {
        tree[cur].ml=tree[cur].mr=tree[cur].mx=1;
        //printf("%d-%d %d\n",l,r,tree[cur].mx);
        return;
    }
    int mid=(r+l)>>1;
    build(l,mid,cur*2);
    build(mid+1,r,cur*2+1);
    tree[cur].mx=max(max(tree[cur*2].mx,tree[cur*2+1].mx),tree[cur*2].mr+tree[cur*2+1].ml);
    tree[cur].ml=tree[cur*2].ml;
    tree[cur].mr=tree[cur*2+1].mr;
    if(tree[cur].ml==(mid-l+1))
        tree[cur].ml+=tree[cur*2+1].ml;
    if(tree[cur].mr==(r-mid))
        tree[cur].mr+=tree[cur*2].mr;
    //printf("%d-%d %d\n",l,r,tree[cur].mx);
}
void pushdown(int cur)
{
    if(tree[cur].laz!=-1)
    {
        tree[cur*2].ml=tree[cur*2].mr=tree[cur*2].mx=(tree[cur*2].r-tree[cur*2].l+1)*tree[cur].laz;
        tree[cur*2+1].ml=tree[cur*2+1].mr=tree[cur*2+1].mx=(tree[cur*2+1].r-tree[cur*2+1].l+1)*tree[cur].laz;
        tree[cur*2].laz=tree[cur*2+1].laz=tree[cur].laz;
        tree[cur].laz=-1;
    }
}
void update(int pl,int pr,int l,int r,int cur,int val)
{
    if(pl<=l&&r<=pr)
    {
        tree[cur].ml=tree[cur].mr=tree[cur].mx=(r-l+1)*val;
        tree[cur].laz=val;
        return;
    }
    pushdown(cur);
    int mid=(r+l)>>1;
    if(pr<=mid) update(pl,pr,l,mid,cur*2,val);
    else if(pl>=mid+1) update(pl,pr,mid+1,r,cur*2+1,val);
    else
    {
        update(pl,pr,l,mid,cur*2,val);
        update(pl,pr,mid+1,r,cur*2+1,val);
    }
    tree[cur].mx=max(max(tree[cur*2].mx,tree[cur*2+1].mx),tree[cur*2].mr+tree[cur*2+1].ml);
    tree[cur].ml=tree[cur*2].ml;
    tree[cur].mr=tree[cur*2+1].mr;
    if(tree[cur].ml==(mid-l+1))
        tree[cur].ml+=tree[cur*2+1].ml;
    if(tree[cur].mr==(r-mid))
        tree[cur].mr+=tree[cur*2].mr;
}
int query(int l,int r,int cur,int num)
{
    if(tree[cur].mx<num) return -1;
    if(l==r) return l;
    pushdown(cur);
    int mid=(r+l)>>1;
    if(tree[cur*2].mx>=num) return query(l,mid,cur*2,num);
    else if(tree[cur*2].mr+tree[cur*2+1].ml>=num) return mid-tree[cur*2].mr+1;
    else if(tree[cur*2+1].mx>=num) return query(mid+1,r,cur*2+1,num);
    else return -1;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        int op;
        int x,l;
        build(1,n,1);
        while(m--)
        {
            scanf("%d",&op);
            if(op==1)
            {
                scanf("%d",&x);
                int ans=query(1,n,1,x);
                if(ans<0) printf("0\n");
                else
                {
                    update(ans,ans+x-1,1,n,1,0);
                    printf("%d\n",ans);
                }
            }
            else
            {
                scanf("%d%d",&l,&x);
                update(l,l+x-1,1,n,1,1);
            }
        }
    }

	return 0;
}