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(线段树+区间合并)的更多相关文章

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

    Hotel 转载自:http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html [题目链接]Hotel [题目类型]线段树 ...

  2. POJ 3667 Hotel (线段树区间合并)

    题目链接:http://poj.org/problem?id=3667 最初给你n间空房,m个操作: 操作1 a 表示检查是否有连续的a间空房,输出最左边的空房编号,并入住a间房间. 操作2 a b ...

  3. POJ 3667 & 1823 Hotel (线段树区间合并)

    两个题目都是用同一个模板,询问最长的连续未覆盖的区间 . lazy代表是否有人,msum代表区间内最大的连续长度,lsum是从左结点往右的连续长度,rsum是从右结点往左的连续长度. 区间合并很恶心啊 ...

  4. poj 3667 Hotel (线段树)

    http://poj.org/problem?id=3667 Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 94 ...

  5. poj 3667 Hotel(线段树,区间合并)

    Hotel Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 10858Accepted: 4691 Description The ...

  6. 线段树(区间合并) POJ 3667 Hotel

    题目传送门 /* 题意:输入 1 a:询问是不是有连续长度为a的空房间,有的话住进最左边 输入 2 a b:将[a,a+b-1]的房间清空 线段树(区间合并):lsum[]统计从左端点起最长连续空房间 ...

  7. Poj 3667——hotel——————【线段树区间合并】

    Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13124   Accepted: 5664 Descriptio ...

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

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

  9. 【bzoj1593】[Usaco2008 Feb]Hotel 旅馆 线段树区间合并

    题目描述 奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光.作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿.这个巨大的旅馆一共有N (1 <= N & ...

  10. POJ 2482 Stars in Your Window (线段树区间合并+扫描线)

    这题开始一直被矩形框束缚了,想法一直都是枚举线,但是这样枚举都需要O(n^2)...但是看了别人的思路,感觉这题思想真心很好(PS:开头好浪漫的描述啊,可惜并没有什么用)  题意就是在平面上给你一些星 ...

随机推荐

  1. [vue]实现父子组件数据双向绑定

    参考: http://www.cnblogs.com/xxcanghai/p/6124699.html <!DOCTYPE html> <html lang="en&quo ...

  2. TSNE数据降维学习【转载】

    转自:https://blog.csdn.net/u012162613/article/details/45920827 https://www.jianshu.com/p/d6e7083d7d61 ...

  3. Outlier Detection

    1)正态分布数据,飘出95%的可能是异常值.变量var正态标准化,|var|<=1.96的可能是异常值,further chk needed!large sample better. 对于偏态分 ...

  4. python3中pymysql模块安装及连接数据库(同‘python安装HTMLTestRunner’)

    https://pypi.org/project/PyMySQL/#files 安装完成之后就是连接数据库了 机器上安装了mysql的数据库,并且已经创建了两张表用于测试 python3连接数据库及删 ...

  5. FM/FFM原理

    转自https://tech.meituan.com/deep-understanding-of-ffm-principles-and-practices.html 深入FFM原理与实践 del2z, ...

  6. Summary: rand5构造rand7

    给一个方法,比如 rand5(), 它能够等概率生成 1-5 之间的整数. 所谓等概率就是1,2,3,4,5 生产的概率均为 0.2 .现在利用rand5(), 构造一个能够等概率生成 1- 7 的方 ...

  7. liferay总结的通用的工具类

    在写增删改查的时候,自己动手写了几个通用的工具类,这几个通用的工具类也是基于API写的 第一个是liferay中的分页.跟我们做普通的web开发,分页是一样的首先需要建立一个分页的实体的类 packa ...

  8. 【Rewrite重定向】Nginx使用rewrite重新定向

    使用nginx做重新定向. nginx参考网址:http://blog.sina.com.cn/s/blog_97688f8e0100zws5.html 语法规则: location [=|~|~*| ...

  9. wprintf或_tprintf不显示中文和乱码问题

    1.由于中文问题,宽字节编译时会遇到一些不显示或显示乱码问题 如下列代码:  分别是WIN API写入和读取文件,测试_tprintf就会不显示 #include "stdafx.h&quo ...

  10. yii2redis安装

    yii2 – redis 配置 转自:http://www.fancyecommerce.com/2016/05/03/yii2-redis-%E9%85%8D%E7%BD%AE/ 安装redis w ...