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

Describtion

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

题意:

有一家旅馆,有N个房间,一开始全空,要入住人,输入1为入住,接下来数字为入住人数,要求要为连续房间号,如果有空的连续的房间,输出最左边的房间号,否则输出0,输入2为清空操作,后面两个数字为起始清空的位置和清空的房间数

思路:

线段树的区间合并,真是神题啊; 一开始想的思路是利用线段树存储从左数每一段最长的空房间数量,去寻找满足题意的解,但奈何超时;
这里给出的思路是 保存从左数最长的连续为空房间的数量;从右数最长连续为空房间的数量;如果左边不行就去右边找,或者在两边的结合处(中间位置)存在解;
区间合并也是线段树进阶的操作,听别人讲解后越看越有感觉;

代码给出的注释也很全面;

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
struct node{
	int sum,lsum,rsum;// sum:区间内最长的连续为1 的长度; lsum:从左数最长的连续为1 的长度; rsum: 从右数最长连续为1 的长度; 
	int left,right;// 区间起点和终点; 
	int lazy;// 懒惰标记; 
}a[200100];
int n,m;
void update(int k) //合并区间; 
{
	a[k].lsum=a[k<<1].lsum; //该区间的lsum = 其子树的lsum(都是从左数); 
	a[k].rsum=a[k<<1|1].rsum;//该区间的rsum = 其子树的rsum(都是从右数); 
	if(a[k<<1].sum==a[k<<1].right-a[k<<1].left+1) a[k].lsum+=a[k<<1|1].lsum; //左子树的数量全满,有可能存在右子树的 lsum!=0 ,可连接的情况; 
	if(a[k<<1|1].sum==a[k<<1|1].right-a[k<<1|1].left+1) a[k].rsum+=a[k<<1].rsum; // 右子树的数量全满,有可能存在左子树的 rsum!=0 ,可连接; 
	a[k].sum=max(a[k<<1].rsum+a[k<<1|1].lsum,max(a[k<<1].sum,a[k<<1|1].sum)); //取最大,可连接或不可连接的状态下选择; 
	return ;
}
void pushdown(int k)// 下传函数; 
{
	if(a[k].lazy==-1) return;
	a[k<<1].lsum=a[k<<1].rsum=a[k<<1].sum=(a[k<<1].right-a[k<<1].left+1)*a[k].lazy;
	a[k<<1|1].lsum=a[k<<1|1].rsum=a[k<<1|1].sum=(a[k<<1|1].right-a[k<<1|1].left+1)*a[k].lazy;
	a[k<<1|1].lazy=a[k<<1].lazy=a[k].lazy;
	a[k].lazy=-1;
	return ;
}
void build(int k,int left,int right) //建树; 
{
	a[k].left=left,a[k].right=right;
	a[k].lsum=a[k].rsum=a[k].sum=right-left+1; a[k].lazy=-1;// 注意lazy 要初始化为没用的数,比如-1;因为0,1要用来表示状态; 
	if(left==right) return ;
	int mid=(left+right)>>1;
	build(k<<1,left,mid);// 向左; 
	build(k<<1|1,mid+1,right); // 向右; 
	return ;
}
void change(int k,int left,int right,int x)
{
	if(left<=a[k].left&&a[k].right<=right)
	{
		a[k].lazy=x;
		a[k].sum=a[k].lsum=a[k].rsum=(a[k].right-a[k].left+1)*x;// a[k].right-a[k].left+1; 
		return ;
	}
	pushdown(k);
	int mid=(a[k].left+a[k].right)>>1;
	if(left<=mid) change(k<<1,left,right,x);
	if(mid<right) change(k<<1|1,left,right,x);
	update(k);//更新区间; 
	return ;
}
int query(int k,int x)
{
	if(a[k].left==a[k].right) return a[k].left; //返回左端点; 
	pushdown(k);
	if(a[k<<1].sum>=x) return query(k<<1,x);// 如果左边存在满足题意得的答案,向左递归查找; 
	else if(a[k<<1].rsum+a[k<<1|1].lsum>=x) return a[k<<1].right-a[k<<1].rsum+1;//如果在左子树和右子树的连接处存在满足题意的答案,左端点一定在左子树内, 
	else return query(k<<1|1,x);// 不行的话就去右子树                          // 可计算得到左端点值; 
}
int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		build(1,1,n);
		while(m--)
		{
			int c;
			scanf("%d",&c);
			if(c==1)
			{
				int x;
				scanf("%d",&x);
				if(x>a[1].sum) printf("0\n");//如果没有满足的,直接出0; 
				else
				{
					int ans=query(1,x); //查找左端点; 
					printf("%d\n",ans);
					change(1,ans,ans+x-1,0);//在left(ans)—right(ans+x-1)的区间内更新值,将状态设为0; 
				}
			}
			else
			{
				int x,y;
				scanf("%d%d",&x,&y);
				change(1,x,x+y-1,1);// 更改状态为1; 
			}
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值