POJ3667--Hotel

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>
using namespace std;
#define maxn 200008
#define lson 2*id,l,mid
#define rson 2*id+1,mid+1,r
int pos;
inline int max(int a,int b,int c)
{
	if(a<b) a=b;
	if(a<c) a=c;
	return a;
}
struct ST
{
	int l,r,len,rlen,llen;
	int val;
}st[4*maxn];
void buildtree(int id,int l,int r)
{
	st[id].l=l;
	st[id].r=r;
	if(l==r)
	{
		st[id].len=st[id].llen=st[id].rlen=1;
		st[id].val=0;
		return;
	}
	int mid=(l+r)>>1;
	buildtree(lson);
	buildtree(rson);
	st[id].len=st[id].llen=st[id].rlen=r-l+1;
	st[id].val=0;
}
void PushDown(int id)
{
	if(st[id].val!=-1)
	{
		st[2*id].val=st[2*id+1].val=st[id].val;
		if(st[id].val)
		{
			st[2*id].len=st[2*id].llen=st[2*id].rlen=0;
			st[2*id+1].len=st[2*id+1].llen=st[2*id+1].rlen=0;
		}
		else 
		{
			st[2*id].len=st[2*id].llen=st[2*id].rlen=st[2*id].r-st[2*id].l+1;
			st[2*id+1].len=st[2*id+1].llen=st[2*id+1].rlen=st[2*id+1].r-st[2*id+1].l+1;
		}
		st[id].val=-1;
	}
}
void PushUp(int id)
{
	st[id].len=max(st[2*id].len,st[2*id+1].len,st[2*id].rlen+st[2*id+1].llen);
	st[id].rlen=(st[2*id+1].rlen==st[2*id+1].r-st[2*id+1].l+1?st[2*id].rlen+st[2*id+1].len:st[2*id+1].rlen);
	st[id].llen=(st[2*id].llen==st[2*id].r-st[2*id].l+1?st[2*id].len+st[2*id+1].llen:st[2*id].llen);
}
void update(int id,int l,int r,int ope)
{
	if(st[id].l==l && st[id].r==r)
	{
		st[id].val=ope;
		if(ope)
		{
			st[id].len=st[id].llen=st[id].rlen=0;
		}
		else st[id].len=st[id].llen=st[id].rlen=r-l+1;
		return;
	}
	PushDown(id);
	if(st[2*id].r >= r)
	{
		update(2*id,l,r,ope);
		PushUp(id);
		return;
	}
	if(st[2*id+1].l <= l)
	{
		update(2*id+1,l,r,ope);
		PushUp(id);
		return;
	}
	update(2*id,l,st[2*id].r,ope);
	update(2*id+1,st[2*id+1].l,r,ope);
	PushUp(id);
}
int query(int id,int l,int r,int cost)
{
	if(l==r)
	{
		return l;
	}
	PushDown(id);
	if(st[2*id].len >= cost)
	{
		return query(2*id,l,st[2*id].r,cost);
	}
	else if(st[2*id].rlen+st[2*id+1].llen >= cost)
	{
		return st[2*id].r-st[2*id].rlen+1;
	}
	else return query(2*id+1,st[2*id+1].l,r,cost);
}
int main()
{
	int n,m;
	while(scanf("%d%d",&n,&m)==2)
	{
		buildtree(1,1,n);
		int ope,u,v;
		for(int i=1;i<=m;i++)
		{
			scanf("%d",&ope);
			if(ope==1)
			{
				scanf("%d",&u);
				if(u>st[1].len)printf("%d\n",0);
				else
				{
					pos=query(1,1,n,u);
					printf("%d\n",pos);
					update(1,pos,pos+u-1,1);
				}
			}
			else 
			{
				scanf("%d%d",&u,&v);
				update(1,u,u+v-1,0);
			}
		}
	}
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值