POJ 3667 Hotel

本文介绍了一种使用线段树实现的酒店房间分配算法,该算法能够高效处理顾客入住与退房请求,确保连续房间的分配,并通过区间修改与查询操作维持资源状态。

摘要生成于 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 ≤ 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

线段树区间修改+谜之查询。
想到还是比较简单的。
re的同学可以试一下 1 1 1 1
很神奇哦!

#include<iostream>
#include<cstdio>
using namespace std;
const int N=100005;
int n,m;
struct node
{
	int l,r,smx,lmx,rmx,tag;
}a[4*N];
void build(int num,int l,int r)
{
	a[num].l=l,a[num].r=r;
	a[num].tag=-1;
	if(l==r)
	{
		a[num].smx=a[num].lmx=a[num].rmx=1;
		return ;
	}
	int mid=(l+r)/2;
	build(2*num,l,mid),build(2*num+1,mid+1,r);
	a[num].rmx=a[num].lmx=a[num].smx=a[2*num].smx+a[2*num+1].smx;
}
int query(int num,int d)
{
	if(a[2*num].smx>=d)
		return query(2*num,d);
	else if(a[2*num].rmx+a[2*num+1].lmx>=d)
		return a[2*num].r-a[2*num].rmx+1;
	else if(a[2*num+1].smx>=d)
		return query(2*num+1,d);
	else
		return 1;
}
void check(int num)
{
	if(a[num].l!=a[num].r)
	{
		if(a[num].tag==1)
		{
			a[2*num].rmx=a[2*num].lmx=a[2*num].smx=0;
			a[2*num+1].rmx=a[2*num+1].lmx=a[2*num+1].smx=0;
		}
		else
		{
			a[2*num].rmx=a[2*num].lmx=a[2*num].smx=a[2*num].r-a[2*num].l+1;
			a[2*num+1].rmx=a[2*num+1].lmx=a[2*num+1].smx=a[2*num+1].r-a[2*num+1].l+1;
		}
		a[2*num].tag=a[2*num+1].tag=a[num].tag;
	}
	a[num].tag=-1;
}
void change(int num,int l,int r,int x)
{
	if(a[num].l>=l&&a[num].r<=r)
	{
		if(x==1)
			a[num].rmx=a[num].lmx=a[num].smx=0;
		else
			a[num].rmx=a[num].lmx=a[num].smx=a[num].r-a[num].l+1;
		a[num].tag=x;
		return ;
	}
	if(a[num].l>r||a[num].r<l)
		return ;
	if(a[num].tag!=-1)
		check(num);
	change(2*num,l,r,x);
	change(2*num+1,l,r,x);
	a[num].lmx=a[2*num].lmx,a[num].rmx=a[2*num+1].rmx;
	a[num].smx=max(max(a[2*num].smx,a[2*num+1].smx),a[2*num].rmx+a[2*num+1].lmx);
	if(a[2*num].lmx==a[2*num].r-a[2*num].l+1)
		a[num].lmx=a[2*num].lmx+a[2*num+1].lmx;
	if(a[2*num+1].rmx==a[2*num+1].r-a[2*num+1].l+1)
		a[num].rmx=a[2*num].rmx+a[2*num+1].rmx;
}
int main()
{
	scanf("%d%d",&n,&m);
	build(1,1,n);
	while(m--)
	{
		int d,k,x;
		scanf("%d",&k);
		if(k==1)
		{
			scanf("%d",&d);
			if(a[1].smx<d)
				printf("0\n");
			else
			{
				x=query(1,d);
				printf("%d\n",x);
				change(1,x,x+d-1,1);
			}
		}
		if(k==2)
		{
			scanf("%d%d",&x,&d);
			change(1,x,x+d-1,0);
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值