此题不难却易出错,很能考察思维的严谨性。

指定ll为区间内左端顶格数的连续可利用房间,rr为右端顶格数的数值,mm为区间内最长的连续可利用房间数。

在查询的时候,由于要返回最靠左的区间左端点,使得在该点右侧有长为l的可利用房间数。

那么首先有解存在当且仅当区间mm值大于等于给定长度l。

考虑区间左端点可能落在左区间也可能落在右区间。

横跨区间中点的情形特别要小心,我就是在这wa了几次。

http://poj.org/problem?id=3667

 #include <cstdio>
#include <cstring>
#include <algorithm>
#define lson (u << 1)
#define rson (u << 1 | 1)
using namespace std;
typedef __int64 LL;
const int maxn = 5e4 + ;
struct Seg{
int l, r;
int lazy;
int ll, mm, rr;//vacant rooms
}seg[maxn << ];
int n, m, ans; void build(int u, int l, int r){
seg[u].l = l, seg[u].r = r, seg[u].lazy = ;
seg[u].mm = seg[u].ll = seg[u].rr = r - l;
if(r - l < ) return;
int mid = (l + r) >> ;
build(lson, l, mid);
build(rson, mid, r);
} void push_down(int u){
if(seg[u].lazy != && seg[u].r - seg[u].l > ){
if(seg[u].lazy == -){
seg[lson].lazy = seg[rson].lazy = -;
seg[lson].mm = seg[lson].rr = seg[lson].ll = seg[lson].r - seg[lson].l;
seg[rson].mm = seg[rson].rr = seg[rson].ll = seg[rson].r - seg[rson].l;
seg[u].lazy = ;
}else if(seg[u].lazy == ){
seg[lson].lazy = seg[rson].lazy = ;
seg[lson].mm = seg[lson].rr = seg[lson].ll = ;
seg[rson].mm = seg[rson].rr = seg[rson].ll = ;
seg[u].lazy = ;
}
}
} void push_up(int u){
seg[u].mm = max(seg[lson].rr + seg[rson].ll, seg[lson].mm);
seg[u].mm = max(seg[u].mm, seg[rson].mm);
seg[u].ll = seg[lson].ll == seg[lson].r - seg[lson].l ? seg[lson].ll + seg[rson].ll : seg[lson].ll;
seg[u].rr = seg[rson].rr == seg[rson].r - seg[rson].l ? seg[rson].rr + seg[lson].rr : seg[rson].rr;
} void query(int u, int l, int r, int p){
if(seg[u].mm < p || ans != -) return;
if(seg[u].ll >= p){
ans = seg[u].l;
return;
}
push_down(u);
int mid = (l + r) >> ;
if(seg[lson].mm >= p) query(lson, l, mid, p);
else if(seg[lson].rr + seg[rson].ll >= p) ans = mid - seg[lson].rr;
else query(rson, mid, r, p);
} void update(int u, int l, int r, int L, int R, int sg){
if(l == L && R == r){
if(sg == -){
seg[u].mm = seg[u].rr = seg[u].ll = r - l;
seg[u].lazy = -;
}else if(sg == ){
seg[u].mm = seg[u].rr = seg[u].ll = ;
seg[u].lazy = ;
}
return;
}
push_down(u);
int mid = (l + r) >> ;
if(R <= mid) update(lson, l, mid, L, R, sg);
else if(L >= mid) update(rson, mid, r, L, R, sg);
else{
update(lson, l, mid, L, mid, sg);
update(rson, mid, r, mid, R, sg);
}
push_up(u);
} int main(){
freopen("in.txt", "r", stdin);
while(~scanf("%d%d", &n, &m)){
build(, , n + );
for(int i = , u, v, op; i < m; i++){
scanf("%d", &op);
if(op == ){
scanf("%d", &u);
ans = -;
query(, , n + , u);
printf("%d\n",ans == - ? : ans);
if(ans != -) update(, , n + , ans, ans + u, );
}else if(op == ){
scanf("%d%d", &u, &v);
update(, , n + , u, u + v, -);
}
}
}
return ;
}

