POJ3667 Hotel

Hotel

Time Limit: 3000MS Memory Limit: 65536K
Total 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 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 ≤ 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 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

题意:大概就是有几个人要住旅馆,要求房间必须是连续的,如果有的话,输出最小的房间编号,没有输出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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值