Hotel poj3667 线段树进阶

题目(poj3667

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

题意

奶牛住旅馆,一群住宿的奶牛的房间号必须连着

分析

用线段树来做,与Tunnel Warfare(地道战)有些相似,对于线段树有两种操作,一个是check in
给你的是要住的人数,你要找房间号最小的房间,而且要满足连着的,另一种操作是check out,是给你要离开的房间开始的房间号以及离开的奶牛数
对于check in,我们要在树的节点存储l到r的区间内连着的最长的序列,这就类似求地道中连续最长的序列了,的确思路也是一样的,我们要想知道每个区间的连着的最长的序列,还需要存储的节点信息有lmax,rmax。这是关键。
对于更新操作就与平常的线段树一样了。

代码

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include<stdio.h>
#include<string.h>
#include<limits.h>
#include<stdbool.h>
#include<vector>
#include<queue>
#include<map>
using namespace std;
const int maxn=95005;
int n,m;
struct tree{
	int l_max;//从左开始连续的序列长度(严格左) 
	int r_max;//从右开始连续的序列长度(严格右)//左右均指l,r区间之内的左右 
	int all_max; // l,r区间之内的最长的连续序列 
	int lazy;//lazy下沉标记 -1表示不需下沉,0表示需要更新为0,1表示需要更新为1 
	
}a[maxn<<2];
void push_up(int l,int r,int root)//这是向上更新的 
{
	int mid=(l+r)>>1;
	a[root].l_max=a[root<<1].l_max;
	a[root].r_max=a[root<<1|1].r_max;
	if(a[root<<1].l_max==mid-l+1)
	{
		a[root].l_max+=a[root<<1|1].l_max;
	}
	if(a[root<<1|1].r_max==r-mid-1+1)
	{
		a[root].r_max+=a[root<<1].r_max;
	}
	a[root].all_max=max(max(a[root<<1].all_max,a[root<<1|1].all_max),a[root<<1].r_max+a[root<<1|1].l_max); 
}
void push_down(int l,int r,int root)//下沉标记 
{
	int mid=(l+r)>>1;
	if(a[root].lazy!=-1)
	{
		a[root<<1].lazy=a[root<<1|1].lazy=a[root].lazy;
		a[root<<1].l_max=a[root<<1].r_max=a[root<<1].all_max=(mid-l+1)*a[root].lazy;
		a[root<<1|1].l_max=a[root<<1|1].r_max=a[root<<1|1].all_max=(r-mid)*a[root].lazy;
		a[root].lazy=-1; //莫忘掉 
	}
}
void build(int l,int r,int root)
{
	a[root].all_max=a[root].l_max=a[root].r_max=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);
}
int query(int l,int r,int root,int val)
{
	if(l==r)
	{
		return l;//这句要加上,表示若找到叶子结点的话,我们需要确定答案并返回的 
	 } //注意一定是有答案的,在main函数里是有判断的 
	int mid=(l+r)>>1;
	push_down(l,r,root);
	if(a[root<<1].all_max>=val)//能向左找的尽量向左 
	{
		return query(l,mid,root<<1,val);
	}
	else if(a[root<<1].r_max+a[root<<1|1].l_max>=val)//说明是在中间部分,好好想想树的结构,此时是能确定的 
	{
		return mid-a[root<<1].r_max+1;
	}
	else{//否则向右 
		return query(mid+1,r,root<<1|1,val);
	}
}
void update(int l,int r,int root,int la,int ra,int val)
{
	if(la<=l&&ra>=r)
	{
		a[root].all_max=a[root].l_max=a[root].r_max=(r-l+1)*val;
		a[root].lazy=val;
		return ;
	}
	push_down(l,r,root);//是需要push_down的,因为我们的更新的话是区间上的操作,与地道是有区别的 
	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 main()
{
	int i,j,a1,a2,a3,a4;
	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");
			}
			else{
			a4=query(1,n,1,a2);
			printf("%d\n",a4); 
			update(1,n,1,a4,a4+a2-1,0);}
			
		}
		else{
			scanf("%d%d",&a2,&a3);
			//printf("%d$$$\n",a2);
			update(1,n,1,a2,a2+a3-1,1);
			
		}
	}
	return 0;
 } 

易错点分析

1.当时忘了写push_down函数,因为我们修改的时候都是区间修改的,在用到树的根部的时候很多状态是没有改变的
2.对于查找是关键,先找左面,再找中间,再找右边,为什么只有中间是可以确定具体位置的,因为如果在mid的左面,他有可能在root<<1的mid的左面,也就是能继续往左找,好好想想这棵树
3.对于查找,如果已经找到叶子节点了,这时候不能再往下找了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值