线段树进阶(区间合并)poj3667

 题目

B - Hotel

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

题意:

对于线段树有两种操作,

一是:1 x  表示酒店有x个人来入住,此时要你输出这批旅客所住房间房间号最小的一个,要求是每批旅客住的房间必须是挨着的,房间号尽量小

二是:

2 x y    表示旅客从酒店房间为x号的开始,共有y人离开酒店,其实就是 x,x+1,x+2 .....x+y-1的酒店都为空了

思路:

其实就是利用线段树区间修改,区间查询,其中查询的东西比较复杂,线段树保存的信息也比较多

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<queue>
using namespace std;
using namespace std;
const int maxn=50080;
struct hotel{
	int lmax,rmax,all_max;//lmax分别表示区间的从左开始的最长连续空房间数,rmax从右开始的连续空房间数,all_max表示这个区间的最长连续空房间数
	int lazy;//线段树的懒惰标记
}a[maxn<<2];
void push_up(int l,int r,int root)
{
	a[root].all_max=max(max(a[root<<1].all_max,a[root<<1|1].all_max),a[root<<1].rmax+a[root<<1|1].lmax);
	a[root].lmax=a[root<<1].lmax;
	a[root].rmax=a[root<<1|1].rmax;
	if(a[root<<1].lmax==(l+r)/2-l+1)
	{
		a[root].lmax+=a[root<<1|1].lmax;
	}
	if(a[root<<1|1].rmax==r-(l+r)/2)
	{
		a[root].rmax+=a[root<<1].rmax;
	}
}
void push_down(int root,int len)//ϳÁ±ê¼Ç
{
	if(a[root].lazy!=-1)
	{
		a[root<<1].all_max=a[root<<1].lmax=a[root<<1].rmax=a[root].lazy*((len+1)/2);
		a[root<<1|1].all_max=a[root<<1|1].lmax=a[root<<1|1].rmax=a[root].lazy*(len/2);
		a[root<<1].lazy=a[root<<1|1].lazy=a[root].lazy;
		a[root].lazy=-1;
	}
}
void build(int l,int r,int root)
{
	a[root].all_max=a[root].lmax=a[root].rmax=r-l+1;
	a[root].lazy=-1;
	if(l==r)
	{
		return ;
	}
	int mid=(l+r)>>1;
	build(l,mid,root<<1);
	build(mid+1,r,root<<1|1);
	//push_up(l,r,root);
}
void update(int l,int r,int root,int la,int ra,int val)
{
	if(l>=la&&r<=ra)
	{
		a[root].all_max=val*(r-l+1);
		a[root].lmax=val*(r-l+1);
		a[root].rmax=val*(r-l+1);
		a[root].lazy=val;
		return ;
	}
	push_down(root,r-l+1);
	int mid=(l+r)>>1;
	if(la<=mid)
	{
		update(l,mid,root<<1,la,ra,val);
	}
	if(ra>mid)
	{
		update(mid+1,r,root<<1|1,la,ra,val);
	}
	push_up(l,r,root);
}
int query(int l,int r,int root,int val)
{
	int mid=(l+r)>>1;
	push_down(root,r-l+1);
	if(a[root].lmax>=val)
	{
		return l;
	}
	//if(l==r){
       // return l;
	//}
    //push_down(root,r-l+1);
    else if(a[root<<1].all_max>=val)
	{
		query(l,mid,root<<1,val);
	}
	else if(a[root<<1].rmax+a[root<<1|1].lmax>=val)
	{
		return mid-a[root<<1].rmax+1;
	}
	else{
		query(mid+1,r,root<<1|1,val);
	}
}
int main()

{
	int n,m,a1,a2,a3,i,j;
	scanf("%d%d",&n,&m);
	build(1,n,1);
	for(i=0;i<m;i++)
	{
		scanf("%d",&a1);
		if(a1==1)
		{
			scanf("%d",&a2);
			if(a[1].all_max<a2)
			{
				printf("0\n");
				continue;
			}
			a3=query(1,n,1,a2);
			printf("%d\n",a3);
			update(1,n,1,a3,a3+a2-1,0);

		}
		else{
			scanf("%d%d",&a2,&a3);

				update(1,n,1,a2,a2+a3-1,1);
			}

	}
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值