POJ 3667 Hotel(线段树+区间合并)
http://poj.org/problem?id=3667
题意:
有N个房间,M次操作。有两种操作(1)"1a",表示找到连续的长度为a的空房间,如果有多解,优先左边的,即表示入住。(2)"2 b len",把起点为b长度的len的房间清空,即退房。
思路:
区间合并的线段树题。
其实如果单点更新和区间更新做得多了的话,区间合并也就不难了。
对于这道题,我们用线段树维护三个值,从左端开始的连续空房间数,从右边开始的连续空房间数,以及整个区间内最大的连续空房间数。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int maxn=+; int n, m; struct node //维护从左端开始的最大连续空房间,从右端开始的最大连续空房间,总的最大连续空房间
{
int l,r;
int ls,rs,sum;
}t[maxn<<]; int lazy[maxn<<]; void PushUp(int o)
{
t[o].ls=t[o<<].ls;
t[o].rs=t[o<<|].rs;
int mid=(t[o].l+t[o].r)>>;
if(t[o].ls==mid-t[o].l+) t[o].ls+=t[o<<|].ls; //如果左半部分都是空房间,那么可以与右半部分的左边空房间连起来
if(t[o].rs==t[o].r-mid) t[o].rs+=t[o<<].rs;
t[o].sum=max(max(t[o<<].sum,t[o<<|].sum),t[o<<].rs+t[o<<|].ls);
} void PushDonw(int o)
{
if(lazy[o]!=-)
{
lazy[o<<]=lazy[o<<|]=lazy[o];
if(lazy[o]) //为1的话要将该区间置满
{
t[o<<].ls=t[o<<].rs=t[o<<].sum=;
t[o<<|].ls=t[o<<|].rs=t[o<<|].sum=;
}
else //为0的话要将该区间置空
{
t[o<<].ls=t[o<<].rs=t[o<<].sum=t[o<<].r-t[o<<].l+;
t[o<<|].ls=t[o<<|].rs=t[o<<|].sum=t[o<<|].r-t[o<<|].l+;
}
lazy[o]=-;
}
} void build(int l, int r, int o)
{
lazy[o]=-;
t[o].l=l;
t[o].r=r;
t[o].ls=t[o].rs=t[o].sum=r-l+;
if(l==r) return;
int mid=(l+r)>>;
build(l,mid,o<<);
build(mid+,r,o<<|);
} void update(int ql, int qr, int l, int r, int flag, int o)
{
if(ql<=l && qr>=r)
{
if(flag) t[o].ls=t[o].rs=t[o].sum=;
else t[o].ls=t[o].rs=t[o].sum=r-l+;
lazy[o]=flag;
return;
}
PushDonw(o);
int mid=(l+r)>>;
if(ql<=mid) update(ql,qr,l,mid,flag,o<<);
if(qr>mid) update(ql,qr,mid+,r,flag,o<<|);
PushUp(o);
} int query(int l, int r, int num, int o)
{
if(l==r) return l;
PushDonw(o);
int mid=(l+r)>>;
if(t[o<<].sum>=num) return query(l,mid,num,o<<);
else if(t[o<<].rs+t[o<<|].ls>=num) return mid-t[o<<].rs+;
else return query(mid+,r,num,o<<|);
} int main()
{
//freopen("in.txt","r",stdin);
scanf("%d%d",&n,&m);
build(,n,);
while(m--)
{
int op;
scanf("%d",&op);
if(op==)
{
int x;
scanf("%d",&x);
if(t[].sum<x) {puts("");continue;}
int pos=query(,n,x,);
printf("%d\n",pos);
update(pos,pos+x-,,n,,);
}
else
{
int s,t;
scanf("%d%d",&s,&t);
update(s,s+t-,,n,,);
}
}
return ;
}
POJ 3667 Hotel(线段树+区间合并)的更多相关文章
- POJ 3667 Hotel(线段树 区间合并)
Hotel 转载自:http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html [题目链接]Hotel [题目类型]线段树 ...
- POJ 3667 Hotel (线段树区间合并)
题目链接:http://poj.org/problem?id=3667 最初给你n间空房,m个操作: 操作1 a 表示检查是否有连续的a间空房,输出最左边的空房编号,并入住a间房间. 操作2 a b ...
- POJ 3667 & 1823 Hotel (线段树区间合并)
两个题目都是用同一个模板,询问最长的连续未覆盖的区间 . lazy代表是否有人,msum代表区间内最大的连续长度,lsum是从左结点往右的连续长度,rsum是从右结点往左的连续长度. 区间合并很恶心啊 ...
- poj 3667 Hotel (线段树)
http://poj.org/problem?id=3667 Hotel Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 94 ...
- poj 3667 Hotel(线段树,区间合并)
Hotel Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 10858Accepted: 4691 Description The ...
- 线段树(区间合并) POJ 3667 Hotel
题目传送门 /* 题意:输入 1 a:询问是不是有连续长度为a的空房间,有的话住进最左边 输入 2 a b:将[a,a+b-1]的房间清空 线段树(区间合并):lsum[]统计从左端点起最长连续空房间 ...
- Poj 3667——hotel——————【线段树区间合并】
Hotel Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 13124 Accepted: 5664 Descriptio ...
- poj3667 Hotel (线段树 区间合并)
poj3667 HotelTime Limit: 3000MS Memory Limit: 65536KTotal Submissions: 18925 Accepted: 8242Descripti ...
- 【bzoj1593】[Usaco2008 Feb]Hotel 旅馆 线段树区间合并
题目描述 奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光.作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿.这个巨大的旅馆一共有N (1 <= N & ...
- POJ 2482 Stars in Your Window (线段树区间合并+扫描线)
这题开始一直被矩形框束缚了,想法一直都是枚举线,但是这样枚举都需要O(n^2)...但是看了别人的思路,感觉这题思想真心很好(PS:开头好浪漫的描述啊,可惜并没有什么用) 题意就是在平面上给你一些星 ...
随机推荐
- NYOJ 47
思路: 在n>3的时候就用前两个小的来带后两个大的,有两种方式(一就是1,2先过,然后1回来,然后n,n-1过,然后2,回来),(二就是 1,n先过去 然后1回来,然后 1,n-1过去吗.,然后 ...
- 12.预处理数据的方法总结(使用sklearn-preprocessing)
https://blog.csdn.net/sinat_33761963/article/details/53433799
- Python 使用ctypes调用 C 函数
在python中通过ctypes可以直接调用c的函数,非常简单易用 下面就一步一步解释用法吧,以Linux为例讲解. 1, 首先确定你的python支持不支持ctypes python2.7以后cty ...
- __import__ 与动态加载 python module
原文出处: koala bear Direct use of __import__() is rare, except in cases where you want to import a m ...
- PAT 1103 Integer Factorization[难]
1103 Integer Factorization(30 分) The K−P factorization of a positive integer N is to write N as the ...
- python中的re模块中的向后引用和零宽断言
1.后向引用 pattern = re.compile(r"(\w+)")#['hello', 'go', 'go', 'hello'] # pattern = re.compil ...
- testng入门教程12 TestNG执行多线程测试
testng入门教程 TestNG执行多线程测试 testng入门教程 TestNG执行多线程测试 并行(多线程)技术在软件术语里被定义为软件.操作系统或者程序可以并行地执行另外一段程序中多个部分或者 ...
- iOS 第三方框架-MBProgressHUD
MBProgressHUD提示框官网地址:https://github.com/jdg/MBProgressHUD 官网里已经提供了足够多的例子供我们使用,但在实现开发中,我们用到的只是其中的一小部分 ...
- IBatis 配置各种数据库
IBatis 与各种数据库之间的配置在providers.config这个文件下. <?xml version="1.0" encoding="utf-8" ...
- jQuery在iframe里取得父窗口的某个元素的值
提供一款jQuery在iframe里取得父窗口的某个元素的值实现,这个iframe用js也差不多,有需要的朋友可以参考一下. 1.在父窗口中获取指定iframe(testiframe) id 为 te ...