【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条无向边,每条边的长度相同,任意两个房间可以相互到达.吉丽要给他的三个妹子各开(一个)房 ...
随机推荐
- SSAS Cube 维度成员关系Rigid 和 Flexible
维度成员关系指示成员关系是否随时间而更改. 值为 Rigid 和 Flexible,前者表示成员之间的关系不随时间而更改,后者表示成员之间的关系随时间而更改. 默认值为 Flexible. 指定适 ...
- bootstrap表单带验证
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...
- ImageSwitcher自定意效果+定时切换图片
Activity实现 1 import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; ...
- hdu 4061 福州赛区网络赛A 数学 ***
a1/sum #include<cstdio> #include<iostream> #include<algorithm> #include<cstring ...
- 同一个项目,项目名称不一致,这两个项目同时在Eclipse中出现
在Eclispse中,实际同一个项目,项目名称不一致,这两个项目同时在Eclipse中出现. ①打开项目文件夹,找到“.cproject”文件 ② 在<name>节点重命名 ③ 导入Ecl ...
- 【RQNOJ356】myt的格斗
题目描述 ’恩 ~~这个和这个也是朋友.把他们放在一起......哇!终于完成了’mty费了好大劲,终于找出了一支最为庞大的军队. fyc很高兴,立马出征与人fight.mty万万没想到fyc竟然把他 ...
- whl文件安装
进入whl文件的目录,直接pip install ...即可
- Linux学习笔记(6)Linux常用命令之帮助命令与用户管理命令
(1)man man命令用于获得命令或配置文件的帮助信息,英文原意为manual,所在路径为/usr/bin/man,其语法格式为: man [命令或配置文件] 注意:查看配置文件的帮助信息时无需绝对 ...
- AChartEngine使用View显示图表
学习过AChartEngine的人肯定都知道,使用ChartFactory创建一张图表可以使用Intent方法,之后调用StartActivity来启用这个Intent,但是这么左右一个坏处,就是当你 ...
- css学习(1)-- 层叠样式表CSS的基础
一.什么是CSS CSS是Cascading Style Sheets的简写,它除了可以轻松设置网页元素的显示位置和格式处,甚至还能产生滤镜,图像淡化,网页淡入淡出的渐变效果. 一个样式表由样式规则组 ...