洛谷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& ...
随机推荐
- 【转载】3D/2D中的D3DXMatrixPerspectiveFovLH和D3DXMatrixOrthoLH投影函数详解
原文:3D/2D中的D3DXMatrixPerspectiveFovLH和D3DXMatrixOrthoLH投影函数详解 3D中z值会影响屏幕坐标系到世界坐标系之间的转换,2D中Z值不会产生影响(而只 ...
- 一篇文章帮你梳理清楚API设计时需要考虑的几个关键点
本文作者是Enchant的架构师,他最近研究了Netflix.SoundCloud.谷歌.亚马逊.Spotify等公司的微服务实践,并根据自己的理解总结出了一套适用于现代Web和云技术的微服务实战经验 ...
- day 14 元组
1. 使用场景? # 列表list 数据类型相同, #rwx文件 100个人的名字, # 用字典 dict ['dɪkt] 很多信息描述1个人, # tuple [ˈtʌpəl] #只读文件 不能修改 ...
- spring源码-aop-5
一.在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发 ...
- 电信NB-IOT的温湿度采集器开发记录
1. 首先打开浏览器,登录电信商用服务器,上传profile文件 2. 上传编解码插件在,注意的是,上传编解码插件是电信测试用服务器平台(不同的网址),反正不明白电信搞啥幺蛾子,得两个地方去上传 3. ...
- T-SQL语句基础
连接服务器 - 去哪个仓库找目标数据库 - 找仓库中的目标区域查找目标表 - 找货柜找数据(以行为基础单位) - 在货柜上找到目标的物品 基础T-Sql语句1.SQL语句的注释 2.创建数据库crea ...
- PHP Redis 缓存数据
// 注:只是在此做下记录,有兴趣的可以参考,不做实际教程文档// 配置文件define('CONFIG', [ 'redis-server' => '127.0.0.1', 'redis-po ...
- ----------BMI指数小程序----------
# 1.创建并输出菜单, 菜单是不可变的. 所以使用元组# menus = ("1, 录入", "2, 查询", "3, 删除", &quo ...
- Zigbee系列(end device)
End device设备分为睡眠和非睡眠两种(RxOnWhenIdle标记不同). 入网时的association请求,会使用这个标记. 共同特性 子节点多次发送数据失败(无回应),发送孤点扫描(re ...
- TCP/IP 网路基础
一.引子 TCP/IP是"Transmission Control Protocol/Internet Protocol"的简写,翻译成中文为传输控制协议/互联网网 ...