【POJ - 3667】【Hotel】

本文介绍了一个复杂的酒店预订系统,该系统使用区间合并算法来处理大量游客的连续房间预订和退房请求,确保了房间分配的高效性和准确性。

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

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 rto 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 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

需要处理好的是;两个子区间之间的关系,必须将这个考虑进去。

区间合并的题目,难就难在怎么将他们合并起来,这道题就是要记录一下每次入住多少人,问咱们再来人能不能住进去,如果能输出最小的那个房间号,如果不能 直接输出0,这道题在结构体里开了一个all 用来记录区间剩余的房间数目

ac代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define maxn 500005
using namespace std;

struct node{
	int lt;
	int rt;
	int left;
	int rigt;
	int laz;
	int all;
}tree[maxn];

void change(int cur,int val)
{
	tree[cur].left=(tree[cur].rt-tree[cur].lt+1)*val;
	tree[cur].rigt=(tree[cur].rt-tree[cur].lt+1)*val;
	tree[cur].all=(tree[cur].rt-tree[cur].lt+1)*val;
	return ; 
}
void pushup(int cur)
{
	tree[cur].left=tree[cur<<1].left;
	if(tree[cur<<1].left==(tree[cur<<1].rt-tree[cur<<1].lt+1))
		tree[cur].left+=tree[cur<<1|1].left;
	tree[cur].rigt=tree[cur<<1|1].rigt;
	if(tree[cur<<1|1].rigt==(tree[cur<<1|1].rt-tree[cur<<1|1].lt+1))
		tree[cur].rigt+=tree[cur<<1].rigt;
	tree[cur].all=max(tree[cur<<1].rigt+tree[cur<<1|1].left,max(tree[cur<<1].all,tree[cur<<1|1].all));
	return ;
}
void pushdown(int cur)
{
	if(tree[cur].laz!=-1)
	{
		change(cur<<1,tree[cur].laz);
		change(cur<<1|1,tree[cur].laz);
		tree[cur<<1].laz=tree[cur].laz;
		tree[cur<<1|1].laz=tree[cur].laz;
//		tree[cur<<1].all=tree[cur<<1].left=tree[cur<<1].rigt=tree[cur<<1].laz? 0:(m-(m>>1));
//		tree[cur<<1|1].all=tree[cur<<1|1].left=tree[cur<<1|1].rigt=tree[cur].laz?0:(m>>1);
		tree[cur].laz=-1;
	}
	return ;
}
void build(int l,int r,int cur)
{	
	tree[cur].lt=l;
	tree[cur].rt=r;
	tree[cur].laz=-1;
	if(l==r)
	{
		tree[cur].left=tree[cur].rigt=tree[cur].all=1;
		return ;
	}
	int m=(l+r)>>1;
	build(l,m,cur<<1);
	build(m+1,r,cur<<1|1);
	pushup(cur);
	return;
}

void updata(int l,int r,int val,int cur)
{
	if(l<=tree[cur].lt&&tree[cur].rt<=r)
	{
		change(cur,val);
		tree[cur].laz=val;
		return;
	}
	pushdown(cur);
	if(l<=tree[cur<<1].rt) updata(l,r,val,cur<<1);
	if(r>=tree[cur<<1|1].lt) updata(l,r,val,cur<<1|1);
	pushup(cur);
	return ;	
}
int  query(int  val,int cur)
{
	if(tree[cur].all<val) 
		return -1;
	if(tree[cur].lt==tree[cur].rt)
		return tree[cur].lt;
	pushdown(cur);
	if(tree[cur<<1].all>=val) 
		return query(val,cur<<1);
	else if(tree[cur<<1].rigt+tree[cur<<1|1].left>=val) 
		return tree[cur<<1].rt-tree[cur<<1].rigt+1;
	else 
		return query(val,cur<<1|1);
}


int main()
{
	int n,m;
	int op;
	int a,b;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		build(1,n,1);
		while(m--)
		{
			scanf("%d",&op);
			if(op==1)
			{
				scanf("%d",&a);
				int p=query(a,1);
				if(p!=-1)
				{
					updata(p,p+a-1,0,1);
					printf("%d\n",p);
				}
				else
					printf("0\n");
			}
			else if(op==2)
			{
				scanf("%d%d",&a,&b);
				updata(a,a+b-1,1,1);
			}
		}
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值