POJ 3667 Hotel

本文介绍了一种使用线段树数据结构解决酒店房间预订问题的方法,通过维护连续空闲房间的状态,高效处理入住与退房请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Hotel
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 14702 Accepted: 6356

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 D(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


线段树区间合并
题目大意:有两种操作
1:输入a,查询区间长度为a的空区间是否存在,如果存在则返回区间左端点,然后将此区间置数。
2:输入a,b,将以a为区间左端点,b为长度的区间置空。

方法:开4个数组
left:当前节点线段范围内左起连续为空的区间长度
right:..................................右...................................
       middle:当前节点线段范围内在左右端点不为空的情况下,中间部分为空的区间长度。
status:状态位,-1表示初始化,0表示当前节点为空,1表示当前节点不为空.
 区间合并:
if(left[x]==length-length/2)
        left[x]+=left[x<<1|1];
    if(right[x]==length/2)
        right[x]+=right[x<<1];
    middle[x]=max(right[x<<1]+left[x<<1|1],max(middle[x<<1],middle[x<<1|1]));

当左连续空区间长度等于了左儿子节点区间长度,说明整个左儿子区间都为空,可以与右儿子节点的左连续区间合并
当前节点的左连续空区间长度即为
left[x]+=left[x<<1|1];
同理,右连续空区间长度等于了右儿子节点区间长度,说明整个右儿子区间都为空,可以与左儿子的右连续区间合并
当前节点的右连续空区间长度即为
right[x]+=right[x<<1];
而中间连续的空区间长度,即为max(左儿子单独中间连续空区间长度,右儿子单独中间连续空区间长度,左儿子右连续空区间+右儿子左连续空区间和的长度)。



#include <stdio.h>
#include <algorithm>
using namespace std;
const int MAXN=50005;
int left[MAXN<<2],right[MAXN<<2],middle[MAXN<<2],status[MAXN<<2];

void build(int x,int l,int r)
{
    status[x]=-1;
    left[x]=right[x]=middle[x]=r-l+1;
    if(l==r)
        return;
    int center=(l+r)/2;
    build(x<<1,l,center);
    build(x<<1|1,center+1,r);
}

void pushUp(int x,int length)
{
    left[x]=left[x<<1];
    right[x]=right[x<<1|1];
    if(left[x]==length-length/2)
        left[x]+=left[x<<1|1];
    if(right[x]==length/2)
        right[x]+=right[x<<1];
    middle[x]=max(right[x<<1]+left[x<<1|1],max(middle[x<<1],middle[x<<1|1]));
}

void pushDown(int x,int length)
{
    if(status[x]!=-1)
    {
        status[x<<1]=status[x<<1|1]=status[x];
        left[x<<1]=right[x<<1]=middle[x<<1]=(status[x]?0:length-length/2);
        left[x<<1|1]=right[x<<1|1]=middle[x<<1|1]=(status[x]?0:length/2);
        status[x]=-1;
    }
}

void update(int x,int var,int ll,int rr,int l,int r)
{
        if(ll>=l&&rr<=r)
        {
            left[x]=right[x]=middle[x]=(var?0:rr-ll+1);
            status[x]=var;
            return;
        }
        pushDown(x,rr-ll+1);
        int center=(ll+rr)/2;
        if(center>=l)
            update(x<<1,var,ll,center,l,r);
        if(center<r)
            update(x<<1|1,var,center+1,rr,l,r);
        pushUp(x,rr-ll+1);
}

int query(int x,int l,int r,int key)
{
    if(l==r)
        return l;
    pushDown(x,r-l+1);
    int center=(l+r)/2;
    if(middle[x<<1]>=key)
        return query(x<<1,l,center,key);
    else if((left[x<<1|1]+right[x<<1])>=key)
        return center-right[x<<1]+1;
    else
        return query(x<<1|1,center+1,r,key);
}

int main()
{
    int n,m,type,a,b;
    while(scanf("%d%d",&n,&m)>0)
    {
        build(1,1,n);
        while(m--)
        {
           scanf("%d",&type);
           if(type==1)
           {
                scanf("%d",&a);
                if(middle[1]<a)
                    printf("0\n");
                else
                {
                    int ans=query(1,1,n,a);
                    printf("%d\n",ans);
                    update(1,1,1,n,ans,ans+a-1);
                }
           }
           else
           {
                scanf("%d%d",&a,&b);
                update(1,0,1,n,a,a+b-1);
           }
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值