POJ 3667 Hotel

题目链接:http://poj.org/problem?id=3667


Hotel

Time Limit: 3000MS
Memory Limit: 65536K
Total Submissions: 15798
Accepted: 6860

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

Source

USACO 2008 February Gold

思路:比较简单的线段树区间合并问题。每个节点保存的信息是左端点开始连续的空位数,右端点结束连续的空位数,区间最大连续的空位数。更新父节点信息,也即区间合并时,对于左端点开始的空位数,先令其等于左儿子的左端点开始的空位数,再判断其与左儿子的右端点是否相等,相等说明左儿子全是空位,那么它可以向右儿子的左边延伸,即将其加上右儿子的左端点开始的空位数,右端点类似。对于区间最大连续的空位数,它要么是左儿子的区间最大连续的空位数,要么是右儿子的区间最大连续的空位数,要么是两个区间合并时衔接的那一块区域的空位数。查询时,如果左儿子能满足,从左儿子查询,否则判断区间合并时中间的空位数能否满足,能满足的话,答案就是左儿子的右端点向前走到连续空位的第一个位置,不能满足的话,就查找右儿子,因为我们要使得起始位置最靠近左边。详见代码。

附上AC代码:
#include <cstdio>
#include <algorithm>
#define lrt rt<<1
#define rrt rt<<1|1
#define lson l, m, lrt
#define rson m+1, r, rrt
using namespace std;
const int maxn = 50005;
int lsum[maxn<<2], rsum[maxn<<2];
int setv[maxn<<2], msum[maxn<<2];
int n, q;

void push_up(int l, int r, int rt){
	lsum[rt] = lsum[lrt];
	rsum[rt] = rsum[rrt];
	int m = (l+r)>>1;
	if (lsum[rt] == m-l+1)
		lsum[rt] += lsum[rrt];
	if (rsum[rt] == r-m)
		rsum[rt] += rsum[lrt];
	msum[rt] = max(max(msum[lrt], msum[rrt]), lsum[rrt]+rsum[lrt]);
}

void build(int l, int r, int rt){
	lsum[rt] = rsum[rt] = msum[rt] = r-l+1;
	setv[rt] = -1;
	if (l == r)
		return ;
	int m = (l+r)>>1;
	build(lson);
	build(rson);
	push_up(l, r, rt);
}

void push_down(int l, int r, int rt){
	if (setv[rt] != -1){
		setv[lrt] = setv[rrt] = setv[rt];
		int m = (l+r)>>1;
		lsum[lrt] = rsum[lrt] = msum[lrt] = setv[rt] ? m-l+1 : 0;
		lsum[rrt] = rsum[rrt] = msum[rrt] = setv[rt] ? r-m : 0;
		setv[rt] = -1;
	}
}

void update(int cl, int cr, int val, int l, int r, int rt){
	if (cl<=l && r<=cr){
		lsum[rt] = rsum[rt] = msum[rt] = val ? r-l+1 : 0;
		setv[rt] = val;
		return ;
	}
	push_down(l, r, rt);
	int m = (l+r)>>1;
	if (cl <= m)
		update(cl, cr, val, lson);
	if (cr > m)
		update(cl, cr, val, rson);
	push_up(l, r, rt);
}

int queries(int val, int l, int r, int rt){
	if (l == r)
		return l;
	push_down(l, r, rt);
	int m = (l+r)>>1;
	if (msum[lrt] >= val)
		return queries(val, lson);
	if (rsum[lrt]+lsum[rrt] >= val)
		return m-rsum[lrt]+1;
	return queries(val, rson);
}

int main(){
	while (~scanf("%d%d", &n, &q)){
		build(1, n, 1);
		int op, a, b;
		while (q--){
			scanf("%d%d", &op, &a);
			if (op == 1){
				if (msum[1] < a)
					printf("0\n");
				else{
					int t = queries(a, 1, n, 1);
					printf("%d\n", t);
					update(t, t+a-1, 0, 1, n, 1);
				}
			}
			else{
				scanf("%d", &b);
				update(a, a+b-1, 1, 1, n, 1);
			}
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值