POJ3667 Hotel

来源:互联网 发布:网络水晶头品牌 编辑:程序博客网 时间:2024/06/05 02:04

Hotel

Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 11154 Accepted: 4822

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 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 value ofr to be the smallest possible.

Visitors also depart the hotel from groups of contiguous rooms. Checkout i has the parameters Xi andDi which specify the vacating of rooms Xi ..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 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 andDi (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 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

题意:大概就是有几个人要住旅馆,要求房间必须是连续的,如果有的话,输出最小的房间编号,没有输出0,一共n个房间,编号1-n。
分析:线段树,区间更新,维护三个变量lsum,msum,rsum,lsum是包含最左边那个点的连续最多的空房,rsum是包含右边那个店的连续最多的空房,msum就是这个区间内最多的连续的空房。然后就是区间合并了。。。。。

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int MAX=50010;struct seg{    int l,r;    int lsum,rsum,msum;    int mark;    int m()    {        return (l+r)>>1;    }}tree[MAX*3];void pushup(int k){    int l_l=tree[2*k].r-tree[2*k].l+1;    int r_l=tree[2*k+1].r-tree[2*k+1].l+1;    tree[k].lsum=tree[2*k].lsum;    if(tree[2*k].lsum==l_l)        tree[k].lsum+=tree[2*k+1].lsum;    tree[k].rsum=tree[2*k+1].rsum;    if(tree[2*k+1].rsum==r_l)        tree[k].rsum+=tree[2*k].rsum;    tree[k].msum=max(max(tree[2*k].msum,tree[2*k+1].msum),tree[2*k].rsum+tree[2*k+1].lsum);}void pushdown(int k){    int ll=tree[2*k].r-tree[2*k].l+1;    int rr=tree[2*k+1].r-tree[2*k+1].l+1;    tree[2*k].mark=tree[2*k+1].mark=tree[k].mark;    tree[k].mark=-1;    if(tree[2*k].mark)        tree[2*k].lsum=tree[2*k].rsum=tree[2*k].msum=0;    else        tree[2*k].lsum=tree[2*k].rsum=tree[2*k].msum=ll;    if(tree[2*k+1].mark)        tree[2*k+1].lsum=tree[2*k+1].rsum=tree[2*k+1].msum=0;    else        tree[2*k+1].lsum=tree[2*k+1].rsum=tree[2*k+1].msum=rr;}void build(int l,int r,int k){    tree[k].l=l;    tree[k].r=r;    tree[k].mark=-1;    tree[k].lsum=tree[k].msum=tree[k].rsum=r-l+1;    if(l==r)        return;    int mid=(l+r)>>1;    build(l,mid,2*k);    build(mid+1,r,2*k+1);    pushup(k);}void update(int l,int r,int c,int k){    if(tree[k].l==l&&tree[k].r==r)    {        tree[k].mark=c;        if(c!=0)            tree[k].lsum=tree[k].rsum=tree[k].msum=0;        else            tree[k].lsum=tree[k].rsum=tree[k].msum=r-l+1;        return;    }    if(tree[k].mark!=-1)        pushdown(k);    int mid=tree[k].m();    if(r<=mid)        update(l,r,c,2*k);    else if(l>mid)        update(l,r,c,2*k+1);    else    {        update(l,mid,c,2*k);        update(mid+1,r,c,2*k+1);    }    pushup(k);}int query(int w,int k){    if(tree[k].l==tree[k].r)        return tree[k].l;    if(tree[k].mark!=-1)        pushdown(k);    int mid=tree[k].m();    if(tree[2*k].msum>=w)        return query(w,2*k);    else if(tree[2*k].rsum+tree[2*k+1].lsum>=w)        return mid-tree[2*k].rsum+1;    return query(w,2*k+1);}int main(){    int n,m,x,y,s;    //freopen("in.txt","r",stdin);    while(scanf("%d%d",&n,&m)==2)    {        build(1,n,1);        while(m--)        {            scanf("%d",&s);            if(s==1)            {                scanf("%d",&x);                if(tree[1].msum<x)                    printf("0\n");                else                {                    int a=query(x,1);                    printf("%d\n",a);                    update(a,a+x-1,1,1);                }            }            else            {                scanf("%d%d",&x,&y);                update(x,x+y-1,0,1);            }        }    }    return 0;}



0 0
原创粉丝点击