poj 3667 Hotel (线段树 + 合并操作)

poj 3667  Hotel  (线段树 + 合并操作)


                                                                                                                                               
Hotel
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 13372 Accepted: 5793

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






题意:这里有一家旅馆,房间是在单独一行。现在有队伍想住进来,同时要求房间连号,而老板总是会选择从符合条件的连续房间,号码最靠前的给他们。
最开始有n,m两个数,表示有连续的1~n个房间,有m次操作
接下来是两种操作:
如果是1,那么后面跟一个数字a,表示一个队伍要住店,要连续的a间房。
房间能连续,并且尽量靠前的房间,输出头一个房间的编号

如果是2,则表示有一个队伍离开了旅馆,后面跟两个数字a,b,表示从a房间开始,总共有b间连续的房间空了出来



题解:这是一道很经典的线段树合并操作。我们要经常进行信息更新操作,所以在每个结点要保持充足的信息,方便更新父亲结点和孩子结点。
1表示房间有人住
0表示空房间
l,r为该个结点的最左和最右
llen为从最左边开始,最长的连续房间数,如[0,0,1,1,1],则llen为2,如[1,0,0,0,0],则llen为0
rlen为从最右边开始,最长的连续房间数,与上述同理;
tlen为该范围内最长的连续房间数,[0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0],则tlen为4,中间有可能有很多段,找出其中最长的

每次入住房间和退房时,都要更新房间的信息

通过mark来标记,就是线段树的覆盖操作,不断的更新房间的最长个数


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

using namespace std;

#define maxn 50005

struct fun
{
	int l, r;
	int mark;
	int llen, rlen, tlen;
}tree[maxn << 2];                //四倍是很不错的长度

int max (int a, int b)
{
	if (a > b) return a;
	return b;
}

void PushUp (int rt)                    //从下往上更新,从孩子结点中寻找不同的连续房间
{
	tree[rt].llen = tree[rt << 1].llen;                     //如果左孩子的llen最长连续为整个左孩子,那么就加上右孩子的llen
	if (tree[rt << 1].llen == (tree[rt << 1].r - tree[rt << 1].l + 1))
		tree[rt].llen = (tree[rt << 1].llen + tree[rt << 1 | 1].llen);

	tree[rt].rlen = tree[rt << 1 | 1].rlen;              //如果右孩子的rlen最长连续为整个右孩子,那么就加上左孩子的rlen
	if (tree[rt << 1 | 1].rlen == (tree[rt << 1 | 1].r - tree[rt << 1 | 1].l + 1))
		tree[rt].rlen = (tree[rt << 1 | 1].rlen + tree[rt << 1].rlen);

	tree[rt].tlen = max(max (tree[rt << 1].tlen, tree[rt << 1 | 1].tlen), tree[rt << 1].rlen + tree[rt << 1 | 1].llen);   //整个结点最长有三种可能性,选择其中最长的一种
}

void PushDown (int rt)           //从上往下更新,从父亲结点的mark标记中来更新孩子
{
	if (tree[rt].mark != -1)
	{
		tree[rt << 1].mark = tree[rt << 1 | 1].mark = tree[rt].mark;       //将父亲的mark延续到孩子
		tree[rt].mark = -1;               // 释放父亲的标记
		tree[rt << 1].llen = tree[rt << 1].rlen = tree[rt << 1].tlen = tree[rt << 1].mark ? 0 : (tree[rt << 1].r - tree[rt << 1].l + 1);      //因为这个范围都在入住或者退房的范围内,所以该结点要么全空,要么全满
		tree[rt << 1 | 1].llen = tree[rt << 1 | 1].rlen = tree[rt << 1 | 1].tlen = tree[rt << 1 | 1].mark ? 0 : (tree[rt << 1 | 1].r - tree[rt << 1 | 1].l + 1);      //同上述一样
	}
}

void BuildTree (int l, int r, int rt)          //建立线段树,一棵二叉树
{
	int m = (l + r) >> 1;

	tree[rt].l = l;
	tree[rt].r = r;
	tree[rt].llen = tree[rt].rlen = tree[rt].tlen = r - l + 1;
	tree[rt].mark = -1;
	if (l == r) return ;
	BuildTree(l, m, rt << 1);
	BuildTree(m + 1, r, rt << 1 | 1);
	return ;
}
 
void UpData (int L, int R, int v, int rt)               //更新结点的房间信息
{
	if (tree[rt].l >= L && tree[rt].r <= R)          //找到要改变信息的范围结点
	{ 
		tree[rt].mark = v;                         //更新其标记mark,通过之后的PushDown来修改其它结点
		tree[rt].llen = tree[rt].rlen = tree[rt].tlen = tree[rt].mark ? 0:(tree[rt].r - tree[rt].l + 1);      //PushDown不会循环到,所以自己更新信息
		return ;
	}
	PushDown (rt);
	int m = (tree[rt].l + tree[rt].r) >> 1;

	if (L <= m) UpData (L, R, v, rt << 1);
	if (R > m) UpData (L, R, v, rt << 1 | 1);
	PushUp (rt);            //往上的更新
}

int Query (int rt, int len)       //查找符合连续,输出最小的编号
{
	if (tree[rt].l == tree[rt].r)
		return tree[rt].l;
	PushDown (rt);
	if (tree[rt << 1].tlen >= len)        //先比较左孩子是否符合,要是符合就从进入左孩子,不断从左开始考虑
		Query (rt << 1, len);
	else if ((tree[rt << 1].rlen + tree[rt << 1 | 1].llen) >= len)      //要是左孩子的llen+rlen大于要求的长度,怎表面无法再寻找左孩子了,这个就是最前面的点了
		return (tree[rt << 1].r - tree[rt << 1].rlen + 1);             
	else 
		Query (rt << 1|1, len);
}

int main ()
{
	int n, m, i, x, a, b, begin;

	while (scanf ("%d%d", &n, &m) != EOF)
	{
		BuildTree (1, n, 1);
		for (i = 0; i < m; ++i)
		{
			scanf ("%d", &x);
			if (x == 1)
			{
				scanf ("%d", &a);
				if (tree[1].tlen < a)        //如果全部范围内最长的连续房间都小于要求的,则直接输出0 
					printf ("0\n");
				else
				{
					begin = Query (1, a);
					printf ("%d\n", begin);
					UpData (begin, begin + a - 1, 1, 1);     //查找到符合条件的房间后,要进行更新房间信息
				}
			}
			else
			{
				scanf ("%d%d", &a, &b);
				UpData (a, a + b - 1, 0, 1);
			}
		}
	}

	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值