POJ 3667 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 Dicontiguous 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

用线段树统计区间里最长连续相同为0的串的长度,询问的时候找出最左边的点,然后再覆盖上1,清空就覆盖上0

#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define loop(i,j,k) for (int i = j;i != -1; i = k[i])
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
#define fi first
#define se second
#define mp(i,j) make_pair(i,j)
#define pii pair<int,int>
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-4;
const int INF = 0x7FFFFFFF;
const int mod = 9973;
const int N = 2e5 + 10;
int n, m, x, y, z;

int f[N], L[N], R[N], g[N];

void build(int x, int l, int r)
{
	L[x] = R[x] = f[x] = r - l + 1;
	g[x] = -1;
	if (l == r) return;
	int mid = l + r >> 1;
	build(lson);	build(rson);
}

void merge(int x, int l, int r)
{
	int mid = l + r >> 1;
	L[x] = L[x << 1] + (L[x << 1] == mid - l + 1 ? L[x << 1 | 1] : 0);
	R[x] = R[x << 1 | 1] + (R[x << 1 | 1] == r - mid ? R[x << 1] : 0);
	f[x] = max(f[x << 1], f[x << 1 | 1]);
	f[x] = max(f[x], R[x << 1] + L[x << 1 | 1]);
}

void pushdown(int x, int l, int r)
{
	if (l == r) return;
	int mid = l + r >> 1;
	g[x << 1] = g[x << 1 | 1] = g[x];
	if (g[x])
	{
		f[x << 1] = L[x << 1] = R[x << 1] = 0;
		f[x << 1 | 1] = L[x << 1 | 1] = R[x << 1 | 1] = 0;
	}
	else
	{
		f[x << 1] = L[x << 1] = R[x << 1] = mid - l + 1;
		f[x << 1 | 1] = L[x << 1 | 1] = R[x << 1 | 1] = r - mid;
	}
	g[x] = -1;
}

int find(int x, int l, int r, int len)
{
	if (f[x] < len) return 0;
	if (g[x] != -1) pushdown(x, l, r);
	if (l == r) return l;
	int mid = l + r >> 1;
	if (f[x << 1] >= len) return find(lson, len);
	if (R[x << 1] + L[x << 1 | 1] >= len) return mid - R[x << 1] + 1;
	return find(rson, len);
}

void make(int x, int l, int r, int ll, int rr, int v)
{
	if (g[x] != -1) pushdown(x, l, r);
	if (ll <= l&&r <= rr)
	{
		g[x] = v;	
		f[x] = L[x] = R[x] = v ? 0 : r - l + 1;
	}
	else
	{
		int mid = l + r >> 1;
		if (ll <= mid) make(lson, ll, rr, v);
		if (rr > mid) make(rson, ll, rr, v);
		merge(x, l, r);
	}
}

int main()
{
	while (scanf("%d%d", &n, &m) != EOF)
	{
		build(1, 1, n);
		while (m--)
		{
			scanf("%d%d", &x, &y);
			if (x == 1) 
			{
				int k = find(1, 1, n, y);
				printf("%d\n", k);
				if (k) make(1, 1, n, k, k + y - 1, 1);
			}
			else
			{
				scanf("%d", &z);
				make(1, 1, n, y, y + z - 1, 0);
			}
		}
 	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值