[USACO08FEB]酒店Hotel(洛谷P2894)

[USACO08FEB]酒店Hotel

题目描述
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.

参考样例,第一行输入n,m ,n代表有n个房间,编号为1—n,开始都为空房,m表示以下有m行操作,以下 每行先输入一个数 i ,表示一种操作:

若i为1,表示查询房间,再输入一个数x,表示在1–n 房间中找到长度为x的连续空房,输出连续x个房间中左端的房间号,尽量让这个房间号最小,若找不到长度为x的连续空房,输出0。

若i为2,表示退房,再输入两个数 x,y 代表 房间号 x—x+y-1 退房,即让房间为空。

输入格式

  • 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

输出格式

  • 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.

输入 #1

10 6
1 3
1 3
1 3
1 3
2 5 5
1 6

输出 #1

1
4
7
0
5

这道题真的有点复杂,细节繁多;但是最坑的是懒标记的运用,之前一直没过也是卡在这里;懒标记千万不要傻傻的弄两个,两个会互相干扰,一个就够了;一个懒标记装两个信息,分别为退房和订房;把这个搞清楚就差不多了,我们只要维护三个东西:
1.区间最大连续空房;
2.区间最大左连续空房;
3.区间最大右连续空房;
然后注意合并过程就行,还有查找过程,每次先往左子树查,找最小左边界;
代码:

#include<bits/stdc++.h>
using namespace std;
int q,x,y;
int ans;
struct Node{
	int r,l,len;//len为区间长度 
	int Rlen,Llen,Zlen;//左边最长,右边最长,总的最长 
	int f;//懒标记 
}tree[200100];
inline void pp(int k){
	tree[k].Llen=tree[k<<1].Llen;
	tree[k].Rlen=tree[k<<1|1].Rlen;
	if(tree[k<<1].Llen==tree[k<<1].len){
		tree[k].Llen=tree[k<<1].len+tree[k<<1|1].Llen;
	}
	if(tree[k<<1|1].Rlen==tree[k<<1|1].len){
		tree[k].Rlen=tree[k<<1|1].len+tree[k<<1].Rlen;
	}
	tree[k].Zlen=max(tree[k<<1].Zlen,tree[k<<1|1].Zlen);
	tree[k].Zlen=max(tree[k].Zlen,tree[k<<1].Rlen+tree[k<<1|1].Llen);
}
inline void pd(int k){
	if(tree[k].f==2){//退房
		tree[k<<1].f=tree[k].f;
		tree[k<<1|1].f=tree[k].f; 
		tree[k<<1].Llen=tree[k<<1].Rlen=tree[k<<1].Zlen=0;
		tree[k<<1|1].Llen=tree[k<<1|1].Rlen=tree[k<<1|1].Zlen=0;
	} 
	if(tree[k].f==1){//订房 
		tree[k<<1].f=tree[k].f;
		tree[k<<1|1].f=tree[k].f;
		tree[k<<1].Llen=tree[k<<1].Rlen=tree[k<<1].Zlen=tree[k<<1].len;
		tree[k<<1|1].Llen=tree[k<<1|1].Rlen=tree[k<<1|1].Zlen=tree[k<<1|1].len;
	}
	tree[k].f=0;
}
inline void build(int k,int ll,int rr){
	tree[k].l=ll,tree[k].r=rr,tree[k].f=0,tree[k].len=rr-ll+1;
	if(ll==rr){
		tree[k].Llen=tree[k].Rlen=tree[k].Zlen=1;
		return;
	}
	int m=(ll+rr)>>1;
	build(k<<1,ll,m);
	build(k<<1|1,m+1,rr);
	pp(k);
}
inline void change1(int k){//订房
	if(tree[k].l>=x&&tree[k].r<=y){
		tree[k].Llen=tree[k].Rlen=tree[k].Zlen=0;
		tree[k].f=2;
		return;
	}
	pd(k);
	int m=(tree[k].l+tree[k].r)>>1;
	if(x<=m) change1(k<<1);
	if(y>m) change1(k<<1|1);
	pp(k);
}
inline void change2(int k){//退房 
	if(tree[k].l>=x&&tree[k].r<=y){
		tree[k].Llen=tree[k].Rlen=tree[k].Zlen=tree[k].len;
		tree[k].f=1;
		return;
	}
	pd(k);
	int m=(tree[k].l+tree[k].r)>>1;
	if(x<=m) change2(k<<1);
	if(y>m) change2(k<<1|1);
	pp(k);
}
inline void ask(int k){
	if(tree[k].l==tree[k].r){
		ans=tree[k].l;
		return;
	}
	pd(k);
	int m=(tree[k].l+tree[k].r)>>1;
	if(tree[k<<1].Zlen>=x){
		ask(k<<1);
	}
	else if(tree[k<<1].Rlen+tree[k<<1|1].Llen>=x){
		ans=m-tree[k<<1].Rlen+1;
		return;
	}
	else if(tree[k<<1|1].Zlen>=x){
		ask(k<<1|1);
	}
}
int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	build(1,1,n);
	while(m--){
		scanf("%d",&q);
		if(q==1){
			ans=0;
			scanf("%d",&x);
			if(tree[1].Zlen<x){
				printf("0\n");
				continue;
			}
			ask(1);
			printf("%d\n",ans);
			y=x+ans-1,x=ans;
			change1(1);//订房 
		}
		else{
			scanf("%d%d",&x,&y);
			y=x+y-1;
			change2(1);//退房 
		}
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值