#POJ 3667 Hotel 【多重线段树、内存分配问题】

题目:

Hotel
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 15276 Accepted: 6609

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

Source


思路: 题意大致为,给定长度为 N 的空房间,存在两种操作,
第一种  递交一个申请,长度为 X ,要求从房间序列中选出第一个连续,且长度大于等于 X 的空房间分配给用户 , 并返回第一个房间的编号。
第二种  递交两个位置,X,Y , 将 编号从X 到 Y的房间清空。

因为取值区间及Query数量都较大(5*10^4),故暴力模拟必然超时。

分析需求得出,所求的是 第一个 连续长度大于等于 X 的序列首位置

根据分析,贪心可得出,一个连续的空房间,不可能有某种操作将其从中间打断,即,对于一个连续的空房间串,对房间的占用一定是从首位置开始的。

那么可以维护一颗线段树,例,对于以下的房间情况:

010030000

对应线段树为下方,即 以该点为起点,最长能满足的连续空间。线段树的父节点存储两个子节点的max,在搜索时,先搜左侧,若左侧没有再搜右侧,能在(log)复杂度内搜索到结果。

对应删除时,可能会有多种情况,需要建立多颗线段树解决,
1,删除的区间前后都没有连续的空房间
2,删除的区间前有空房间
3,删除的区间后有空房间
4,删除的区间前后都有空房间

故,建立一个线段树用于查询某区间内满房间的数量, 数组内若房间内为空,则置入0,否则置入1。父节点为子节点之和。
在(log)复杂度内查询区间内有多少满房间。然后在其之前的第一个非空房间(第三棵树)累加前后空间的值。并将区间内房间置0即可。

#define _CRT_SECURE_NO_WARNINGS

#include <fstream>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <algorithm>

#define MAXN 100001

struct data{
	int empty_room;
	int seg_room;
	int max_con;
}t[2 * MAXN];
int a[2 * MAXN];
int lazy1[2 * MAXN], lazy2[2 * MAXN], lazy3[2 * MAXN];

using namespace std;

void BuildTree(int l, int r, int x, char i){
	if (i == 'e')
	{
		if (l == r)
		{
			t[x].empty_room = a[l];
			return;
		}
		int m = (l + r) >> 1;
		BuildTree(l, m, x << 1, 'e');
		BuildTree(m + 1, r, x << 1 | 1, 'e');
		t[x].empty_room = t[x << 1].empty_room + t[x << 1 | 1].empty_room;
		return;
	}
	if (i == 's')
	{
		if (l == r)
		{
			t[x].seg_room = a[l];
			return;
		}
		int m = (l + r) >> 1;
		BuildTree(l, m, x << 1, 's');
		BuildTree(m + 1, r, x << 1 | 1, 's');
		t[x].seg_room = t[x << 1].seg_room + t[x << 1 | 1].seg_room;
		return;
	}
	if (i == 'm')
	{
		if (l == r)
		{
			t[x].max_con = a[l];
			return;
		}
		int m = (l + r) >> 1;
		BuildTree(l, m, x << 1, 'm');
		BuildTree(m + 1, r, x << 1 | 1, 'm');
		t[x].max_con = max(t[x << 1].max_con, t[x << 1 | 1].max_con);
		return;
	}
}

void PushDown(int l, int r, int x, char i){
	int * auto_lazy = NULL;
	switch (i)
	{
	case 'e':
		auto_lazy = lazy1;
		break;
	case 's':
		auto_lazy = lazy2;
		break;
	case 'm':
		auto_lazy = lazy3;
		break;
	}

	int m = (l + r) >> 1;
	if (auto_lazy[x] != -1){
		switch (i)
		{
		case 'e':
			t[x << 1].empty_room = auto_lazy[x] * (m - l + 1);
			t[x << 1 | 1].empty_room = auto_lazy[x] * (r - m);
			break;
		case 's':
			t[x << 1].seg_room = auto_lazy[x] * (m - l + 1);
			t[x << 1 | 1].seg_room = auto_lazy[x] * (r - m);
			break;
		case 'm':
			t[x << 1].max_con = auto_lazy[x];
			t[x << 1 | 1].max_con = auto_lazy[x];
			break;
		}
		auto_lazy[x << 1 | 1] = auto_lazy[x];
		auto_lazy[x << 1] = auto_lazy[x];
		auto_lazy[x] = -1;
	}
	return;
}

int Query(int L, int R, int l, int r, int x, char i){

	if (i == 'm')
	{
		if (l == r)
		{
			return l;
		}
		PushDown(l, r, x, 'm');
		if (t[x << 1].max_con >= L) return Query(L, R, l, (l + r) / 2, x << 1, i);
		else if (t[x << 1 | 1].max_con >= L) return Query(L, R, (l + r) / 2 + 1, r, x << 1 | 1, i);
		else return 0;
	}

	if (L == l && R == r)
	{
		switch (i)
		{
		case 'e':
			return t[x].empty_room;
			break;
		case 's':
			return t[x].seg_room;
			break;
		}
	}
	PushDown(l, r, x, i);
	int m = (l + r) >> 1;
	if (R <= m)return Query(L, R, l, m, x << 1, i);
	else if (L > m)return Query(L, R, m + 1, r, x << 1 | 1, i);
	else switch (i)
	{
	case 'e':
		return Query(L, m, l, m, x << 1, i) + Query(m + 1, R, m + 1, r, x << 1 | 1, i);
		break;
	case 's':
		return Query(L, m, l, m, x << 1, i) + Query(m + 1, R, m + 1, r, x << 1 | 1, i);
		break;
	}
}


