POJ-3667 Hotel

Hotel
Time Limit: 3000MS Memory Limit: 65536K
   

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

————————————————————集训27.3的分割线————————————————————

思路:线段树上区间存放的是该区间连续空余的最大长度。

每次进来一群人,就考虑能存放这伙人的区间,一直找到最左的塞进去。但是由于线段树会把区间隔开,如下:

   1~4

1~2   3~4

那么2~3如果是连续空余的怎么办呢?

这个时候就需要用到区间合并的思想。断开的区间能够合并的条件是:左儿子的右端点空余且右儿子的左端点空余。

这时候线段树需要三个值:该区间最大连续空余长度、左起空余长度、右起空余长度。

当然还需要懒惰标记,表示该区间是住满或者全空。

代码如下:

/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <climits>
#include <iostream>
#define INF 0x3f3f3f3f
using namespace std;
/****************************************/
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define mid int m = (l+r) >> 1
#define node int l, int r, int rt
const int N = 5e4+5;
struct Node {
	int cov;
	int maxi, left, right;
}tree[N<<2];

void Down(int rt, int len)
{
	Node &ll = tree[rt<<1], &rr = tree[rt<<1|1], &t = tree[rt];
	if(t.cov != -1) {//住满还是清空
		ll.cov = rr.cov = t.cov;
		ll.maxi = ll.left = ll.right = t.cov ? 0 : (len+1>>1);
		rr.maxi = rr.left = rr.right = t.cov ? 0 : (len>>1);
		t.cov = -1;
	}
}

void Up(int rt, int len)//区间合并
{
	Node &ll = tree[rt<<1], &rr = tree[rt<<1|1], &t = tree[rt];
	t.left = ll.left; 
	t.right = rr.right;
	if(t.left == (len+1>>1)) t.left += rr.left;//左右儿子是连续的
	if(t.right == (len>>1)) t.right += ll.right;
	t.maxi = max(ll.right + rr.left, max(ll.maxi, rr.maxi));//维护该区间最大连续空余长度
}

int query(int d, node)
{
	Node &ll = tree[rt<<1], &rr = tree[rt<<1|1], &t = tree[rt];
	if(l == r) return l;
	Down(rt, r-l+1);//先下放懒惰标记,保证数据正确,再更新
	mid;
	if(ll.maxi >= d) return query(d, lson);//左儿子能容纳
	else if(ll.right + rr.left >= d) return m - ll.right + 1;//左儿子和右儿子区间合并后能容纳,直接返回左儿子的右起区间的起点
	else return query(d, rson);//右儿子能容纳
}

void update(int L, int R, bool op, node)
{
	if(L <= l && r <= R) {
		tree[rt].cov = op;
		tree[rt].maxi = tree[rt].left = tree[rt].right = op ? 0 : r-l+1;//该区间是住满还是清空
		return ;
	}
	Down(rt, r-l+1);//先下放懒惰标记,保证数据正确,再更新
	mid;
	if(L <= m) update(L, R, op, lson);
	if(m < R) update(L, R, op, rson);
	Up(rt, r-l+1);//更新完毕要向上推
}

void build(node)
{
	tree[rt].maxi = tree[rt].left = tree[rt].right = r-l+1;
	tree[rt].cov = -1;//初始化懒惰标记
	if(l == r) return ;
	mid;
	build(lson); build(rson);
}

int main()
{
#ifdef J_Sure
//	freopen("000.in", "r", stdin);
//	freopen(".out", "w", stdout);
#endif
	int n, m;
	scanf("%d%d", &n, &m);
	build(1, n, 1);
	int L, R, d;
	while(m--) {
		int op;
		scanf("%d", &op);
		if(op == 1) {
			scanf("%d", &d);
			if(tree[1].maxi < d) puts("0");
			else {
				int pos = query(d, 1, n, 1);
				printf("%d\n", pos);
				update(pos, pos+d-1, true, 1, n, 1);
			}
		}
		else {
			scanf("%d%d", &L, &R);
			R = L+R-1;
			update(L, R, false, 1, n, 1);
		}
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值