【POJ3667】Hotel
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 ≤ Xi ≤N-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.
Input
* Line 1: Two space-separated integers: N and M
* Lines 2..M+1: Line i+1 contains request expressed as one of two possible formats: (a) Two space separated integers representing a check-in request: 1 and Di (b) Three space-separated integers representing a check-out: 2, Xi, and Di
Output
* Lines 1.....: For each check-in request, output a single line with a single integer r, the first room in the contiguous sequence of rooms to be occupied. If the request cannot be satisfied, output 0.
Sample Input
10 6
1 3
1 3
1 3
1 3
2 5 5
1 6
Sample Output
1
4
7
0
5
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#define N 100000
using namespace std;
struct data{int sl,sr,maxs,ls,rs;}seg[N*];
int lazy[N*];
int n,m;
void pushsign(int now)
{
if (lazy[now]==)
{
seg[(now<<)].maxs=seg[(now<<)].ls=seg[(now<<)].rs=seg[(now<<)].sr-seg[(now<<)].sl+;
lazy[(now<<)]=lazy[now];
seg[(now<<)+].maxs=seg[(now<<)+].ls=seg[(now<<)+].rs=seg[(now<<)+].sr-seg[(now<<)+].sl+;
lazy[(now<<)+]=lazy[now];
lazy[now]=;
}
else if (lazy[now]==)
{
seg[(now<<)].maxs=seg[(now<<)].ls=seg[(now<<)].rs=;
lazy[(now<<)]=lazy[now];
seg[(now<<)+].maxs=seg[(now<<)+].ls=seg[(now<<)+].rs=;
lazy[(now<<)+]=lazy[now];
lazy[now]=;
}
}
void adddata(int now)
{
seg[now].maxs=seg[(now<<)].rs+seg[(now<<)+].ls;
seg[now].maxs=max(max(seg[(now<<)].maxs,seg[(now<<)+].maxs),seg[now].maxs);
seg[now].ls=seg[(now<<)].ls;
if (seg[(now<<)].ls==seg[(now<<)].sr-seg[(now<<)].sl+) seg[now].ls+=seg[(now<<)+].ls;
seg[now].rs=seg[(now<<)+].rs;
if (seg[(now<<)+].rs==seg[(now<<)+].sr-seg[(now<<)+].sl+) seg[now].rs+=seg[(now<<)].rs;
}
void buildtree(int now,int l,int r)
{ seg[now].sl=l; seg[now].sr=r; seg[now].maxs=seg[now].ls=seg[now].rs=r-l+;
if (l==r) return;
int mid=(l+r)>>;
buildtree((now<<),l,mid);
buildtree((now<<)+,mid+,r);
adddata(now);
}
int query(int now,int lon)
{
int l=seg[now].sl,r=seg[now].sr;
if (l==r) return l;
pushsign(now);
int pos=;
if (seg[(now<<)].ls>=lon) return seg[(now<<)].sl;
else if (seg[(now<<)].maxs>=lon) pos=query((now<<),lon);
else if (seg[(now<<)].rs+seg[(now<<)+].ls>=lon &&seg[(now<<)].rs!=) return seg[(now<<)].sr-seg[(now<<)].rs+;
else if (seg[(now<<)+].maxs>=lon)pos=query((now<<)+,lon);
else if (seg[now].maxs==r-l+) return l;
return pos;
}
void ichange1(int now,int begin,int end)
{
int l=seg[now].sl,r=seg[now].sr;
if (begin<=l && end>=r) {seg[now].maxs=seg[now].ls=seg[now].rs=r-l+; lazy[now]=; return;}
pushsign(now);
int mid=(l+r)>>;
if (begin<=mid) ichange1((now<<),begin,end);
if (end>mid) ichange1((now<<)+,begin,end);
adddata(now);
}
void ichange2(int now,int begin,int end)
{
int l=seg[now].sl,r=seg[now].sr;
if (begin<=l && end>=r) {seg[now].maxs=seg[now].ls=seg[now].rs=; lazy[now]=; return;}
pushsign(now);
int mid=(l+r)>>;
if (begin<=mid) ichange2((now<<),begin,end);
if (end>mid) ichange2((now<<)+,begin,end);
adddata(now);
}
int main()
{
int i;
int a,b,p;
while (~scanf("%d%d",&n,&m))
{
buildtree(,,n);
for (i=;i<=m;i++)
{
scanf("%d",&p);
if (p==)
{
scanf("%d",&a);
if (seg[].maxs<a) {printf("0\n");continue;}
int pos=query(,a);
printf("%d\n",pos);
ichange2(,pos,pos+a-);
}
else
{
scanf("%d%d",&a,&b);
ichange1(,a,a+b-);
}
}
}
//system("pause");
return ;
}
【POJ3667】Hotel的更多相关文章
- 【POJ3667】Hotel(线段树)
题意:有n个依次编号的元素,要求维护以下两个操作: 1.询问整个数列中是否有长度>=x的连续的一段未被标记的元素,若无输出0,若有输出最小的开始编号ans并将[ans,ans+x-1]标记 2. ...
- 【BZOJ4543】Hotel加强版(长链剖分)
[BZOJ4543]Hotel加强版(长链剖分) 题面 BZOJ,没有题面 洛谷,只是普通版本 题解 原来我们的\(O(n^2)\)做法是设\(f[i][j]\)表示以\(i\)为根的子树中,距离\( ...
- 【BZOJ4543】Hotel加强版
[BZOJ4543]Hotel加强版 题面 bzoj 洛谷 $ps:$在洛谷看题在bzoj交... 题解 我们分析一下这个问题,要怎么样的点才满足三点距离两两相等呢? 1.存在三个点有共同的$LCA$ ...
- 【BZOJ】【3522】【POI2014】Hotel
暴力/树形DP 要求在树上找出等距三点,求方案数,那么用类似Free Tour2那样的合并方法,可以写出: f[i][j]表示以 i 为根的子树中,距离 i 为 j 的点有多少个: g[i][j]表示 ...
- 【BZOJ3522】【BZOJ4543】【POI2014】Hotel 树形DP 长链剖分 启发式合并
题目大意 给你一棵树,求有多少个组点满足\(x\neq y,x\neq z,y\neq z,dist_{x,y}=dist_{x,z}=dist_{y,z}\) \(1\leq n\leq 1 ...
- 【bzoj4543】Hotel加强版(thr)
Portal --> bzoj4543 Solution 一年前的题== 然而一年前我大概是在划水qwq 其实感觉好像关键是..设一个好的状态?然后..你要用一种十分优秀的方式快乐转移 ...
- 【POJ1823】【线段树】Hotel
Description The "Informatics" hotel is one of the most luxurious hotels from Galaciuc. A l ...
- 【BZOJ4543】[POI2014]Hotel加强版 长链剖分+DP
[BZOJ4543][POI2014]Hotel加强版 Description 同OJ3522数据范围:n<=100000 Sample Input 7 1 2 5 7 2 5 2 3 5 6 ...
- 【BZOJ3522】[Poi2014]Hotel 树形DP
[BZOJ3522][Poi2014]Hotel Description 有一个树形结构的宾馆,n个房间,n-1条无向边,每条边的长度相同,任意两个房间可以相互到达.吉丽要给他的三个妹子各开(一个)房 ...
随机推荐
- RESTful架构入门
理解RESTful架构 - 阮一峰的网络日志http://www.ruanyifeng.com/blog/2011/09/restful RESTful API 设计指南 - 阮一峰的网络日志http ...
- android 入门-Eclipse 费解的问题
1.第一次打开eclipse的时候 代码程序出好多红点.等待加载项目,如果加载完项目之后仍然存在,请重启eclipse. 2.如果你在创建页面中的button 的时候,设置了android:gravi ...
- phpcms v9最常用的22个调用代码
新源网络工作室友情总结phpcms v9最常用的22个调用代码: 调用最新文章,带所在版块{pc:get sql="SELECT a.title, a.catid, b.catid, b.c ...
- python生成RSS(PyRSS2Gen)
既然能够用python解析rss,那么也顺带研究下生成rss. 其实很简单,只是生成一个比较特殊点的xml文档而已. 这里我使用了PyRss2Gen,用法很简单,看代码就知道了,如下: import ...
- W-数据库基础
数据库系统由三部分组成:数据库(DB).数据库管理系统(DBMS)和数据库应用系统 数据加是用来存储数据的,里面存储两大类数据:用户数据及系统数据/数据字典,具体为系统中的用户以及用户孤权限,各种统计 ...
- Android ImageCache图片缓存,使用简单,支持预取,支持多种缓存算法,支持不同网络类型,扩展性强
本文主要介绍一个支持图片自动预取.支持多种缓存算法的图片缓存的使用及功能.图片较大需要SD卡保存情况推荐使用ImageSDCardCache. 与Android LruCache相比主要特性:(1). ...
- 用Feature的方式删除SharePoint2010的Page中重复的WebPart
用Feature的方式删除SharePoint2010的Page中重复的WebPart. 代码如下所示: public class SupportCenterDuplicatedWebpartRemo ...
- 湖南省第十二届大学生计算机程序设计竞赛 F 地铁 多源多汇最短路
1808: 地铁 Description Bobo 居住在大城市 ICPCCamp. ICPCCamp 有 n 个地铁站,用 1,2,…,n 编号. m 段双向的地铁线路连接 n 个地铁站,其中第 i ...
- 数字信号处理实验(五)——IIR滤波器的设计
一.使用自编函数设计IIR滤波器 1.冲激响应法 (1)注给出的数字滤波器指标先化成模拟指标 (2)设计出模拟滤波器: (3)使用冲激响应法转化成数字滤波器 (4)一个demo clear all; ...
- 【T_SQL】基础 续+
十.模糊查询 1.LIKE --查询时,字段中的内容并不一定与查询内容完全匹配,只要字段中含有这些内容. SELECT StuName AS 姓名 FROM Stuinfo WHERE stuname ...