poj 3667 Hotel(此题跟poj 1823有共同点,都属于区间合并问题)

1、http://poj.org/problem?id=3667

2、题目大意:
一个hotel住旅客和走旅客,现在有N间房子,操作命令有两种,1代表要找一个连续空间长度就是1后边跟着的数字,此时要输出这个连续空间的首位置,2代表清空部分房间,2后边跟着两个数n,c,b就代表起始位置,c代表连续c个房子都要清空,详细与poj 1823相同,就是求的有些不同,多了一个查询

3、题目:

Hotel
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 10380 Accepted: 4456

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

 

4、AC代码;

#include<stdio.h>
#include<algorithm>
using namespace std;
#define N 50005
struct node
{
    int l;
    int r;
    int lsum,rsum,msum;
    int flag;
    void cal()
    {
        lsum=rsum=msum=(r-l+1)*flag;
    }
} a[N*4];
void build(int l,int r,int rt)
{
    a[rt].l=l;
    a[rt].r=r;
    a[rt].flag=1;
    a[rt].cal();
    if(l==r)
        return ;
    int m=(l+r)>>1;
    build(l,m,rt<<1);
    build(m+1,r,rt<<1|1);
}
void update(int L,int R,int rt,int flag)
{
    //printf("******%d %d %d %d %d\n",L,R,rt,a[rt].l,a[rt].r);
    if(L<=a[rt].l && R>=a[rt].r)
    {
        a[rt].flag=flag;
        a[rt].cal();
        return ;
    }
    if(a[rt].flag!=-1)
    {
        a[rt<<1].flag=a[rt<<1|1].flag=a[rt].flag;
        a[rt<<1].cal();
        a[rt<<1|1].cal();
        a[rt].flag=-1;
    }
    int m=(a[rt].l+a[rt].r)>>1;
    if(R<=m)
        update(L,R,rt<<1,flag);
    else if(L>m)
        update(L,R,rt<<1|1,flag);
    else
    {
        update(L,m,rt<<1,flag);
        update(m+1,R,rt<<1|1,flag);
    }
    //如果左孩子的左边空的等于线段长度,说明整个左孩子都是空的,
    if(a[rt<<1].lsum==a[rt<<1].r-a[rt<<1].l+1)
        a[rt].lsum=a[rt<<1].lsum+a[rt<<1|1].lsum;
    else
        a[rt].lsum=a[rt<<1].lsum;
    //如果右孩子的右边空的等于线段长度,说明整个右孩子都是空的,
    if(a[rt<<1|1].rsum==a[rt<<1|1].r-a[rt<<1|1].l+1)
        a[rt].rsum=a[rt<<1|1].rsum+a[rt<<1].rsum;
    else
        a[rt].rsum=a[rt<<1|1].rsum;
    //更新中间的值
    a[rt].msum=max(max(a[rt<<1].msum,a[rt<<1|1].msum),a[rt<<1].rsum+a[rt<<1|1].lsum);
}
int query(int rt,int num)
{
    if(a[rt].l==a[rt].r && num==1)
        return a[rt].l;
    if(a[rt].flag!=-1)
    {
        a[rt<<1].flag=a[rt<<1|1].flag=a[rt].flag;
        a[rt<<1].cal();
        a[rt<<1|1].cal();
        a[rt].flag=-1;
    }
    if(a[rt<<1].msum>=num)
        return query(rt<<1,num);
    else if(a[rt<<1].rsum+a[rt<<1|1].lsum>=num)
        return a[rt<<1].r-a[rt<<1].rsum+1;
    else if(a[rt<<1|1].msum>=num)
        return query(rt<<1|1,num);
    else
        return 0;
}
int main()
{
    int n,p,x,b,c;
    scanf("%d%d",&n,&p);
    build(1,n,1);
    for(int i=1; i<=p; i++)
    {
        scanf("%d",&x);
        if(x==1)
        {
            scanf("%d",&c);
            //这里得注意先判断能不能安排,如果不能安排,就不要更新
            if(a[1].msum<c)
                printf("0\n");
            else
            {
                int ans=query(1,c);
                printf("%d\n",ans);
                update(ans,ans+c-1,1,0);
            }
        }
        else if(x==2)
        {
            scanf("%d%d",&b,&c);
            update(b,b+c-1,1,1);
        }
    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值