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

指定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. 解决eclipse svn插件 的lock问题

    org.tigris.subversion.javahl.ClientException: Attemptedto lock an already-locked dir异常解决方法 myeclipse ...

  2. html 圆角边框

    <input style="border-radius: 10px;" type="submit" value="确认"> bo ...

  3. java中hashCode()方法的作用

    hashcode方法返回该对象的哈希码值.      hashCode()方法可以用来来提高Map里面的搜索效率的,Map会根据不同的hashCode()来放在不同的位置,Map在搜索一个对象的时候先 ...

  4. 通过反射封装JDBC

    具体上代码我的BaseDao: public class BaseDao<T> {  private Class clazz;  private Properties pro=null;  ...

  5. Fresco源码解析 - 创建一个ImagePipeline(一)

    在Fresco源码解析 - 初始化过程分析章节中, 我们分析了Fresco的初始化过程,两个initialize方法中都用到了 ImagePipelineFactory类. ImagePipeline ...

  6. Microsoft.Jet.OLEDB.4.0和Microsoft.ACE.OLEDB.12.0的区别

    Microsoft.Jet.OLEDB.4.0和Microsoft.ACE.OLEDB.12.0的区别 时间 2012-12-19 20:30:12  CSDN博客原文  http://blog.cs ...

  7. 夺命雷公狗—angularjs—21—解决angularjs压缩问题

    我们在实际的开发中往往离不开js的代码压缩,因为这样可以减轻服务器的压力,是的的方法如下所示: <!DOCTYPE html> <html lang="en" n ...

  8. lower power设计中的DVFS设计

    Pswitch = Ceff * Vvdd^2*Fclk, Pshort-circuit = Isc * Vdd * Fclk, Pleakage = f(Vdd, Vth, W/L) 尽管对电压的s ...

  9. [fedora21]给fedora21安装fcitx输入法

    如果已经安装了ibus,需要卸载ibus:   sudo yum remove ibus; 安装fcitx: sudo yum install fcitx fcitx-pinyin fcitx-con ...

  10. jquery冲突细节

    使用jquery报一个错误,之前也遇到过,今天记录下来,方便以后使用 Uncaught TypeError: Object #<Object> has no method 'test' 这 ...