天天看点

3667 Hotel

Hotel

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 2523 Accepted: 957

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 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 r to 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
      

Source

USACO 2008 February Gold

#include<stdio.h>

#include<string.h>

#define LL(x)  (x<<1)

#define RR(x)  (x<<1|1)

struct Seg_tree

{

    int l,r,f;

    int cval,lval,rval;//

    void doit() { cval=lval=rval=( f==1? 0 : dis() ); }

    int dis() { return(r-l+1) ;}

    int mid() { return(l+r)/2 ;}

}tree[50010*3];

int n,m;

int max(int a,int b)

{

    if(a>b)  return a;

    return b;

}

void build(int left,int right,int idx)

{

    tree[idx].l=left;

    tree[idx].r=right;

    tree[idx].f=-1;

    tree[idx].doit();

    if(left==right) return ;

    int mid=tree[idx].mid();

    build(left,mid,LL(idx));

    build(mid+1,right,RR(idx));

}

int query(int x,int idx)

{

    if(tree[idx].l==tree[idx].r)

    {

        if(x==1)  return tree[idx].l;//注意若是X=1,这还是成立的

        else   return 0;

    }

    if(tree[idx].f!=-1)

    {

        tree[LL(idx)].f=tree[RR(idx)].f=tree[idx].f;

        tree[LL(idx)].doit();

        tree[RR(idx)].doit();

        tree[idx].f=-1;

    }

    if(tree[LL(idx)].cval>=x)  return query(x,LL(idx));//尽可能左边,极其重要

    else if(tree[LL(idx)].rval+tree[RR(idx)].lval>=x)//分为3种情况

    return tree[LL(idx)].r-tree[LL(idx)].rval+1;

    else if(tree[RR(idx)].cval>=x)  return query(x,RR(idx));

    else  return 0;

}

void update(int left,int right,int flag,int idx)

{

    if(left<=tree[idx].l&&right>=tree[idx].r)

    {

        tree[idx].f=flag;

        tree[idx].doit();

        return ;

    }

    if(tree[idx].f!=-1)

    {

        tree[LL(idx)].f=tree[RR(idx)].f=tree[idx].f;

        tree[LL(idx)].doit();

        tree[RR(idx)].doit();

        tree[idx].f=-1;

    }

    int mid=tree[idx].mid();

    if(left<=mid)  update(left,right,flag,LL(idx));

    if(right>mid)  update(left,right,flag,RR(idx));

    tree[idx].cval=max(tree[LL(idx)].rval+tree[RR(idx)].lval,

                       max(tree[LL(idx)].cval,tree[RR(idx)].cval));

    tree[idx].lval=tree[LL(idx)].lval;

    tree[idx].rval=tree[RR(idx)].rval;

    if( tree[LL(idx)].cval==tree[LL(idx)].dis() ) tree[idx].lval+=tree[RR(idx)].lval;

    if( tree[RR(idx)].cval==tree[RR(idx)].dis() ) tree[idx].rval+=tree[LL(idx)].rval;

}

int main()

{

    //freopen("in.txt","r",stdin);

    //freopen("out.txt","w",stdout);

    while(scanf("%d%d",&n,&m)!=EOF)

    {

        memset(tree,0,sizeof(tree));

        build(1,n,1);

        for(int i=1;i<=m;i++)

        {

            int c;

            scanf("%d",&c);

            if(c==1)

            {

                int x;

                scanf("%d",&x);

                int t=query(x,1);

                printf("%d/n",t);

                if(t!=0) update(t,t+x-1,1,1);

            }

            else

            {

                int x,y;

                scanf("%d%d",&x,&y);

                update(x,x+y-1,0,1);

            }

        }

    }

    return 0;

}

继续阅读