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. 微信小程序开发 [00] 写在前面的话,疯狂唠唠

    我总是喜欢在写东西之前唠唠嗑,按照惯例会在博文的开篇写这么一段"写在前面的话",这次却为了这个唠嗑单独开了一篇文,大概预想着要胡说八道的话有点多. 前段时间突然对小程序来了兴趣,说 ...

  2. 设置ssh key后push为什么还要输入用户名和密码

    $ git push Username for 'https://github.com': Password for 'https://Username@github.com': Counting o ...

  3. Flask安装教程

    第1步:确保本机已经安装有python,下载easy_install到本地某一目录,双击ez_setup.py,python将自动下载到python安装目录/Scripts 下面,然后在系统环境变量的 ...

  4. currentBackgroundImage:获取按钮背景图片

    NSData *imagedata1=UIImagePNGRepresentation(btn.currentBackgroundImage);//按钮背景图片转NSData NSData *imag ...

  5. 20155204《网络对抗》Exp 6 信息搜集与漏洞扫描

    20155204<网络对抗>Exp 6 信息搜集与漏洞扫描 一.实验后回答问题 1.哪些组织负责DNS,IP的管理. 互联网名称与数字地址分配机构,简称ICANN机构,决定了域名和IP地址 ...

  6. # 20155337《网络对抗》Web基础

    20155337<网络对抗>Exp8 Web基础 实践目标 1. 实践内容 (1).Web前端HTML 能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法,编写 ...

  7. python 回溯法 子集树模板 系列 —— 8、图的遍历

    问题 一个图: A --> B A --> C B --> C B --> D B --> E C --> A C --> D D --> C E -- ...

  8. 【第九课】MriaDB密码重置和慢查询日志

    目录 1.如何进行修改MariaDB的密码 2.Mariadb的慢查询日志 1.如何进行修改MariaDB的密码 记得root密码的修改方式: [root@localhost ~]# mysqladm ...

  9. maven 相关问题

    maven 这里要更新完 不一定非要clean install  那个出问题了再弄,一般刷新一下maven仓库就行了,最好还是用自己配置的maven,不容易出问题

  10. 定制 input[type="radio"] 和 input[type="checkbox"] 样式

    表单中,经常会使用到单选按钮和复选框,但是,input[type="radio"] 和 input[type="checkbox"] 的默认样式在不同的浏览器或 ...