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 & ...
随机推荐
- day14-推导式和生成器表达式
1.推导式规则 [每一个元素或者是和元素相关的操作 for 元素 in 可迭代数据类型] ----------遍历之后挨个处理[满足条件的元素相关的操作 for 元素 in 可迭代数据类型 if 元素 ...
- python2和python3,字典和json
Python2的标准数据类型有: Numbers (数字) String (字符串) List (列表) Tuple (元组) Dictionary (字典) Python3的标准数据类型有: Num ...
- [python学习篇] uiautomator xiaocong
Skip to content This repository Pull requests Issues Marketplace Gist Sign out Watch103 ...
- HDU - 1705 Count the grid
昨天吉老师讲了皮克定理 皮克定理用于计算点阵中顶点在格点上的多边形面积.对于一个顶点全部在格点上的多边形来说,它的面积计算有如下特点: 如果用a表示位于多边形内部的格点数,b表示位于多边形边界上的格点 ...
- Ajax、Comet、Websocket、SSE
从 http 协议说起 1996年IETF HTTP工作组发布了HTTP协议的1.0版本 ,到现在普遍使用的版本1.1,HTTP协议经历了17 年的发展.这种分布式.无状态.基于TCP的请求/响应式 ...
- java 8:I / O 基础
原文地址:https://docs.oracle.com/javase/tutorial/essential/io/index.html 说明:每一个点都有一篇详细的文章与之对应,每翻译完一篇文章会更 ...
- 在Asp.net MVC中应该怎样使用Spring.Net
简单工厂 专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类或接口.简单工厂模式又称为静态工厂方法(Static Factory Method)模式,属于类的创建型模式,通常根据一 ...
- 九度oj 题目1358:陈博的平均主义
题目描述: 在JOBDU团队里,陈博是最讲平均主义的人了,但并不是像梁山好汉那样能够做到有钱同花,有肉同吃,毕竟,他还是被家里的领导管着的……陈博的平均主义,就只能体现在他对数字的喜好了.陈博特别喜欢 ...
- 【Luogu】P3332K大数查询(树套树)
题目链接 这题我费尽心思不用标记永久化终于卡过去了qwq 权值线段树下面套一个区间线段树.然后乱搞搞即可. // luogu-judger-enable-o2 #include<cstdio&g ...
- [LOJ#515]「LibreOJ β Round #2」贪心只能过样例
[LOJ#515]「LibreOJ β Round #2」贪心只能过样例 试题描述 一共有 \(n\) 个数,第 \(i\) 个数 \(x_i\) 可以取 \([a_i , b_i]\) 中任意值. ...