poj3667 Hotel
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 18925 Accepted: 8242
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

思路:
线段树区间合并,算是模板题了把。。
乍一看好像很复杂,其实理清楚思路的话还是不算难的
这道题讲解起来太麻烦了,就不写思路了。给一篇我觉得讲解的很好的博客:https://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html

实现代码:

#include<iostream>
using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid int m = (l + r) >> 1
const int M = 5e5+;
int cov[M<<],lsum[M<<],rsum[M<<],sum[M<<];
void pushdown(int m,int rt){
if(cov[rt]!=-){
cov[rt<<] = cov[rt<<|] = cov[rt];
sum[rt<<] = lsum[rt<<] = rsum[rt<<] = cov[rt]?:m-(m>>);
sum[rt<<|] = lsum[rt<<|] = rsum[rt<<|] = cov[rt]?:(m>>);
cov[rt] = -;
}
} void pushup(int m,int rt){
lsum[rt] = lsum[rt<<];
rsum[rt] = rsum[rt<<|];
if(lsum[rt] == m-(m>>)) lsum[rt] += lsum[rt<<|];
if(rsum[rt] == (m>>)) rsum[rt] += rsum[rt<<];
sum[rt] = max(lsum[rt<<|]+rsum[rt<<],max(sum[rt<<],sum[rt<<|]));
} void build(int l,int r,int rt){
sum[rt] = lsum[rt] = rsum[rt] = r - l + ;
cov[rt] = -;
if(l == r) return ;
int m = (l + r) >> ;
build(lson); build(rson);
} void update(int L,int R,int c,int l,int r,int rt){
if(L <= l&&R >= r){
sum[rt] = lsum[rt] = rsum[rt] = c?:r-l+;
cov[rt] = c;
return ;
}
pushdown(r - l + ,rt);
int m = (l + r) >> ;
if(L <= m) update(L,R,c,lson);
if(R > m) update(L,R,c,rson);
pushup(r - l + ,rt);
} int query(int w,int l,int r,int rt){
if(l == r) return l;
pushdown(r - l + ,rt);
int m = (l + r) >> ;
if(sum[rt<<] >= w) return query(w,lson);
else if(lsum[rt<<|] + rsum[rt<<] >= w) return m - rsum[rt<<] + ;
else return query(w,rson);
} int main()
{
int n,m,x,a,b,y;
cin>>n>>m;
build(,n,);
while(m--){
cin>>x;
if(x == ){
cin>>y;
if(sum[] < y) cout<<<<endl;
else {
int p = query(y,,n,);
cout<<p<<endl;
update(p,p+y-,,,n,);
}
}
else{
cin>>a>>b;
update(a,a+b-,,,n,);
}
}
}

poj3667 Hotel (线段树 区间合并)的更多相关文章

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

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

  2. poj-3667(线段树区间合并)

    题目链接:传送门 参考文章:传送门 思路:线段树区间合并问题,每次查询到满足线段树的区间最左值,然后更新线段树. #include<iostream> #include<cstdio ...

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

    题目链接:http://poj.org/problem?id=3667 最初给你n间空房,m个操作: 操作1 a 表示检查是否有连续的a间空房,输出最左边的空房编号,并入住a间房间. 操作2 a b ...

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

    两个题目都是用同一个模板,询问最长的连续未覆盖的区间 . lazy代表是否有人,msum代表区间内最大的连续长度,lsum是从左结点往右的连续长度,rsum是从右结点往左的连续长度. 区间合并很恶心啊 ...

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

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

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

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

  7. poj3667 线段树 区间合并

    //Accepted 3728 KB 1079 ms //线段树 区间合并 #include <cstdio> #include <cstring> #include < ...

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

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

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

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

随机推荐

  1. POJ 3687 Labeling Balls(反向拓扑+贪心思想!!!非常棒的一道题)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16100   Accepted: 4726 D ...

  2. kubernetes 禁用虚拟内存 swapoff -a ----- 顺便复习sed 命令

    1.如果不关闭swap,就会在kubeadm初始化Kubernetes的时候报错,如下图: [ERROR Swap]: running with swap on is not supported. P ...

  3. 搞个组装机:D

    时间:2016年7月 主机:就是主机 整机:主机+显示器 推荐:自己组装,淘宝或者京东,或者去淘宝上的宁美国度.攀升兄弟看看. 4000多块配个电脑: 处理器:i5 4590 散片(发热量小) 111 ...

  4. Swift10大开源项目记录

    Alamofire : Swift编写的HTTP网络库,用于异步网络通信. Surge: Surge基于Accelerate框架开发,用于执行矩阵数学.数字信号处理以及图像处理等方面. SwiftyJ ...

  5. input:file onchange事件无法读取解决方法

    最近做项目,移动端的多文件上传,使用input:file读取文件 <input type='file' name='file' multiple accept='image/*' capture ...

  6. cleanCode[1]:有意义的命名

    为什么要有意义的命名: 我们都曾经说过有朝一日再回头清理那些糟糕的代码,然而最终总是弃之不顾.稍后等于永不,我们需要立即行动,写优雅的代码. 写代码的过程中,读占的比例很大,所以首先要让代码易读. 有 ...

  7. storm报错:Exception in thread "main" java.lang.RuntimeException: InvalidTopologyException(msg:Component: [mybolt] subscribes from non-existent stream: [default] of component [kafka_spout])

    问题描述: storm版本:1.2.2,kafka版本:2.11.   在使用storm去消费kafka中的数据时,发生了如下错误. [root@node01 jars]# /opt/storm-1. ...

  8. libgdx学习记录25——Rectangle与Circle是否重叠

    Rect与Circle重叠有三种情况: 1. Rect至少有一个角在Circle里面 2. Circle与Rect的左边或右边相交,或者Circle在Rect内 3. Circle与Rect的顶边或底 ...

  9. springboot 设置 session 过期时间

    application.properties server.session.timeout=86400 #单位(s) 这里是24小时

  10. github协同开发

    看官请移步GitHub团队项目合作流程 本文是上述链接的截图,担心哪天作者不小心删除了,备一份在自己这里,仅为自己看着方便.侵权请告知