码迷,mamicode.com
首页 > 其他好文 > 详细

poj3667 Hotel

时间:2015-05-20 14:51:48      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:线段树

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 rto 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

看了别人的思路自己写出来了,感觉真棒!(笑)

这道题属于线段树区间更新,首先维护线段树6个变量l,r,llen,rlen,tlen,mark(l,r表示区间最左和最右的坐标,llen表示从区间的最左端开始向右连续有空位的总个数,rlen表示从区间的最右端开始向左连续有空位的总个数,tlen表示这个区间最大连续空位的个数,mark表示这段区间是否有空位的情况,1表示都占满了,0表示都是空位,-1表示既有空位又有被占的。这里为了提高线段树的时间效率采用了成段更新,即每次不更新到叶节点,当b[i].l==l && b[i].r==r时返回。

每段的tlen:是左右子区间的tlen以及左子树rlen加上右子树llen的最大值。

每段的rlen:是左子树的rlen,如果左子树都是空位,那么再加上右子树llen.

每段的llen:是右子树的llen,如果右子树都是空位,那么再加上左子树llen.

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<string>
using namespace std;
#define maxn 50006
int a[maxn];
struct node{
	int l,r,llen,rlen,tlen,mark;
}b[4*maxn];

void build(int l,int r,int i)
{
	int mid;
	b[i].l=l;b[i].r=r;b[i].mark=0;
	b[i].llen=b[i].rlen=b[i].tlen=r-l+1;
	if(l==r) return;
	mid=(l+r)/2;
	build(l,mid,i*2);
	build(mid+1,r,i*2+1);
}

void update(int l,int r,int mark,int i)
{
	int mid;
	if(b[i].mark==mark)return;
	if(b[i].l==l && b[i].r==r){
		b[i].mark=mark;
		if(mark==0)b[i].llen=b[i].rlen=b[i].tlen=r-l+1;
		else b[i].llen=b[i].rlen=b[i].tlen=0;return;
	}
	if(b[i].mark!=-1){
		b[i*2].mark=b[i*2+1].mark=b[i].mark;
		if(b[i].mark==0){
			b[i*2].llen=b[i*2].rlen=b[i*2].tlen=b[i*2].r-b[i*2].l+1;
			b[i*2+1].llen=b[i*2+1].rlen=b[i*2+1].tlen=b[i*2+1].r-b[i*2+1].l+1;
		}
		else if(b[i].mark==1){
			b[i*2].llen=b[i*2].rlen=b[i*2].tlen=0;
			b[i*2+1].llen=b[i*2+1].rlen=b[i*2+1].tlen=0;
		}
		b[i].mark=-1;
	}
	mid=(b[i].l+b[i].r)/2;
	if(r<=mid)update(l,r,mark,i*2);
	else if(l>mid)update(l,r,mark,i*2+1);
	else{
		update(l,mid,mark,i*2);
		update(mid+1,r,mark,i*2+1);
	}
	int temp=max(b[i*2].tlen,b[i*2+1].tlen);
	b[i].tlen=max(temp,b[i*2].rlen+b[i*2+1].llen);
	b[i].llen=b[i*2].llen;
	if(b[i*2].llen==b[i*2].r-b[i*2].l+1) b[i].llen+=b[i*2+1].llen;
	b[i].rlen=b[i*2+1].rlen;
	if(b[i*2+1].rlen==b[i*2+1].r-b[i*2+1].l+1) b[i].rlen+=b[i*2].rlen;
}

int question(int num,int i)
{
	int mid;
	if(b[i].tlen<num)return 0;
	if(b[i].l==b[i].r && num==1)return b[i].l;
	if(b[i].mark!=-1){
		b[i*2].mark=b[i*2+1].mark=b[i].mark;
		if(b[i].mark==0){
			b[i*2].llen=b[i*2].rlen=b[i*2].tlen=b[i*2].r-b[i*2].l+1;
			b[i*2+1].llen=b[i*2+1].rlen=b[i*2+1].tlen=b[i*2+1].r-b[i*2+1].l+1;
		}
		else if(b[i].mark==1){
			b[i*2].llen=b[i*2].rlen=b[i*2].tlen=0;
			b[i*2+1].llen=b[i*2+1].rlen=b[i*2+1].tlen=0;
		}
	}
	if(b[i].llen>=num)return b[i].l;
	else if(b[i*2].tlen>=num) return question(num,i*2);
	else if(b[i*2].rlen+b[i*2+1].llen>=num)return b[i*2].r-b[i*2].rlen+1;
	else return question(num,i*2+1);
}

int main()
{
	int n,m,i,j,a,b,c,ans;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		build(1,n,1);
		for(i=1;i<=m;i++){
			scanf("%d",&a);
			if(a==1){
				scanf("%d",&b);
				ans=question(b,1);
				printf("%d\n",ans);
				if(ans)update(ans,ans+b-1,1,1);
			}
			else{
				scanf("%d%d",&b,&c);
				update(b,b+c-1,0,1);
			}
		}
	}
	return 0;
}


poj3667 Hotel

标签:线段树

原文地址:http://blog.csdn.net/kirito_acmer/article/details/45869811

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!