poj 3667 Hotel 找最左面连续区间>=need的区间 线段树

本文介绍了一种用于酒店房间分配的有效算法。该算法通过构建一种特殊的数据结构来处理顾客入住和退房请求,确保能快速找到并分配连续空闲房间,同时支持高效地更新房间占用状态。

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

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

//


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=151000;
struct Node
{
    int left,right;
    int cval;//当前段的最长连续区间长度
    int lval;//当前段从左端点开始的最长连续区间长度
    int rval;//当前段从右端点开始的最长连续区间长度
};
Node tree[maxn];


inline int getLen(int id)//当前段的总长度
{
    return tree[id].right-tree[id].left+1;
}
void buildtree(int id,int l,int r)//建树
{
    tree[id].left=l,tree[id].right=r;
    tree[id].cval=tree[id].lval=tree[id].rval=getLen(id);
    if(l!=r)
    {
        int mid=(l+r)>>1;
        buildtree(id<<1,l,mid);
        buildtree((id<<1)|1,mid+1,r);
    }
}
//查看是否存在连续长度为need的区间,若存在返回左端点,否则返回0
int search(int id,int need)
{
    if(tree[id].cval<need) return 0;
    if(tree[id].lval>=need) return tree[id].left;
    if(tree[id<<1].cval>=need) return search(id<<1,need);
    else if(tree[id<<1].rval+tree[(id<<1)|1].lval>=need)
            return tree[id<<1].right-tree[id<<1].rval+1;
    else return search((id<<1)|1,need);
}
//填充或者删除区间:state:0 填充,state:1删除
void update(int id,int l,int r,int state)
{
    if(tree[id].left>=l&&tree[id].right<=r)//当前段在要修改的区间内,不需要再往下修改
    {
        if(!state) tree[id].cval=tree[id].lval=tree[id].rval=0;
        else tree[id].cval=tree[id].lval=tree[id].rval=getLen(id);
        return ;
    }
    //当前段拆开,弥补上一步操作
    if(tree[id].cval==getLen(id))//没有放置东西
    {
        tree[id<<1].cval=tree[id<<1].lval=tree[id<<1].rval=getLen(id<<1);
        tree[(id<<1)|1].cval=tree[(id<<1)|1].lval=tree[(id<<1)|1].rval=getLen((id<<1)|1);
    }
    if(tree[id].cval==0)//东西已经放满当前段
    {
        tree[id<<1].cval=tree[id<<1].lval=tree[id<<1].rval=0;
        tree[(id<<1)|1].cval=tree[(id<<1)|1].lval=tree[(id<<1)|1].rval=0;
    }
    int mid=(tree[id].left+tree[id].right)>>1;
    if(r<=mid) update(id<<1,l,r,state);
    else if(l>=mid+1) update((id<<1)|1,l,r,state);
    else
    {
        update(id<<1,l,mid,state);
        update((id<<1)|1,mid+1,r,state);
    }
    //更新当点段
    tree[id].lval=tree[id<<1].lval;
    if(tree[id].lval==getLen(id<<1)) tree[id].lval+=tree[(id<<1)|1].lval;
    tree[id].rval=tree[(id<<1)|1].rval;
    if(tree[id].rval==getLen((id<<1)|1)) tree[id].rval+=tree[id<<1].rval;
    tree[id].cval=tree[id<<1].rval+tree[(id<<1)|1].lval;
    tree[id].cval=max(tree[id<<1].cval,tree[id].cval);
    tree[id].cval=max(tree[(id<<1)|1].cval,tree[id].cval);
}
int main()
{
    int n,ci;scanf("%d%d",&n,&ci);
    buildtree(1,1,n);
    while(ci--)
    {
        int q;scanf("%d",&q);
        if(q==1)//查询
        {
            int need;
            scanf("%d",&need);
            int r=search(1,need);
            printf("%d\n",r);
            if(r) update(1,r,r+need-1,0);
        }
        else//释放区间
        {
            int l,len;scanf("%d%d",&l,&len);
            int r=l+len-1;
            update(1,l,r,1);
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值