POJ3667(Hotel)

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

思路

线段树维护区间最长连续子序列模型 + 区间更新 + 左端点查询。通过维护区间最长连续子序列ms,区间最长左起连续子序列ls,区间最长右终点连续子序列rs。

  1. 对于操作1,先去查询s[1].ms是否大于等于数量x,如果满足去查询左端点 l 并且输出,并且更新区间[x,x+l-1]。不满足直接输出0即可。

  2. 对于操作2,直接更新区间[x,x+y-1]。将这个区间清空即可,具体看细节。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
#define lson k<<1,l,m
#define rson k<<1|1,m+1,r
const int maxn = 50005;
struct node{
	int ls;
	int rs;
	int ms;
}s[maxn<<2];
inline void build(int k,int l,int r)
{
	s[k].ls = s[k].rs = s[k].ms = r-l+1;
	if(l == r){
		return ;
	}
	int m = (l + r) >> 1;
	build(lson);
	build(rson);
}
inline void push_up(int k,int l,int r)
{
	int m = (l + r) >> 1;
	s[k].ls = s[k<<1].ls;			//父节点的左连续 == 左孩子的左起点连续
	s[k].rs = s[k<<1|1].rs;			//父节点的右连续 == 右孩子的右终点连续
	//最大是3种情况:左区间最大,右区间最大,左区间的右 + 右区间的左。
	s[k].ms = max(max(s[k<<1].ms,s[k<<1|1].ms),s[k<<1].rs+s[k<<1|1].ls);	
	if(s[k<<1].ms == m-l+1){		//左区间是个满区间,父节点的最大左连续需要加上右区间的左连续
		s[k].ls += s[k<<1|1].ls;
	}	
	if(s[k<<1|1].ms == r-m){		//右区间是个满区间,父节点的最大右连续需要加上左区间的右连续
		s[k].rs += s[k<<1].rs;
	}
}
inline void push_down(int k,int l,int r)
{
	int m = (l + r) >> 1;
	if(s[k].ms == r-l+1){
		s[k<<1].ms = s[k<<1].ls = s[k<<1].rs = m-l+1;
		s[k<<1|1].ms = s[k<<1|1].ls = s[k<<1|1].rs = r-m;
	}
	else if(s[k].ms == 0){
		s[k<<1].ms = s[k<<1].ls = s[k<<1].rs = 0;
		s[k<<1|1].ms = s[k<<1|1].ls = s[k<<1|1].rs = 0;
	}
}
inline void update(int k,int l,int r,int ql,int qr,int op)
{
	if(qr < l || r < ql){
		return ;
	}
	if(ql <= l && r <= qr){
		if(op == 1){
			s[k].ls = s[k].rs = s[k].ms = 0;
		}
		else{
			s[k].ls = s[k].rs = s[k].ms = r-l+1;
		}
		return ;
	}
	push_down(k,l,r);
	int m = (l + r) >> 1;
	update(lson,ql,qr,op);
	update(rson,ql,qr,op);
	push_up(k,l,r);
}
int query(int k,int l,int r,int ans)
{
	if(l == r){
		return s[k].ms;
	}
	int m = (l + r) >> 1;
	push_down(k,l,r);
	if(s[k<<1].ms >= ans){			//完全落在左区间
		return query(lson,ans);
	}
	else if(s[k<<1].rs+s[k<<1|1].ls >= ans){		//跨越左右区间,那么左端点必定在左区间
		return m - s[k<<1].rs + 1;
	}
	else{			//完全落在右区间
		return query(rson,ans);
	}
}
int main()
{
	int n,m;
	while(~scanf("%d%d",&n,&m)){
		build(1,1,n);
		while(m--){
			int x,y,op;
			scanf("%d%d",&op,&x);
			if(op == 1){
				if(s[1].ms < x){		//不满足
					printf("0\n");
				}
				else{
					int l = query(1,1,n,x);		//左端点
					printf("%d\n",l);
					update(1,1,n,l,l+x-1,1);
				}
			}
			else{
				scanf("%d",&y);
				update(1,1,n,x,x+y-1,2);
			}
		}
	}
	return 0;
}

愿你走出半生,归来仍是少年~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值