Description

  The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shores of Lake Superior. Bessie, ever the competent travel agent, has named the Bullmoose Hotel on famed Cumberland Street as their vacation residence. This immense hotel has N (1 ≤ N ≤ 50,000) rooms all located on the same side of an extremely long hallway (all the better to see the lake, of course).

  The cows and other visitors arrive in groups of size Di (1 ≤ Di ≤ N) and approach the front desk to check in. Each group i requests a set of Di contiguous rooms from Canmuu, the moose staffing the counter. He assigns them some set of consecutive room numbers r..r+Di-1 if they are available or, if no contiguous set of rooms is available, politely suggests alternate lodging. Canmuu always chooses the value of r to be the smallest possible.

  Visitors also depart the hotel from groups of contiguous rooms. Checkout i has the parameters Xi and Di which specify the vacating of rooms Xi ..Xi +Di-1 (1 ≤ XiN-Di+1). Some (or all) of those rooms might be empty before the checkout.

  Your job is to assist Canmuu by processing M (1 ≤ M < 50,000) checkin/checkout requests. The hotel is initially unoccupied.

  题目大致就是说对一段区间进行两种操作,分别是找到能够容下D的最左边的位置,然后就是更新一段区间为0或1。

  很典型的区间覆盖问题,维护最大空房间和前缀最大,后缀最大三个值。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring> #define lson L,M,po*2
#define rson M+1,R,po*2+1
#define lc po*2
#define rc po*2+1 using namespace std; const int maxn=; int msum[maxn*],lsum[maxn*],rsum[maxn*];
int COL[maxn*]; void pushDown(int po,int len)
{
if(COL[po]==)
{
COL[lc]=COL[rc]=;
msum[lc]=rsum[lc]=lsum[lc]=;
msum[rc]=rsum[rc]=lsum[rc]=; COL[po]=-;
}
else if(COL[po]==)
{
COL[lc]=COL[rc]=;
msum[lc]=rsum[lc]=lsum[lc]=len-(len/);
msum[rc]=rsum[rc]=lsum[rc]=len/; COL[po]=-;
}
} void pushUP(int po,int len)
{
msum[po]=max(msum[lc],msum[rc]);
msum[po]=max(msum[po],rsum[lc]+lsum[rc]); lsum[po]=lsum[lc];
if(lsum[lc]==(len-(len/)))
lsum[po]+=lsum[rc]; rsum[po]=rsum[rc];
if(rsum[rc]==len/)
rsum[po]+=rsum[lc];
} void build_tree(int L,int R,int po)
{
COL[po]=-;
msum[po]=rsum[po]=lsum[po]=R-L+; if(L==R)
return; int M=(L+R)/; build_tree(lson);
build_tree(rson);
} void update(int ul,int ur,int ut,int L,int R,int po)
{
if(ul<=L&&ur>=R)
{
COL[po]=ut;
msum[po]=lsum[po]=rsum[po]=(ut ? : R-L+); return;
} pushDown(po,R-L+); int M=(L+R)/; if(ul<=M)
update(ul,ur,ut,lson);
if(ur>M)
update(ul,ur,ut,rson); pushUP(po,R-L+);
} int query(int len,int L,int R,int po)
{
if(L==R)
return L; pushDown(po,R-L+); int M=(L+R)/; if(msum[lc]>=len)
return query(len,lson);
else if(rsum[lc]+lsum[rc]>=len)
return M-rsum[lc]+;
else
return query(len,rson);
} int main()
{
int M,N;
int a,b,c; while(~scanf("%d %d",&N,&M))
{
build_tree(,N,); for(int i=;i<M;++i)
{
scanf("%d %d",&a,&b); if(a==)
{
if(msum[]<b)
printf("%d\n",);
else
{
c=query(b,,N,);
update(c,c+b-,,,N,);
printf("%d\n",c);
}
}
else
{
scanf("%d",&c);
update(b,b+c-,,,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. POJ 3667 线段树区间合并

    http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html 用线段树,首先要定义好线段树的节点信息,一般看到一个问题,很难很 ...

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

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

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

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

随机推荐

  1. HDU 1495 非常可乐 BFS

    题目大意:中文题不说了. 题目思路:我有同学用GCD数论写出来的代码很简洁,但是很抱歉,数论蒟蒻,我觉得比赛的时候我没办法推出.如果用BFS的话思路很简单的,就是6方向广搜,只不过稍微麻烦点.具体看代 ...

  2. XAMPP(v1.83)中的PHP(v5.5.15)访问SQLServer2014

    驱动安装: 1. 下载SQLServer的微软官方PHP驱动,http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx 2. 安装SQLSRV31 ...

  3. JAVA-基本知识

    1.JAVA跨平台 其实就是在每个平台上要安装对应该操作系统的JVM,JVM负责解析执行,即实现了跨平台.JVM是操作系统与java程序之间的桥梁. 2.JRE:java运行环境,包含JVM+核心类库 ...

  4. POJ 3368/RMQ/线段数

    题目链接 /* 给出一段序列,询问[L,R]区间内最大相同数的个数. 用一个很巧妙地方法,转化成求区间内的最大值的问题. RMQ维护区间最大值. MAX处理: */ for(int i=1;i< ...

  5. UICollectionView 浅析

    什么是UICollectionView UICollectionView是一种新的数据展示方式,简单来说可以把他理解成多列的UITableView(请一定注意这是UICollectionView的最最 ...

  6. mysql Group By

    1.概述 “Group By”从字面意义上理解就是根据“By”指定的规则对数据进行分组,所谓的分组就是将一个“数据集”划分成若干个“小区域”,然后针对若干个“小区域”进行数据处理. 2.原始表 3.简 ...

  7. java 数据结构 图

    以下内容主要来自大话数据结构之中,部分内容参考互联网中其他前辈的博客,主要是在自己理解的基础上进行记录. 图的定义 图是由顶点的有穷非空集合和顶点之间边的集合组成,通过表示为G(V,E),其中,G标示 ...

  8. Centos-ip配置详解

    1 搭建好Centos ,我这里是CentOS-6.7-x86_64-minimal  提供一个下载地址 链接:http://pan.baidu.com/s/1nvTUTh3 密码:xewk 2 我是 ...

  9. hdu_5179_beautiful number(数位DP)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5179 题意:给你一个范围,问你漂亮的数有多少个,漂亮的数的定义为 数位高的比数位低的大,并且 数位高的 ...

  10. java dom4j解析xml实例(2)

    java利用dom4j解析xml 需要的jar包: dom4j官方网站在 http://www.dom4j.org/ 下载dom4j-1.6.1.zip 解开后有两个包,仅操作XML文档的话把dom4 ...