Hotel

Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

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

/****
思路: 线段树的区间合并问题: 要查询连续区间长度,所以要记录最长的连续区间,一段区间的连续可以分为左连续,右连续,中间连续,然后记录就可以了 父节点左连续区间为左儿子左连续,如果左儿子在整个左区间内连续则再加上右儿子左连续 父节点右连续区间为右儿子右连续,如果右儿子在整个右区间内连续则再加上左儿子右连续 父节点中间连续区间为左儿子右连续加上右儿子左连续 然后记录每个节点总的最长连续区间的值 每次向上更新或者向下延迟都要重新计算节点信息
****/
#include"iostream"
#include"algorithm"
#include"cstdio"
#include"cstring"
#include"cmath"
//#define max(a,b) a>b?a:b //【这个地方WA了好多次。。。】
//#define min(a,b) a<b?a:b //【这个宏定义有问题。。。以后再也不随便用宏定义了。。。WA哭了。。。】
#define MX 110000
#define INF 0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std; int lsum[MX<<],rsum[MX<<],sum[MX<<];
int lazy[MX<<]; // 1 a 询问是否存在a间连续的空房间,有则住进最左边 【】
// 2 a b 将 [a,a+b-1] 编号的房间清空 【成段更新】 void PushDown(int rt,int m) {
if(lazy[rt]!=INF) {//1 ->
lazy[rt<<]=lazy[rt<<|]=lazy[rt]; //下移懒惰标记
sum[rt<<]=lsum[rt<<]=rsum[rt<<]=lazy[rt]*(m-(m>>)); //因为
sum[rt<<|]=lsum[rt<<|]=rsum[rt<<|]=lazy[rt]*(m>>);
lazy[rt]=INF;
}
} void PushUp(int rt,int m) {
lsum[rt]=lsum[rt<<];
rsum[rt]=rsum[rt<<|];
if(lsum[rt]==m-(m>>)) lsum[rt]+=lsum[rt<<|]; // 这里lson[rt<<1]是表示的左区间的左最大值,lson[rt<<1|1]表示的是左区间的右最大值
if(rsum[rt]== (m>>) ) rsum[rt]+=rsum[rt<<]; // rson[。。。] 类似 而左区间的右最大值加右区间的左最大值就是中间区间的最大值
sum[rt]=max(max(sum[rt<<],sum[rt<<|]),lsum[rt<<|]+rsum[rt<<]); //保存两边子节点和 中最大值 中的最大值
} void Build(int l,int r,int rt) {
sum[rt]=lsum[rt]=rsum[rt]=r-l+; //把每一个空房间标记为1,每个父节点记录为子节点最大的空区间
lazy[rt]=INF; //懒惰标记清空
if(l==r) return ;
int m=(r+l)>>;
Build(lson);
Build(rson);
} void UpData(int L,int R,int val,int l,int r,int rt) {
if(L<=l&&r<=R) {
sum[rt]=lsum[rt]=rsum[rt]= val ? (r-l+):;
lazy[rt]=val;
return ;
}
PushDown(rt,r-l+);
int m=(r+l)>>;
if(L<=m) UpData(L,R,val,lson);
if(R> m) UpData(L,R,val,rson);
PushUp(rt,r-l+);
} int Query(int ll,int l,int r,int rt) {
if(l==r) return l;
PushDown(rt,r-l+);
int m=(r+l)>>;
if(sum[rt<<]>=ll) return Query(ll,lson); //如果左最大值满足就在往左继续搜
else if(rsum[rt<<]+lsum[rt<<|]>=ll)return m-rsum[rt<<]+;//如果中最大值,满足就输出左区间的右最大值的第一个位置
return Query(ll,rson); //否则就向右继续搜
} int main() {
int n,m;
scanf("%d%d",&n,&m);
Build(,n,);
int o,a,b;
for(int i=; i<m; i++) {
scanf("%d",&o) ;
if(o==) {
scanf("%d",&a);
if(sum[]<a) printf("0\n");
else {
int q=Query(a,,n,);
printf("%d\n",q);
UpData(q,q+a-,,,n,);
}
} else if(o==) {
scanf("%d%d",&a,&b);
UpData(a,a+b-,,,n,);
}
}
return ;
}

