洛谷P2894[USACO08FEB]酒店Hotel(线段树)
问题描述
奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光。作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿。这个巨大的旅馆一共有N (1 <= N <= 50,000)间客房,它们在同一层楼中顺次一字排开,在任何一个房间里,只需要拉开窗帘,就能见到波光粼粼的湖面。 贝茜一行,以及其他慕名而来的旅游者,都是一批批地来到旅馆的服务台,希望能订到D_i (1 <= D_i <= N)间连续的房间。服务台的接待工作也很简单:如果存在r满足编号为r..r+D_i-1的房间均空着,他就将这一批顾客安排到这些房间入住;如果没有满足条件的r,他会道歉说没有足够的空房间,请顾客们另找一家宾馆。如果有多个满足条件的r,服务员会选择其中最小的一个。 旅馆中的退房服务也是批量进行的。每一个退房请求由2个数字X_i、D_i 描述,表示编号为X_i..X_i+D_i-1 (1 <= X_i <= N-D_i+1)房间中的客人全部离开。退房前,请求退掉的房间中的一些,甚至是所有,可能本来就无人入住。 而你的工作,就是写一个程序,帮服务员为旅客安排房间。你的程序一共需要处理M (1 <= M < 50,000)个按输入次序到来的住店或退房的请求。第一个请求到来前,旅店中所有房间都是空闲的。
输入输出格式
输入格式:
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 本题可用线段树维护区间最长连续0的长度,同时记录区间左起最长连续0长度和右起最长连续0长度就可实现区间合并。
program rrr(input,output);
type
treetype=record
l,r,lmax,rmax,max,c:longint;
end;
var
a:array[..]of treetype;
n,m,i,opt,x,y:longint;
function max(a,b:longint):longint;
begin
if a>b then exit(a) else exit(b);
end;
procedure build(k,l,r:longint);
var
mid,t:longint;
begin
t:=r-l+;
a[k].l:=l;a[k].r:=r;a[k].max:=t;a[k].lmax:=t;a[k].rmax:=t;a[k].c:=-;
if l=r then exit;
mid:=(l+r)>>;
build(k+k,l,mid);
build(k+k+,mid+,r);
end;
procedure pushdown(k:longint);
var
i,t:longint;
begin
if a[k].c=- then exit;
if a[k].l=a[k].r then begin a[k].c:=-;exit; end;
i:=k+k;
if a[k].c= then
begin
t:=a[i].r-a[i].l+;
a[i].lmax:=t;a[i].rmax:=t;a[i].max:=t;a[i].c:=;
t:=a[i+].r-a[i+].l+;
a[i+].lmax:=t;a[i+].rmax:=t;a[i+].max:=t;a[i+].c:=;
end
else
begin
a[i].lmax:=;a[i].rmax:=;a[i].max:=;a[i].c:=;
a[i+].lmax:=;a[i+].rmax:=;a[i+].max:=;a[i+].c:=;
end;
a[k].c:=-;
end;
function ask(k:longint):longint;
var
i:longint;
begin
pushdown(k);
i:=k+k;
if a[i].max>=x then exit(ask(i))
else if a[i].rmax+a[i+].lmax>=x then exit(a[i].r-a[i].rmax+)
else exit(ask(i+));
end;
procedure change1(k,x,y:longint);
var
mid,i:longint;
begin
pushdown(k);
if (x<=a[k].l) and (a[k].r<=y) then
begin a[k].lmax:=;a[k].rmax:=;a[k].max:=;a[k].c:=;exit; end;
mid:=(a[k].l+a[k].r)>>;i:=k+k;
if x<=mid then change1(i,x,y);
if mid<y then change1(i+,x,y);
if a[i].lmax=a[i].r-a[i].l+ then a[k].lmax:=a[i].lmax+a[i+].lmax else a[k].lmax:=a[i].lmax;
if a[i+].rmax=a[i+].r-a[i+].l+ then a[k].rmax:=a[i].rmax+a[i+].rmax else a[k].rmax:=a[i+].rmax;
a[k].max:=max(max(a[i].max,a[i+].max),a[i].rmax+a[i+].lmax);
end;
procedure change0(k,x,y:longint);
var
mid,t,i:longint;
begin
pushdown(k);
if (x<=a[k].l) and (a[k].r<=y) then
begin t:=a[k].r-a[k].l+;a[k].lmax:=t;a[k].rmax:=t;a[k].max:=t;a[k].c:=;exit; end;
mid:=(a[k].l+a[k].r)>>;i:=k+k;
if x<=mid then change0(i,x,y);
if mid<y then change0(i+,x,y);
if a[i].lmax=a[i].r-a[i].l+ then a[k].lmax:=a[i].lmax+a[i+].lmax else a[k].lmax:=a[i].lmax;
if a[i+].rmax=a[i+].r-a[i+].l+ then a[k].rmax:=a[i].rmax+a[i+].rmax else a[k].rmax:=a[i+].rmax;
a[k].max:=max(max(a[i].max,a[i+].max),a[i].rmax+a[i+].lmax);
end;
begin
assign(input,'r.in');assign(output,'r.out');reset(input);rewrite(output);
readln(n,m);
build(,,n);
for i:= to m do
begin
read(opt);
if opt= then
begin
readln(x);
if a[].max<x then writeln()
else begin y:=ask();writeln(y);change1(,y,y+x-); end;
end
else begin readln(x,y);change0(,x,x+y-); end;
end;
close(input);close(output);
end.
洛谷P2894[USACO08FEB]酒店Hotel(线段树)的更多相关文章
- 洛谷P2894 [USACO08FEB]酒店Hotel [线段树]
题目传送门 酒店 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and ...
- 洛谷 P2894 [USACO08FEB]酒店Hotel-线段树区间合并(判断找位置,不需要维护端点)+分治
P2894 [USACO08FEB]酒店Hotel 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultur ...
- 洛谷 P2894 [USACO08FEB]酒店Hotel 解题报告
P2894 [USACO08FEB]酒店Hotel 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultur ...
- 洛谷P2894 [USACO08FEB]酒店Hotel
P2894 [USACO08FEB]酒店Hotel https://www.luogu.org/problem/show?pid=2894 题目描述 The cows are journeying n ...
- 洛谷 P2894 [USACO08FEB]酒店Hotel
题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a ...
- 区间连续长度的线段树——洛谷P2894 [USACO08FEB]酒店Hotel
https://www.luogu.org/problem/P2894 #include<cstdio> #include<iostream> using namespace ...
- P2894 [USACO08FEB]酒店Hotel 线段树
题目大意 多次操作 查询并修改区间内长度==len的第一次出现位置 修改区间,变为空 思路 类似于求区间最大子段和(应该是这个吧,反正我没做过) 维护区间rt的 从l开始向右的最长长度 从r开始向左的 ...
- [USACO08FEB]酒店Hotel 线段树
[USACO08FEB]酒店Hotel 线段树 题面 其实就是区间多维护一个lmax,rmax(表示从左开始有连续lmax个空房,一直有连续rmax个空房到最右边),合并时讨论一下即可. void p ...
- 洛谷 P2894 [USACO08FEB]酒店
题目描述 用线段树维护三个值:区间最长空位长度,从左端点可以延伸的最长空位长度,从右端点可以延伸的最长空位长度. #include<complex> #include<cstdio& ...
随机推荐
- 20155328 《Java程序设计》实验一(Java开发环境的熟悉) 实验报告
20155328 <Java程序设计>实验一(Java开发环境的熟悉) 实验报告 一.实验内容及步骤 (一)使用JDK编译.运行简单的java程序 命令行下的程序开发: 打开windows ...
- POI导出excel文件样式
需求: 公司业务和银行挂钩,各种形式的数据之间交互性比较强,这就涉及到了存储形式之间的转换 比如数据库数据与excel文件之间的转换 解决: 我目前使用过的是POI转换数据库和文件之间的数据,下边上代 ...
- spring_cloud多个微服务访问时偶发forward_error问题
1.问题: 最近在做SpringBoot项目的时候,有多个分开的微服务,偶发forward error 问题 2.猜想: 个人理解为服务跳转错误,可能本身没找到目标服务,或者目标服务损坏 3.解决: ...
- 2801 LOL-盖伦的蹲草计划
2801 LOL-盖伦的蹲草计划 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题目描述 Description 众所周知,LOL这款伟大的游戏,有个叫 ...
- GlusterFS学习之路(一)GlusterFS初识
一.GlusterFS简介 GlusterFS是Scale-Out存储解决方案Gluster的核心,它是一个开源的分布式文件系统,具有强大的横向扩展能力,通过扩展能够支持数PB存储容量和处理数千客户端 ...
- bzoj1861 书架
bzoj1861 书架 原题链接 神题... 先吐槽洛谷的样例 10 10 1 3 2 7 5 8 10 4 9 6 Query 3 Top 5 Ask 6 Bottom 3 Ask 3 Top 6 ...
- Mybatis JPA-集成方案+源码
2018-04-18 update 当前文章已过时,请访问代码仓库查看当前版本wiki. github https://github.com/cnsvili/mybatis-jpa gitee htt ...
- Maven学习(六)-----Maven仓库的详细介绍
Maven仓库的详细介绍 在Maven中,任何一个依赖.插件或者项目构建的输出,都可以称之为构件.Maven在某个统一的位置存储所有项目的共享的构件,这个统一的位置,我们就称之为仓库.(仓库就是存放依 ...
- Keil出错解决方法
1.安装KEIL5后创建工程后出现这个报错 解决方法:打开下图目录的文件. Keil.STM32F1xx_DFP.pdsc文件是只读文件,必须将只读属性取消. 如下图所示,注释掉红色圆圈的哪一行,保存 ...
- Phaser3游戏三角学应用--一只跟随屏幕点击位置游动的鱼
fish fish 资源图: fish-136x80.png undersea-bg.png 代码 var config = { type: Phaser.AUTO, parent: 'iFiero' ...