poj3667 Hotel的更多相关文章

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

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

  2. [POJ3667]Hotel(线段树,区间合并)

    题目链接:http://poj.org/problem?id=3667 题意:有一个hotel有n间房子,现在有2种操作: 1 a,check in,表示入住.需要a间连续的房子.返回尽量靠左的房间编 ...

  3. POJ3667 Hotel 题解

    和最大子段和的思路是一样的,可以记 \(lmax,rmax,dat\) 分别表示从当前区间最靠左/右的最大连续空子段和当前区间的最大连续空子段. 需要用延迟标记,每次遇到开房操作先ask,如果能找到就 ...

  4. 线段树总结 (转载 里面有扫描线类 还有NotOnlySuccess线段树大神的地址)

    转载自:http://blog.csdn.net/shiqi_614/article/details/8228102 之前做了些线段树相关的题目,开学一段时间后,想着把它整理下,完成了大牛NotOnl ...

  5. HDU 3308 LCIS 线段树区间更新

    最近开始线段树一段时间了,也发现了不少大牛的博客比如HH大牛  ,小媛姐.这个题目是我在看HH大牛的线段树专题是给出的习题,(可以去他博客找找,真心推荐)原本例题是POJ3667 Hotel 这个题目 ...

  6. [转载]完全版线段树 by notonlysuccess大牛

    原文出处:http://www.notonlysuccess.com/ (好像现在这个博客已经挂掉了,在网上找到的全部都是转载) 今天在清北学堂听课,听到了一些很令人吃惊的消息.至于这消息具体是啥,等 ...

  7. 【转】线段树完全版~by NotOnlySuccess

    线段树完全版  ~by NotOnlySuccess 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章了,觉 ...

  8. 《完全版线段树》——notonlysuccess

    转载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文 ...

  9. 【转】 线段树完全版 ~by NotOnlySuccess

    载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章 ...

随机推荐

  1. Velocity(1)——注释

    Velocity的单行注释,使用## 多行注释使用#* cooments *#

  2. CSS位置如何获取的

  3. SQL 2008 数据库只读 修改

        先对数据库分离 数据库鼠标右键->任务->分离   将UsersDB.mdf UsersDB_log.LDF文件 属性->安全->编辑   两个文件的都要更改权限   ...

  4. iis access denied, you do not have permission.

    this kind of problems are usually caused by some IIS configuration issues, like application pool set ...

  5. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Beauty of Array

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5496 The 12th Zhejiang Provincial ...

  6. nyist 62 笨小熊

    http://acm.nyist.net/JudgeOnline/problem.php?pid=62 笨小熊 时间限制:2000 ms  |  内存限制:65535 KB 难度:2   描述 笨小熊 ...

  7. [转] MongoDB shell 操作 (查询)

    最近有用到mongoDB,每次都去查看官方文档很是费劲,自己准备写点东西.但在博客园上看到另外的一篇博文不错,就转载过来,加上点儿自己的修饰 左边是mongodb查询语句,右边是sql语句.对照着用, ...

  8. ZOJ 3545 Rescue the Rabbit(AC自动机+状压DP)(The 2011 ACM-ICPC Asia Dalian Regional Contest)

    Dr. X is a biologist, who likes rabbits very much and can do everything for them. 2012 is coming, an ...

  9. paper 36 :[教程] 基于GridSearch的svm参数寻优

    尊重原创~~~ 转载出处:http://www.matlabsky.com/thread-12411-1-1.html 交叉验证(Cross Validation)方法思想简介http://www.m ...

  10. 【linux】暂时解决sis m672(神舟F4000 D9) linux驱动 宽屏分辨率的问题?

    1. 首先安装包 sudo apt-get install gcc make binutils git xorg-dev mesa-common-dev libdrm-dev libtool buil ...