ACM: Hotel 解题报告 - 线段树-区间合并的更多相关文章

  1. ACM: Billboard 解题报告-线段树

     Billboard Time Limit:8000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descript ...

  2. Poj 3667——hotel——————【线段树区间合并】

    Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13124   Accepted: 5664 Descriptio ...

  3. POJ 3667 Hotel(线段树 区间合并)

    Hotel 转载自:http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html [题目链接]Hotel [题目类型]线段树 ...

  4. 线段树(区间合并) POJ 3667 Hotel

    题目传送门 /* 题意:输入 1 a:询问是不是有连续长度为a的空房间,有的话住进最左边 输入 2 a b:将[a,a+b-1]的房间清空 线段树(区间合并):lsum[]统计从左端点起最长连续空房间 ...

  5. poj3667 Hotel (线段树 区间合并)

    poj3667 HotelTime Limit: 3000MS Memory Limit: 65536KTotal Submissions: 18925 Accepted: 8242Descripti ...

  6. 【bzoj1593】[Usaco2008 Feb]Hotel 旅馆 线段树区间合并

    题目描述 奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光.作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿.这个巨大的旅馆一共有N (1 <= N & ...

  7. HDU 3911 线段树区间合并、异或取反操作

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...

  8. HDU 3308 LCIS (线段树区间合并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 题目很好懂,就是单点更新,然后求区间的最长上升子序列. 线段树区间合并问题,注意合并的条件是a[ ...

  9. hdu 3911 Black And White (线段树 区间合并)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3911 题意: 给你一段01序列,有两个操作: 1.区间异或,2.询问区间最长的连续的1得长度 思路: ...

随机推荐

  1. AIX扩展文件系统的大小

    由于AIX系统空间不够需要增加硬盘,希望增加文件系统的空间,折腾了好几天怎么都不能扩展文件系统的空间,原来是把硬盘加错了卷组 首先,确定文件系统所在的LV /dev/datalv      270.0 ...

  2. subversion安装使用

    这里仅针对subversion进行说明,未完待续. 一.下载subversion并安装: a.subversion 要做svn服务器的必须装 b.Tortoisesvn 仅仅是访问svn服务器的客户端 ...

  3. annotation-config 和 component-scan 的区别

    <context:annotation-config> 和 <context:component-scan>是Spring Core里面的两个基础概念,每个使用者都有必要理解怎 ...

  4. Java Hour 67 Java Collection API

    本文不是一个大而全的讲述Java Coleection 相关的APi, 而是笔者认为哪些是一个初学者所能够而且必须确切知道的知识点. Collection 一脉 这里有我们比较常用的List<E ...

  5. VS2010 水晶报表的使用

    在VS2010中新建一个“Windows 窗体应用程序”项目,在该项目中添加一个水晶报表“CrystalReport1.rpt”,然后在项目上点击鼠标右键属性,将“目标框架”改为“.Net Frame ...

  6. 编译器 expected unqualified-id before numeric constant 错误

    今天调试代码,碰到expected unqualified-id before numeric constant 错误,代码的错误模块出现在一个函数模块上, 奇怪的是这个函数模块之前编译了很多次,也没 ...

  7. barabasilab-networkScience学习笔记2-图理论

    第一次接触复杂性科学是在一本叫think complexity的书上,Allen博士很好的讲述了数据结构与复杂性科学,barabasi是一个知名的复杂性网络科学家,barabasilab则是他所主导的 ...

  8. thinkphp数据表操作恐怖事件。

    1当使用thinkphp的where(array())时,如果里面的字段在数据库是没有的,则默认这个条件为1,这时就可能出现大批修改记录问题.如修改所有用户的密码.特别要注意的是,这里的表字段是区分大 ...

  9. Liferay 6.2 改造系列之十二:修改Portal设置页面表单内容

    将Portal设置页面中无用的内容删除: 在/portal-master/portal-impl/src/portal.properties文件中,有如下配置: # # Input a list of ...

  10. CSS3_实现圆角效果box-shadow

    1.outline的直角与圆角 来给个div: <div class="use-outline"></div> 来再给个样式: .use-outline{ ...