POJ3667---Hotel

Hotel
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 12673   Accepted: 5453

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

线段树区间合并, 查询的时候能往左边走就往左边走


/*************************************************************************
    > File Name: POJ3667.cpp
    > Author: ALex
    > Mail: [email protected] 
    > Created Time: 2015年01月11日 星期日 14时59分18秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 50010;

struct node
{
	int lsum, rsum, sum;
	int l, r;
	int add;
}tree[N << 2];

void pushup (int p)
{
	tree[p].lsum = tree[p << 1].lsum;
	tree[p].rsum = tree[p << 1 | 1].rsum;
	if (tree[p << 1].lsum == tree[p << 1].r - tree[p << 1].l + 1)
	{
		tree[p].lsum += tree[p << 1 | 1].lsum;
	}
	if (tree[p << 1 | 1].rsum == tree[p << 1 | 1].r - tree[p << 1 | 1].l + 1)
	{
		tree[p].rsum += tree[p << 1].rsum;
	}
	tree[p].sum = max(tree[p << 1].rsum + tree[p << 1 | 1].lsum, max(tree[p << 1].sum, tree[p << 1 | 1].sum));
}

void pushdown (int p)
{
	if (tree[p].add != -1)
	{
		if (tree[p].add == 1)
		{
			tree[p << 1].lsum = tree[p << 1].rsum = tree[p << 1].sum = 0;
			tree[p << 1 | 1].lsum = tree[p << 1 | 1].rsum = tree[p << 1 | 1].sum = 0;
		}
		else
		{
			tree[p << 1].lsum = tree[p << 1].rsum = tree[p << 1].sum = tree[p << 1].r - tree[p << 1].l + 1;
			tree[p << 1 | 1].lsum = tree[p << 1 | 1].rsum = tree[p << 1 | 1].sum = tree[p << 1 | 1].r - tree[p << 1 | 1].l + 1;
		}
		tree[p << 1].add = tree[p << 1 | 1].add = tree[p].add;
		tree[p].add = -1;
	}
}

void build (int p, int l, int r)
{
	tree[p].l = l;
	tree[p].r = r;
	tree[p].add = -1;
	tree[p].lsum = tree[p].rsum = tree[p].sum = r - l + 1;
	if (l == r)
	{
		return;
	}
	int mid = (l + r) >> 1;
	build (p << 1, l, mid);
	build (p << 1 | 1, mid + 1, r);
}

void update (int p, int l, int r, int g)
{
	if (l == tree[p].l && tree[p].r == r)
	{
		if (g == 1) //入住
		{
			tree[p].lsum = tree[p].rsum = tree[p].sum = 0;
			tree[p].add = 1;
		}
		else
		{
			tree[p].lsum = tree[p].rsum = tree[p].sum = tree[p].r - tree[p].l + 1;
			tree[p].add = 0;
		}
		return;
	}
	pushdown (p);
	int mid = (tree[p].l + tree[p].r) >> 1;
	if (r <= mid)
	{
		update (p << 1, l, r, g);
	}
	else if (l > mid)
	{
		update (p << 1 | 1, l, r, g);
	}
	else
	{
		update (p << 1, l, mid, g);
		update (p << 1 | 1, mid + 1, r, g);
	}
	pushup (p);
}

int query (int p, int len)
{
	if (tree[p].l == tree[p].r)
	{
		return tree[p].l;
	}
	pushdown (p);
	if (tree[p << 1].sum >= len)
	{
		return query (p << 1, len);
	}
	else if (tree[p << 1].rsum + tree[p << 1 | 1].lsum >= len)
	{
		return tree[p << 1].r - tree[p << 1].rsum + 1;
	}
	else
	{
		return query (p << 1 | 1, len);
	}
}

int main()
{
	int n, m;
	int op, l, r, x;
	while (~scanf("%d%d", &n, &m))
	{
		build (1, 1, n);
		while (m--)
		{
			scanf("%d", &op);
			if (op == 1)
			{
				scanf("%d", &x);
				if(tree[1].sum < x)
				{
					printf("0\n");
				}
				else
				{
					int c = query (1, x);
					printf("%d\n", c);
					update (1, c, c + x - 1, 1);
				}
			}
			else
			{
				scanf("%d%d", &l, &r);
				update (1, l, l + r - 1, 0);
			}
		}
	}
	return 0;
}


你可能感兴趣的:(线段树)