题目传送门

酒店

题目描述

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.

参考样例,第一行输入n,m ,n代表有n个房间,编号为1---n,开始都为空房,m表示以下有m行操作,以下 每行先输入一个数 i ,表示一种操作:

若i为1,表示查询房间,再输入一个数x,表示在1--n 房间中找到长度为x的连续空房,输出连续x个房间中左端的房间号,尽量让这个房间号最小,若找不到长度为x的连续空房,输出0。

若i为2,表示退房,再输入两个数 x,y 代表 房间号 x---x+y-1 退房,即让房间为空。

输入输出格式

输入格式:

* 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

输出格式:

* 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.

输入输出样例

输入样例#1:

10 6
1 3
1 3
1 3
1 3
2 5 5
1 6
输出样例#1:

1
4
7
0
5

  分析:

  可以说这一类题目就是检验自己线段树是否真正理解透彻的测验。

  对于这题,我们需要维护四个信息,$sum$表示这一段最大的连续空房间数,$sl$表示这一段从左数的最大连续空房间数,$sr$表示这一段从右数的最大连续空房间数,$sign$表示这段区间是否被修改。

  在维护信息时,$pushup$操作如下,详细解释在代码中:

inline void pushup(int rt)
{
if( t[ls].sum==t[ls].len )
t[rt].sl=t[ls].sum+t[rs].sl;//如果左子树全为空的情况
else
t[rt].sl=t[ls].sl;
if( t[rs].sum==t[rs].len )
t[rt].sr=t[rs].sum+t[ls].sr;//右子树全为空的情况
else
t[rt].sr=t[rs].sr;
t[rt].sum=max(max(t[ls].sum,t[rs].sum),t[ls].sr+t[rs].sl);
//求最大连续空房间数。
}

  $pushdown$操作如下:

inline void pushdown(int rt)
{
if( t[rt].sign== ) return;
if( t[rt].sign== ) {//如果sign为1,则房间已经被占用了
t[ls].sum=t[ls].sl=t[ls].sr=;
t[rs].sum=t[rs].sl=t[rs].sr=;
t[ls].sign=t[rs].sign=;
}
if( t[rt].sign==- ) {//如果sign为-1,则房间要空出来
t[ls].sum=t[ls].sl=t[ls].sr=t[ls].len;
t[rs].sum=t[rs].sl=t[rs].sr=t[rs].len;
t[ls].sign=t[rs].sign=-;
}
t[rt].sign=;
}

  再结合全部代码应该就很好理解了。

  Code:

//It is made by HolseLee on 5th Nov 2018
//Luogu.org P2894
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ls (rt<<1)
#define rs (rt<<1|1)
using namespace std; const int N=1e5+;
int n,m;
struct Segment {
int sum,sl,sr,len,sign;
}t[N<<]; inline int read()
{
char ch=getchar(); int x=; bool flag=false;
while( ch<'' || ch>'' ) {
if( ch=='-' ) flag=true; ch=getchar();
}
while( ch>='' && ch<='' ) {
x=x*+ch-''; ch=getchar();
}
return flag ? -x : x;
} inline void pushup(int rt)
{
if( t[ls].sum==t[ls].len )
t[rt].sl=t[ls].sum+t[rs].sl;
else
t[rt].sl=t[ls].sl;
if( t[rs].sum==t[rs].len )
t[rt].sr=t[rs].sum+t[ls].sr;
else
t[rt].sr=t[rs].sr;
t[rt].sum=max(max(t[ls].sum,t[rs].sum),t[ls].sr+t[rs].sl);
} inline void pushdown(int rt)
{
if( t[rt].sign== ) return;
if( t[rt].sign== ) {
t[ls].sum=t[ls].sl=t[ls].sr=;
t[rs].sum=t[rs].sl=t[rs].sr=;
t[ls].sign=t[rs].sign=;
}
if( t[rt].sign==- ) {
t[ls].sum=t[ls].sl=t[ls].sr=t[ls].len;
t[rs].sum=t[rs].sl=t[rs].sr=t[rs].len;
t[ls].sign=t[rs].sign=-;
}
t[rt].sign=;
} void build(int l,int r,int rt)
{
t[rt].sign=;
t[rt].sum=t[rt].sl=t[rt].sr=t[rt].len=r-l+;
if( l==r ) return;
int mid=(l+r)>>;
build(l,mid,ls); build(mid+,r,rs);
} void update(int l,int r,int rt,int L,int R,int C)
{
pushdown(rt);
if( L<=l && r<=R ) {
if( C== ) t[rt].sum=t[rt].sl=t[rt].sr=;
else t[rt].sum=t[rt].sl=t[rt].sr=t[rt].len;
t[rt].sign=C;
return;
}
int mid=(l+r)>>;
if( L<=mid ) update(l,mid,ls,L,R,C);
if( R>mid ) update(mid+,r,rs,L,R,C);
pushup(rt);
} int query(int l,int r,int rt,int C)
{
pushdown(rt);
if( l==r ) return l;
int mid=(l+r)>>;
if( t[ls].sum>=C ) return query(l,mid,ls,C);
else if( t[ls].sr+t[rs].sl>=C ) return (mid-t[ls].sr+);
else return query(mid+,r,rs,C);
} int main()
{
n=read(), m=read();
int opt,x,y;
build(,n,);
for(int i=; i<=m; ++i) {
opt=read();
if( opt== ) {
x=read();
if( t[].sum<x ) { puts(""); continue; }
printf("%d\n",y=query(,n,,x));
update(,n,,y,y+x-,);
} else {
x=read(), y=read();
update(,n,,x,x+y-,-);
}
}
return ;
}

