Poj 3667——hotel——————【线段树区间合并】
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 13124 | Accepted: 5664 |
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 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-n,有m次询问,如果第一个数字为1,第二个数字为a表示问有没有连续的a间房,如果有,输出这a间房的第一间房编号,如果没有输出0。如果第一个数字为2,第二、三个数字分别为a,b则表示其他游客退房,将从a起的连续b间房退掉。 解题思路:用三个数组分别记录结点左端点起连续的区间长度,结点右端点起连续的区间长度及结点包含的最长的连续区间长度。cover表示各个结点当前的状态。
/*
线段树区间合并:query操作实现查询一段连续区间的左端点
update操作实现更新某段区间
PushUP操作实现合并左右儿子的区间
PushDown操作实现延迟标记向儿子下移
*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define mid (L+R)/2
#define lson rt*2,L,mid
#define rson rt*2+1,mid+1,R
const int maxn=60000;
//分别记录结点左端、右端及结点表示的连续区间长度
int l_sum[maxn*4],r_sum[maxn*4],rt_sum[maxn*4];
int cover[maxn*4];//标记结点状态,空、满、初始值
void build(int rt,int L,int R){
l_sum[rt]=r_sum[rt]=rt_sum[rt]=(R-L+1);
cover[rt]=-1;
if(L==R)
return;
build(lson);
build(rson);
}
void PushUP(int rt,int len){
l_sum[rt]=l_sum[rt*2];
r_sum[rt]=r_sum[rt*2+1];
if(l_sum[rt]==len-(len/2)){
//如果左儿子左端连续区间长度为左儿子结点管辖长度,则说明该结点左端连续区间可以扩增,继续加上右儿子的从左端连续的区间
l_sum[rt]+=l_sum[rt*2+1];
}
if(r_sum[rt]==len/2){
//如果右儿子右端连续区间长度为右儿子结点管辖长度,则说明该结点右端连续区间长度可以扩增,继续加上左儿子从右端连续的区间
r_sum[rt]+=r_sum[rt*2];
}
//用左右儿子区间长度的最大值或合并后的最大值来更新该结点的区间长度
rt_sum[rt]=max(max(rt_sum[rt*2],rt_sum[rt*2+1]),r_sum[rt*2]+l_sum[rt*2+1]);
}
void Pushdown(int rt,int len){
if(cover[rt]!=-1){
cover[rt*2]=cover[rt*2+1]=cover[rt];
l_sum[rt*2]=r_sum[rt*2]=rt_sum[rt*2]=cover[rt]?0:len-len/2;
l_sum[rt*2+1]=r_sum[rt*2+1]=rt_sum[rt*2+1]=cover[rt]?0:len/2;
cover[rt]=-1;
}
}
void update(int rt,int L,int R,int flg,int l_ran,int r_ran){
if(l_ran<=L&&R<=r_ran){ //该结点在询问区间内
l_sum[rt]=r_sum[rt]=rt_sum[rt]= flg?0:R-L+1;
cover[rt]=flg;
return ;
}
Pushdown(rt,R-L+1); //延迟标记下移
if(l_ran<=mid){
update(lson,flg,l_ran,r_ran);
}
if(r_ran>mid){
update(rson,flg,l_ran,r_ran);
}
PushUP(rt,R-L+1); //合并出可以更长的连续区间
}
int query(int rt,int L,int R,int sum){
if(L==R){
return L; //返回满足条件的区间最左端点
}
Pushdown(rt,R-L+1); //延迟标记向下移动
if(rt_sum[rt*2]>=sum){
return query(lson,sum); //左儿子的最长区间长度能满足要求
}
if(r_sum[rt*2]+l_sum[rt*2+1]>=sum){
//左儿子的右端连续区间长度加右儿子的左端连续区间长度能满足要求
return mid-r_sum[rt*2]+1;
}
return query(rson,sum); //询问右儿子
}
int main(){
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
build(1,1,n);
for(int i=0;i<m;i++){
int type,st,num;
scanf("%d",&type);
if(type==1){
scanf("%d",&num);
if(rt_sum[1]<num){
printf("0\n");
continue;
}
st=query(1,1,n,num);
printf("%d\n",st);
update(1,1,n,1,st,st+num-1);
}else{
scanf("%d%d",&st,&num);
update(1,1,n,0,st,st+num-1);
}
}
}
return 0;
}
Poj 3667——hotel——————【线段树区间合并】的更多相关文章
- POJ 3667 Hotel(线段树 区间合并)
Hotel 转载自:http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html [题目链接]Hotel [题目类型]线段树 ...
- POJ 3667 Hotel (线段树区间合并)
题目链接:http://poj.org/problem?id=3667 最初给你n间空房,m个操作: 操作1 a 表示检查是否有连续的a间空房,输出最左边的空房编号,并入住a间房间. 操作2 a b ...
- POJ 3667 & 1823 Hotel (线段树区间合并)
两个题目都是用同一个模板,询问最长的连续未覆盖的区间 . lazy代表是否有人,msum代表区间内最大的连续长度,lsum是从左结点往右的连续长度,rsum是从右结点往左的连续长度. 区间合并很恶心啊 ...
- poj 3667 Hotel (线段树)
http://poj.org/problem?id=3667 Hotel Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 94 ...
- poj 3667 Hotel(线段树,区间合并)
Hotel Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 10858Accepted: 4691 Description The ...
- 线段树(区间合并) POJ 3667 Hotel
题目传送门 /* 题意:输入 1 a:询问是不是有连续长度为a的空房间,有的话住进最左边 输入 2 a b:将[a,a+b-1]的房间清空 线段树(区间合并):lsum[]统计从左端点起最长连续空房间 ...
- poj3667 Hotel (线段树 区间合并)
poj3667 HotelTime Limit: 3000MS Memory Limit: 65536KTotal Submissions: 18925 Accepted: 8242Descripti ...
- 【bzoj1593】[Usaco2008 Feb]Hotel 旅馆 线段树区间合并
题目描述 奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光.作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿.这个巨大的旅馆一共有N (1 <= N & ...
- POJ 2482 Stars in Your Window (线段树区间合并+扫描线)
这题开始一直被矩形框束缚了,想法一直都是枚举线,但是这样枚举都需要O(n^2)...但是看了别人的思路,感觉这题思想真心很好(PS:开头好浪漫的描述啊,可惜并没有什么用) 题意就是在平面上给你一些星 ...
随机推荐
- 关于C#中Timer定时器的重入问题解决方法(也适用于多线程)
项目中用到了定时器随着服务启动作定时任务,按指定的准点时间定时执行相关操作,但是在指定准点时间内我只想让它执行一次,要避免重入问题的发生. 首先简单介绍一下timer,这里所说的timer是指的Sys ...
- 内存布局------c++程序设计基础、编程抽象与算法策略
图中给出了在一个典型c++程序中如何组织内存的框架.程序中的指令(在底层都是按位存储的).全局变量.静态对象和只读常量往往被存储在静态去(static area)(第二个图中的数据段.代码段.值得注意 ...
- LoadRunner---杂问题&接口测试
问题1] 响应时间是系统完成事务执行准备后所采集的时间戳和系统完成待执行事务后所采集的时间戳之间的时间间隔,是衡量特定类型应用事务性能的重要指标,标志了用户执行一项操作大致需要多长时间.[问题2] 系 ...
- oracle重新编译所有invalid objects
点第一个,按住shift键点最后一个,右键recompile就OK了
- CF1137F Matches Are Not a Child's Play(树链剖分)
题面 我们定义一棵树的删除序列为:每一次将树中编号最小的叶子删掉,将该节点编号加入到当前序列的最末端,最后只剩下一个节点时将该节点的编号加入到结尾. 例如对于上图中的树,它的删除序列为:2 4 3 1 ...
- jq学习笔记(一)
1 .attr() 与 .removeAttr()方法 - atr()方法用来获取和设置元素属性 attr()有4个表达式: attr(传入属性名):获取属性的值 attr(属性名, 属性值):设置属 ...
- Freeman链码
[简介] 链码(又称为freeman码)是用曲线起始点的坐标和边界点方向代码来描述曲线或边界的方法,常被用来在图像处理.计算机图形学.模式识别等领域中表示曲线和区域边界.它是一种边界的编码表示法,用边 ...
- Qt 学习之路 2(76):QML 和 QtQuick 2
Home / Qt 学习之路 2 / Qt 学习之路 2(76):QML 和 QtQuick 2 Qt 学习之路 2(76):QML 和 QtQuick 2 豆子 2013年12月18日 Qt ...
- 创建逻辑卷LVM以及swap分区
#!/bin/bash ##创建逻辑卷LVM /dev/mapper/lvm_data-data### ###default 大小为500G,但是LV一般会比500略小################ ...
- vue 遇到的一个问题......
当我用 @tap 或者 @click 触发 ajax事件时,返回的结果会非常慢--- 我也不清楚为啥会这样....(仅仅在chrome下会这样--- 所以 我用 touchend 方法替代了 该方法. ...