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. day41

    今日内容: 1.完整查询语句 2.多表查询 3.子查询 1.完整查询语句: 首先对于昨天的学习补充一个复制表 示例:首先我在一个库中创建了一个t1表(id 为int类型 设置为主键 并且设置了自增描述 ...

  2. Intel 面试(就不该报外企,英语是硬伤)

    1 自我介绍(用英文) 啊啊啊,能不能用中文啊,最好用英文,蒙了.... 2 你对硬件了解吗,对X86系统了解吗,知道CPU是怎么处理读一个数据的吗,说说cpu从读一个数据,到内存怎么进行处理? 说的 ...

  3. Spring + SpringMVC配置

    代码结构如下 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xs ...

  4. Django Rest Framework源码剖析(八)-----视图与路由

    一.简介 django rest framework 给我们带来了很多组件,除了认证.权限.序列化...其中一个重要组件就是视图,一般视图是和路由配合使用,这种方式给我们提供了更灵活的使用方法,对于使 ...

  5. 20155237 《JAVA程序设计》实验三(敏捷开发与XP实践)实验报告

    20155237 <JAVA程序设计>实验三(敏捷开发与XP实践)实验报告 实验内容 敏捷开发与XP实践 XP基础 XP核心实践 相关工具 实验要求 1.没有Linux基础的同学建议先学习 ...

  6. 【来龙去脉系列】AutoMapper一款自动映射框架

    前言 通常在一个应用程序中,我们开发人员会在两个不同的类型对象之间传输数据,通常我们会用DTOs(数据传输对象),View Models(视图模型),或者直接是一些从一个service或者Web AP ...

  7. CF434D Nanami's Power Plant

    就是切糕那道题,首先对每个函数连一串,然后\(x_u\leq x_v+d\)这个条件就是\(u\)函数\(i\)取值连向\(v\)函数\(i-d\)取值边权为inf,然后答案就是最小割了. #incl ...

  8. 如何设计一个异步Web服务——接口部分

    需求比较简单,提供一个异步Web服务供使用者调用.比如说,某应用程序需要批量地给图片加lomo效果.由于加lomo效果这个操作非常消耗CPU资源,所以我们需要把这个加lomo效果的程序逻辑放到一台单独 ...

  9. Appium+python自动化4-元素定位uiautomatorviewer

    前言 环境搭建好了,下一步元素定位,元素定位本篇主要介绍如何使用uiautomatorviewer,通过定位到页面上的元素,然后进行相应的点击等操作. uiautomatorviewer是androi ...

  10. 关于最近996.icu的一点感想

    最近这个996.ICU的话题讨论的火热,特别是一些业界大佬有直言不讳的说就是要996,有的弄些鸡汤文把996说成年轻人就该这样的.作为一个普通的码农,实在是看不下去了,在这里说些自己的看法,希望年轻人 ...