void SegModify(int L, int R, int val, int l, int r, int x, char i){
	int * auto_lazy = NULL;
	switch (i)
	{
	case 'e':
		auto_lazy = lazy1;
		break;
	case 's':
		auto_lazy = lazy2;
		break;
	case 'm':
		auto_lazy = lazy3;
		break;
	}
	int m;
	switch (i)
	{
	case 'e':
		if (l == L&&r == R){
			t[x].empty_room = (R - L + 1)*val;
			auto_lazy[x] = val;
			return;
		}
		PushDown(l, r, x, i);
		m = (l + r) >> 1;
		if (R <= m)SegModify(L, R, val, l, m, x << 1, i);
		else if (L > m)SegModify(L, R, val, m + 1, r, x << 1 | 1, i);
		else {
			SegModify(L, m, val, l, m, x << 1, i);
			SegModify(m + 1, R, val, m + 1, r, x << 1 | 1, i);
		}
		t[x].empty_room = t[x << 1].empty_room + t[x << 1 | 1].empty_room;
		return;
	case 's':
		if (l == L&&r == R){
			t[x].seg_room = (R - L + 1)*val;
			auto_lazy[x] = val;
			return;
		}
		PushDown(l, r, x, i);
		m = (l + r) >> 1;
		if (R <= m)SegModify(L, R, val, l, m, x << 1, i);
		else if (L > m)SegModify(L, R, val, m + 1, r, x << 1 | 1, i);
		else {
			SegModify(L, m, val, l, m, x << 1, i);
			SegModify(m + 1, R, val, m + 1, r, x << 1 | 1, i);
		}

		t[x].seg_room = t[x << 1].seg_room + t[x << 1 | 1].seg_room;
		return;
	case 'm':
		if (l == L&&r == R){
			t[x].max_con = val;
			auto_lazy[x] = val;
			return;
		}
		PushDown(l, r, x, i);
		m = (l + r) >> 1;
		if (R <= m)SegModify(L, R, val, l, m, x << 1, i);
		else if (L > m)SegModify(L, R, val, m + 1, r, x << 1 | 1, i);
		else {
			SegModify(L, m, val, l, m, x << 1, i);
			SegModify(m + 1, R, val, m + 1, r, x << 1 | 1, i);
		}

		t[x].max_con = max(t[x << 1].max_con, t[x << 1 | 1].max_con);
		return;
	}
}


int finds(int pos, int l, int r, int x)
{
	if (t[x].empty_room == (r - l) + 1)
	{
		return 0;
	}
	if (l >= pos)
	{
		return 0;
	}
	if (l == r)
	{
		return l;
	}
	int m = (l + r) / 2;
	PushDown(l, r, x, 'e');
	int k = finds(pos, m + 1, r, x << 1 | 1);
	if (!k)
	{
		return finds(pos, l, m, x << 1);
	}
	else
	{
		return k;
	}
}


int main()
{
	//ifstream fin("3out.txt");


	//freopen("3.txt", "r", stdin);

	int nn, mm;
	while (cin >> nn >> mm)
	{
		int n;
		n = 1;
		while (n <= nn)
		{
			n *= 2;
		}
		memset(t, 0, sizeof(t));
		memset(a, 0, sizeof(a));
		memset(lazy1, -1, sizeof(lazy1));
		memset(lazy2, -1, sizeof(lazy2));
		memset(lazy3, -1, sizeof(lazy3));
		a[1] = nn;
		BuildTree(1, n, 1, 's');
		BuildTree(1, n, 1, 'm');
		for (size_t i = 1; i <= nn; i++)
		{
			a[i] = 1;
		}
		BuildTree(1, n, 1, 'e');



		for (size_t i = 0; i < mm; i++)
		{
			int op;
			scanf("%d", &op);
			if (op == 1)
			{
				int people;
				scanf("%d", &people);
				int pos = Query(people, 0, 1, n, 1, 'm');
				if (!pos)
				{
					//int check;
					//fin >> check;
					//if (check!=0)
					//{
					//	system("pause");
					//	printf("0\n");
					//}
					printf("0\n");
				}
				else
				{
					SegModify(pos, pos + people - 1, 0, 1, n, 1, 'e');
					SegModify(pos + people, pos + people, t[n - 1 + pos].max_con - people, 1, n, 1, 'm');
					SegModify(pos + people, pos + people, t[n - 1 + pos].max_con - people, 1, n, 1, 's');
					SegModify(pos, pos, 0, 1, n, 1, 'm');
					SegModify(pos, pos, 0, 1, n, 1, 's');

					//int check;
					//fin >> check;
					//if (check != pos)
					//{
					//	system("pause");
					//	printf("%d\n",pos);
					//}
					printf("%d\n", pos);
				}
			}
			if (op == 2)
			{
				int pos, people;
				scanf("%d%d", &pos, &people);
				if (pos != 1 && Query(pos - 1, pos - 1, 1, n, 1, 'e') == 1)
				{
					int x;
					x = finds(pos, 1, n, 1);
					x++;

					people += pos - x;
					pos = x;
				}


				people = min(people, nn - pos + 1);
				int to_empty = -1 * Query(pos, pos + people - 1, 1, n, 1, 'e');
				to_empty += people;
				to_empty += Query(pos, pos + people, 1, n, 1, 's');
				if (to_empty == 0)
				{
					continue;
				}

				SegModify(pos, pos + to_empty - 1, 1, 1, n, 1, 'e');
				SegModify(pos, pos + to_empty - 1, 0, 1, n, 1, 's');
				SegModify(pos, pos + to_empty - 1, 0, 1, n, 1, 'm');
				SegModify(pos, pos, to_empty, 1, n, 1, 's');
				SegModify(pos, pos, to_empty, 1, n, 1, 'm');

			}
		}
		int bes = 0;
	}
	return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值