POJ 3667 Hotel (线段树区间合并 )

Language:
Hotel
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 12417   Accepted: 5346

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件连续的房子,開始都为空,有m个操作,每个操作,假设第一个数为1 ,下一个数为le,那么输出长度le的最左边的编号。而且把这le个房间住人(假设没有那么多空房输出0)。假设第一个数为2 后面  le,ri,那么把le 到le+ri-1的房间清空


就是线段树区间合并。须要注意的一个问题我在代码中标出了:


代码:



#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)
using namespace std;

#define N 50005

int n,m;

struct stud{
int le,ri;
int ls,rs,ms;
int cover;
}f[N*4];

void pushdown(int pos)
{
	if(f[pos].cover==-1) return ;

	f[L(pos)].ls=f[L(pos)].rs=f[L(pos)].ms=f[pos].cover ?0:f[L(pos)].ri-f[L(pos)].le+1;
	f[R(pos)].ls=f[R(pos)].rs=f[R(pos)].ms=f[pos].cover ?0:f[R(pos)].ri-f[R(pos)].le+1;

    f[L(pos)].cover=f[R(pos)].cover=f[pos].cover;
    f[pos].cover=-1;
    //唯一须要注意的一点就是把f[pos].cover更改为-1,由于假设不改,下次还会pushdown
//这里它已经把值pushdown了,下次不须要pushdown

}

void pushup(int pos)
{
	f[pos].ls=f[L(pos)].ls;
	f[pos].rs=f[R(pos)].rs;
	f[pos].ms=max(f[L(pos)].ms,f[R(pos)].ms);

	if(f[L(pos)].ls==f[L(pos)].ri-f[L(pos)].le+1)
		f[pos].ls+=f[R(pos)].ls;

	if(f[R(pos)].rs==f[R(pos)].ri-f[R(pos)].le+1)
		f[pos].rs+=f[L(pos)].rs;

	f[pos].ms=max(f[pos].ms,f[L(pos)].rs+f[R(pos)].ls);
}

void build(int pos,int le,int ri)
{
	f[pos].le=le;
	f[pos].ri=ri;
	f[pos].ls=f[pos].rs=f[pos].ms=ri-le+1;
	f[pos].cover=0;
	if(le==ri) return ;

	int mid=MID(le,ri);
	build(L(pos),le,mid);
	build(R(pos),mid+1,ri);

	pushup(pos);
}

void update(int pos,int le,int ri,int va)
{
	if(f[pos].le==le&&f[pos].ri==ri)
	{
		f[pos].ls=f[pos].rs=f[pos].ms=va? 0:ri-le+1;
		f[pos].cover=va;
		return ;
	}

    pushdown(pos);

    int mid=MID(f[pos].le,f[pos].ri);

    if(mid>=ri)
		update(L(pos),le,ri,va);
	else
		if(mid<le)
		update(R(pos),le,ri,va);
	else
	{
		update(L(pos),le,mid,va);
		update(R(pos),mid+1,ri,va);
	}

  pushup(pos);
}

int query(int pos,int le)
{
	if(f[pos].le==f[pos].ri)
		return f[pos].le;

	pushdown(pos);

	int mid=MID(f[pos].le,f[pos].ri);

	if(f[L(pos)].ms>=le)
		return query(L(pos),le);
	else
		if(f[L(pos)].rs+f[R(pos)].ls>=le)
		return mid-f[L(pos)].rs+1;

	return query(R(pos),le);

}

int main()
{
	int i,j,x,le,ri;
	while(~scanf("%d%d",&n,&m))
	{

		build(1,1,n);

		while(m--)
		{
			scanf("%d",&x);
			if(x==1)
			{
				scanf("%d",&le);
				if(f[1].ms<le)
				{
					printf("0\n");
				}
				else
				{
					int ans=query(1,le);
					printf("%d\n",ans);
					update(1,ans,ans+le-1,1);
				}
			}
           else
		   {
		   	scanf("%d%d",&le,&ri);
		   	update(1,le,le+ri-1,0);
		   }
		}
	}
    return 0;
}


posted on 2017-07-25 15:12  ljbguanli  阅读(147)  评论(0编辑  收藏  举报