洛谷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& ...
随机推荐
- 20155316 实验三《敏捷开发与XP实践》实验报告
实验1 实验内容 在IDEA中使用工具(Code->Reformate Code)把下面代码重新格式化,再研究一下Code菜单,找出一项让自己感觉最好用的功能.提交截图,加上自己学号水印. pu ...
- kettle 将job等导入导出成xml
一.导出 工具->资源库->探索资源库 就可以看见资源库里面的资源了. 如果要导出资源库里面的某个目录就右键就行了. 如果要导出全部资源库的文件就如下图所示 将资源库导出其实也是一个xml ...
- c++编译器处理 函数返回值
X bar() { X xx; return xx; } // compiler generated temporary X __temp0; ( bar( __temp0 ), __temp0 ). ...
- header field token is not allowed by Access-Control-Allow-Headers in preflight response问题解决
今天下午,本来打算使用aioxs在header里传一个token给后台服务器,如下图所示: 结果,控制台报了如下的错: 然后,我不停地百度,不停的改后台express的header设置,一直没有效果: ...
- DeepLearning - Overview of Sequence model
I have had a hard time trying to understand recurrent model. Compared to Ng's deep learning course, ...
- python sys.argv是什么?
1.sys.argv 是获取运行python文件的时候命令行参数,且以list形式存储参数 2.sys.argv[0] 代表当前module的名字 下面的代码文件是a.py,当我不用IDE工具,只用命 ...
- NO.2:自学python之路------变量类型、列表、字典
引言 本周初步认识了库,并学习了Python中各种类型的变量和常用操作.并完成了较为完善的用户与商家购物界面设计. 正文 模块: Python有标准库和第三方库.第三方库需要安装才能使用.大量的库可以 ...
- 微软职位内部推荐-Principal Group Program Manager
微软近期Open的职位: Standard job title: Principal Group Program Manager Discipline: Program Management Prod ...
- 第九次ScrumMeeting博客
第九次ScrumMeeting博客 本次会议于11月4日(六)22时整在3公寓725房间召开,持续20分钟. 与会人员:刘畅.辛德泰.窦鑫泽.张安澜.赵奕.方科栋. 1. 每个人的工作(有Issue的 ...
- python3【基础】-字符串 常用的方法
字符串一个最重要的特性就是不可修改. name.capitalize() 首字母大写 name.casefold() 大写全部变小写 name.center(50,"-") 输出 ...