题目描述

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

线段树维护每个节点左边有多少连续的空的,右边有少连续的空的,

最长有多少连续的空的,最长的空位从什么地方开始

#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的更多相关文章

  1. 线段树||BZOJ1593: [Usaco2008 Feb]Hotel 旅馆||Luogu P2894 [USACO08FEB]酒店Hotel

    题面:P2894 [USACO08FEB]酒店Hotel 题解:和基础的线段树操作差别不是很大,就是在传统的线段树基础上多维护一段区间最长的合法前驱(h_),最长合法后驱(t_),一段中最长的合法区间 ...

  2. 洛谷P2894 [USACO08FEB]酒店Hotel

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

  3. P2894 [USACO08FEB]酒店Hotel

    P2894 [USACO08FEB]酒店Hotel 简单的线段树维护区间信息. 维护三个值,一个是从左端点能拓展的长度,一个是从右端点能脱产的的长度.另一个是整个区间内的最大连续零一长度. 记录这三个 ...

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

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

  5. 洛谷P2894 [USACO08FEB]酒店Hotel [线段树]

    题目传送门 酒店 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and ...

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

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

  7. 洛谷 P2894 [USACO08FEB]酒店Hotel

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

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

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

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

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

随机推荐

  1. proc_info_list

    内核中每种处理器架构抽象为一个proc_info_list结构体,在arch/arm/include/asm/procinfo.h中定义, struct proc_info_list { unsign ...

  2. Linux学习-Linux的账号与群组

    使用者识别码: UID 与 GID Linux 主机并不会直接认识 你的"帐号名称"的,他仅认识 ID 啊 (ID 就是一组号码啦). 由于计算机仅认识 0 与 1,所 以主机对于 ...

  3. 大家好,我是一个JAVA初学者,想在这里记下自己学习过程中的点点滴滴,请多多关照

    大家好,我是一个JAVA初学者,想在这里记下自己学习JAVA的点点滴滴,请多多关照. 以前一直在QQ空间里记录的,但感觉有些麻烦,而且有些东西自己理解的并不完善甚至都不正确,现在开始在这里重新记录,从 ...

  4. Python类元编程

    类元编程是指在运行时创建或定制类.在Python中,类是一等对象,因此任何时候都可以使用函数创建新类,而无需用class关键字.类装饰器也是函数,不过能够审查.修改,甚至把被装饰的类替换成其他类.元类 ...

  5. mantisbt邮件配置

    PHP.INI里面 [mail function]; For Win32 only.#SMTP = 192.168.0.249SMTP = smtp.163.comsmtp_port = 25 ; F ...

  6. 【转】javascript操作Select标记中options集合

    先来看看options集合的这几个方法:options.add(option)方法向集合里添加一项option对象:options.remove(index)方法移除options集合中的指定项:op ...

  7. MySQL将内存用在了哪里

    本片文章参考官网讲述MySQL是如何分配内部内存,同时涉及到如何合适设的置内存分配以及如何监控内存的使用情况 官方文档 MySQL在启动时默认被分配给512MB RAM,可以通过设置相关内存参数对其进 ...

  8. hdu6038[找规律+循环节] 2017多校1

    /*hdu6038[找规律+循环节] 2017多校1*/ #include<bits/stdc++.h> using namespace std; typedef long long LL ...

  9. kb-07线段树-06离散化(与第四题类似)

    /* zoj1610 这题是离散化,区间特殊查询的,和之前的第4 题是异曲同工的 */ #include<iostream> #include<cstdio> #include ...

  10. ESXi 给虚拟机添加网络串口

    之前的有点儿小问题,我再更新下: Notice: 要看配没配对,能不能通,得先把虚拟机开开,在关机状态下,这种telnet方式一直是连不通的. 1. 先将ESXi的SSH开启(不知道不开行不行): 2 ...