poj3667 Hotel
此题不难却易出错,很能考察思维的严谨性。
指定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的更多相关文章
- poj3667 Hotel (线段树 区间合并)
poj3667 HotelTime Limit: 3000MS Memory Limit: 65536KTotal Submissions: 18925 Accepted: 8242Descripti ...
- [POJ3667]Hotel(线段树,区间合并)
题目链接:http://poj.org/problem?id=3667 题意:有一个hotel有n间房子,现在有2种操作: 1 a,check in,表示入住.需要a间连续的房子.返回尽量靠左的房间编 ...
- POJ3667 Hotel 题解
和最大子段和的思路是一样的,可以记 \(lmax,rmax,dat\) 分别表示从当前区间最靠左/右的最大连续空子段和当前区间的最大连续空子段. 需要用延迟标记,每次遇到开房操作先ask,如果能找到就 ...
- 线段树总结 (转载 里面有扫描线类 还有NotOnlySuccess线段树大神的地址)
转载自:http://blog.csdn.net/shiqi_614/article/details/8228102 之前做了些线段树相关的题目,开学一段时间后,想着把它整理下,完成了大牛NotOnl ...
- HDU 3308 LCIS 线段树区间更新
最近开始线段树一段时间了,也发现了不少大牛的博客比如HH大牛 ,小媛姐.这个题目是我在看HH大牛的线段树专题是给出的习题,(可以去他博客找找,真心推荐)原本例题是POJ3667 Hotel 这个题目 ...
- [转载]完全版线段树 by notonlysuccess大牛
原文出处:http://www.notonlysuccess.com/ (好像现在这个博客已经挂掉了,在网上找到的全部都是转载) 今天在清北学堂听课,听到了一些很令人吃惊的消息.至于这消息具体是啥,等 ...
- 【转】线段树完全版~by NotOnlySuccess
线段树完全版 ~by NotOnlySuccess 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章了,觉 ...
- 《完全版线段树》——notonlysuccess
转载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文 ...
- 【转】 线段树完全版 ~by NotOnlySuccess
载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章 ...
随机推荐
- 原来现在很多人都用SignalR来实现Chat Room
今天从一个业余开发的群里,看到有人要求这样一个项目需求: 1,)学员可以通过在线课堂找到自己喜欢的老师和课程. 2,)每个人可以建立自己课堂,每个课堂扣分多个子房间,交流群.设置管理员:有录音功能,可 ...
- decimal类型保留两位小数
oj.PriceTop =Math.Round(Convert.ToDecimal(reader["PriceTop"]),2);
- 关于prototype
之前听过课,可是这一块没怎么听懂,最近练了两个例子,又问了问小石同学,朦朦胧胧,感觉还是不太懂,记录点心得 最基本的例子 function Box(name,age){ this.name=name; ...
- Servlet 基础认识 使用
参考书<JSP Web 开发案例教程> 在jsp项目中的src文件下面创建HelloServletTest.java,代码如下,注意src的包名,我的包名是zhbit.com packag ...
- Codeforce Round #218 Div2
A:没个元素的个数少的变成多的和就是了 B:居然被systemtest搓掉了- -分东西,我改的代码,还是shit一样的过的...别人的直接两个操作数相减就可以了! C:二分题- -,没想到比赛时因为 ...
- 双端队列(单调队列)poj2823 区间最小值(RMQ也可以)
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 41844 Accepted: 12384 ...
- cookie的保存时间
使用 .setMaxAge()设置(单位是秒) second>0 保存在硬盘上的时间 second<0 默认保存在浏览器的内存中 second=0 立即删除
- URAL 1018 Binary Apple Tree(树DP)
Let's imagine how apple tree looks in binary computer world. You're right, it looks just like a bina ...
- 在EditText前面添加一个搜索的小图片
1,这是EditText中的一个小的属性 代码: android:drawableLeft="@drawable/searchico" 效果图如下:
- sql语句中的 inner join 、 left join 、 right join、 full join 的区别
简单明了地说,连接分内连接和外链接 假设有A和B两张表 内连接:inner join 表示把AB表的记录相符都显示出来,把AB表不符合条件的都排除 外连接分三种,即左连接(LEFT OUTER J ...