洛谷P2894 [USACO08FEB]酒店Hotel [线段树]的更多相关文章

  1. 洛谷 P2894 [USACO08FEB]酒店Hotel-线段树区间合并(判断找位置,不需要维护端点)+分治

    P2894 [USACO08FEB]酒店Hotel 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultur ...

  2. 洛谷 P2894 [USACO08FEB]酒店Hotel 解题报告

    P2894 [USACO08FEB]酒店Hotel 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultur ...

  3. 洛谷P2894 [USACO08FEB]酒店Hotel

    P2894 [USACO08FEB]酒店Hotel https://www.luogu.org/problem/show?pid=2894 题目描述 The cows are journeying n ...

  4. 洛谷P2894[USACO08FEB]酒店Hotel(线段树)

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

  5. 洛谷 P2894 [USACO08FEB]酒店Hotel

    题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a ...

  6. 区间连续长度的线段树——洛谷P2894 [USACO08FEB]酒店Hotel

    https://www.luogu.org/problem/P2894 #include<cstdio> #include<iostream> using namespace ...

  7. P2894 [USACO08FEB]酒店Hotel 线段树

    题目大意 多次操作 查询并修改区间内长度==len的第一次出现位置 修改区间,变为空 思路 类似于求区间最大子段和(应该是这个吧,反正我没做过) 维护区间rt的 从l开始向右的最长长度 从r开始向左的 ...

  8. [USACO08FEB]酒店Hotel 线段树

    [USACO08FEB]酒店Hotel 线段树 题面 其实就是区间多维护一个lmax,rmax(表示从左开始有连续lmax个空房,一直有连续rmax个空房到最右边),合并时讨论一下即可. void p ...

  9. 洛谷 P2894 [USACO08FEB]酒店

    题目描述 用线段树维护三个值:区间最长空位长度,从左端点可以延伸的最长空位长度,从右端点可以延伸的最长空位长度. #include<complex> #include<cstdio& ...

随机推荐

  1. 洛谷 P2607 [ZJOI2008]骑士 解题报告

    P2607 [ZJOI2008]骑士 题目描述 Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬. 最近发生了一件可怕的事情,邪恶的Y国发动了一 ...

  2. Tomcat:IOException while loading persisted sessions: java.io.EOFException

    Tomcat:IOException while loading persisted sessions: java.io.EOFException 产生原因: 最近将项目名称修改了一下,然后启动项目, ...

  3. WEB下载显示下载名称乱码--java

    将文件名编码转换为ISO8859-1即可,如下 downloadFileName = new String(fileName.getBytes("gbk"), "ISO8 ...

  4. Java后台面试 常见问题

    Java后台面试 常见问题   从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米.百度.阿里.京东.新浪.CVTE.乐视家的研发岗offer.我找的是java后台开发,把常见的问题分享 ...

  5. 浅谈使用NIO,AIO的感受

    花了十多天的时间把原来的WEB服务由BIO(阻塞IO)模式改写成NIO(非阻塞IO)模式,然后在xp机子上用ab测试并发性能,确实提升了30%左右的并发性能,测试完成后,当时感觉还是挺满意的.几天前在 ...

  6. Redis与memecache的区别

    转载连接: https://www.biaodianfu.com/redis-vs-memcached.html Redis的作者Salvatore Sanfilippo曾经对这两种基于内存的数据存储 ...

  7. Shell中的case命令

    case语句和判断语句[if...elif...else]功能类似;当在逻辑判断比较简单的情况下,比后者的代码量要少许多.case用法,用变量来匹配某值,如果匹配成功则执行它下面的命令,直到 ::为止 ...

  8. (64位)本体学习程序(ontoEnrich)系统使用说明文档

    系统运行:文件夹system下,可执行文件ontoEnrichment 概念学习 --------------------------------------------------------1.简 ...

  9. [整理]win7下VS2010遇到内存不足解决方发

    电脑重装Win7 64bit不久后,一天内VS2010使用久了,就会出现内存不足,实际内存使用情况却不是,显示内存已使用70%.以前没有遇到过,经网上查找,貌似是VS2010对内存计算会在某些情况下计 ...

  10. 20145234黄斐《Java程序设计》第八周

    教材学习内容总结 第十四章-NIO与NIO2 NIO与IO的区别 NIO Channel继承框架 想要取得Channel的操作对象,可以使用Channels类,它定义了静态方法newChannel() ...