luogu P2894 [USACO08FEB]酒店Hotel
题目描述
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.
输入输出样例
10 6
1 3
1 3
1 3
1 3
2 5 5
1 6
1
4
7
0
5
线段树维护每个节点左边有多少连续的空的,右边有少连续的空的,
最长有多少连续的空的,最长的空位从什么地方开始
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 400010
int sum[N],lm[N],rm[N],m[N],tag[N];
int n,T;
inline void update(int rt)
{
if(m[rt<<]==sum[rt<<]){
lm[rt]=m[rt<<]+lm[rt<<|];
}else {
lm[rt]=lm[rt<<];
}
if(m[rt<<|]==sum[rt<<|]){
rm[rt]=m[rt<<|]+rm[rt<<];
}else {
rm[rt]=rm[rt<<|];
}
m[rt]=max(max(m[rt<<],m[rt<<|]),rm[rt<<]+lm[rt<<|]); }
inline void pushdown(int rt)
{
if(tag[rt]==){
tag[rt<<]=tag[rt<<|]=;
m[rt<<]=lm[rt<<]=rm[rt<<]=;
m[rt<<|]=lm[rt<<|]=rm[rt<<|]=;
}
else {
tag[rt<<]=tag[rt<<|]=;
m[rt<<]=lm[rt<<]=rm[rt<<]=sum[rt<<];
m[rt<<|]=lm[rt<<|]=rm[rt<<|]=sum[rt<<|];
}
tag[rt]=;
}
void build(int l,int r,int rt)
{
lm[rt]=rm[rt]=m[rt]=sum[rt]=r-l+;
tag[rt]=;
if(l==r)return ;
int mid=(l+r)>>;
build(l,mid,rt<<);
build(mid+,r,rt<<|);
} int query(int l,int r,int rt,int len)
{
if(tag[rt]) pushdown(rt);
if(l==r) return l;
int mid=(l+r)>>;
if(m[rt<<]>=len) return query(l,mid,rt<<,len);
if(rm[rt<<]+lm[rt<<|]>=len)return mid-rm[rt<<]+;
else return query(mid+,r,rt<<|,len);
} void modify(int l,int r,int rt,int L,int R,int c)
{
if(tag[rt])pushdown(rt);
if(L<=l&&r<=R){
if(c==) m[rt]=lm[rt]=rm[rt]=;
else m[rt]=lm[rt]=rm[rt]=sum[rt];
tag[rt]=c;
return;
}
int mid=(l+r)>>;
if(L<=mid) modify(l,mid,rt<<,L,R,c);
if(R>mid) modify(mid+,r,rt<<|,L,R,c);
update(rt);
}
int main()
{
scanf("%d%d",&n,&T);
build(,n,);
while(T--)
{
int a,b,gg;
scanf("%d",&gg);
if(gg==)
{
scanf("%d",&a);
if(m[]<a){puts("");continue;}
int p=query(,n,,a);
printf("%d\n",p);
modify(,n,,p,p+a-,);
}else {
scanf("%d%d",&a,&b);
modify(,n,,a,a+b-,);
}
}
return ;
}
luogu P2894 [USACO08FEB]酒店Hotel的更多相关文章
- 线段树||BZOJ1593: [Usaco2008 Feb]Hotel 旅馆||Luogu P2894 [USACO08FEB]酒店Hotel
题面:P2894 [USACO08FEB]酒店Hotel 题解:和基础的线段树操作差别不是很大,就是在传统的线段树基础上多维护一段区间最长的合法前驱(h_),最长合法后驱(t_),一段中最长的合法区间 ...
- 洛谷P2894 [USACO08FEB]酒店Hotel
P2894 [USACO08FEB]酒店Hotel https://www.luogu.org/problem/show?pid=2894 题目描述 The cows are journeying n ...
- P2894 [USACO08FEB]酒店Hotel
P2894 [USACO08FEB]酒店Hotel 简单的线段树维护区间信息. 维护三个值,一个是从左端点能拓展的长度,一个是从右端点能脱产的的长度.另一个是整个区间内的最大连续零一长度. 记录这三个 ...
- 洛谷 P2894 [USACO08FEB]酒店Hotel 解题报告
P2894 [USACO08FEB]酒店Hotel 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultur ...
- 洛谷P2894 [USACO08FEB]酒店Hotel [线段树]
题目传送门 酒店 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and ...
- 区间连续长度的线段树——洛谷P2894 [USACO08FEB]酒店Hotel
https://www.luogu.org/problem/P2894 #include<cstdio> #include<iostream> using namespace ...
- 洛谷 P2894 [USACO08FEB]酒店Hotel
题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a ...
- P2894 [USACO08FEB]酒店Hotel 线段树
题目大意 多次操作 查询并修改区间内长度==len的第一次出现位置 修改区间,变为空 思路 类似于求区间最大子段和(应该是这个吧,反正我没做过) 维护区间rt的 从l开始向右的最长长度 从r开始向左的 ...
- 洛谷P2894[USACO08FEB]酒店Hotel(线段树)
问题描述 奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光.作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿.这个巨大的旅馆一共有N (1 <= N & ...
随机推荐
- 《linux设备驱动开发详解》笔记——12linux设备驱动的软件架构思想
本章重点讲解思想.思想.思想. 12.1 linux驱动的软件架构 下述三种思想,在linux的spi.iic.usb等复杂驱动里广泛使用.后面几节分别对这些思想进行详细说明. 思想1:驱动与设备分离 ...
- 实验二 JSP基本动态元素的使用
实验二 JSP基本动态元素的使用 实验性质:验证性 实验学时: 2学时 实验地点: 一 .实验目的与要求 1.掌握JSP中声明变量.定义方法.java程序片及表达式的使 ...
- Developing for nRF52810(转载)
Table of Contents Introduction Hardware emulation of nRF52810 Limitations Software emulation of nRF5 ...
- CentOS 7 安装 配置 Nginx + PHP
. CentOS 7 下配置 yum 安装 Nginx. 进入/etc/yum.repos.d/目录下创建一个源配置文件nginx.repo: cd /etc/yum.repos.d/ vim ngi ...
- iOS 中的视图函数 init initwithnib viewDidLoad viewWillAppear的总结
我要总结的函数主要是这几个: UIView *view-如果view还没有被初始化的话,getter方法会先调用[self loadView],如果getter或者setter方法被重写了,子类中的g ...
- 欧拉函数:HDU1787-GCD Again(欧拉函数的模板)
GCD Again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- bash函数定义/使用/传参…
函数:function, 功能 过程式编程,代码重用 模块化编程 简洁 语法: function f_name { ...
- 金阳光Android自动化测试第一季
第一季:http://www.chuanke.com/v1983382-106000-218422.html 第一节:Android自动化预备课程基础(上) 1. 基于坐标点触屏:monkey ...
- HBase0.94.2-cdh4.2.0需求评估测试报告1.0之三
1.1.1 测试记录 第一组:一个列,一个分区,顺序ID 测试列和分区 测试程序或命令 导入文件大小(Mb) 导入文件个数(个) 是否触发flush事件(布尔) 是否触发compact事件(布尔) 触 ...
- UML结构与解析——BUAA OO第四单元作业总结
UML与解析架构 UML是什么 统一建模语言(英语:Unified Modeling Language,缩写 UML)是非专利的第三代建模和规约语言.UML是一种开放的方法,用于说明.可视化.构建和编 ...