洛谷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& ...
随机推荐
- 20155325 实验三 敏捷开发与XP实践
实验三 敏捷开发与XP实践-1 http://www.cnblogs.com/rocedu/p/4795776.html, Eclipse的内容替换成IDEA 在IDEA中使用工具(Code-> ...
- PostgreSQL通过mysql_fdw访问MySQL数据库
Mysql与PostgreSQL的安装过程省略. 为简便起见,把MySQL和PostgreSQL都安装在一个机器上,然后在此机器上(准确地说是在PostgreSQL运行的机器上)安装mysql_fdw ...
- CF 959 E. Mahmoud and Ehab and the xor-MST
E. Mahmoud and Ehab and the xor-MST https://codeforces.com/contest/959/problem/E 分析: 每个点x应该和x ^ lowb ...
- 【redis的链接】redis的两种连接方法
执行redis-server /etc/redis.conf开启服务 方法一: [root@zhangmeng ~]# redis-cli > > quit 方法二: [root@zhan ...
- Lambada表达式的作用
Lambda函数的用处 假设你设计了一个地址簿的类.现在你要提供函数查询这个地址簿,可能根据姓名查询,可能根据地址查询,还有可能两者结合.要是你为这些情况都写个函数,那么你一定就跪了.所以你应该提 ...
- sqlite两表更新update
1 2 3 4 5 6 7 8 9 10 11 12 UPDATE t1 SET Column1 = ( SELECT Columnx FROM t2 WHERE t2. KEY = ...
- python全栈开发- 前⽅⾼能-迭代器
python_day_12 今日主要内容 1, 函数名的应用,第一类对象 函数名可以像变量一样进行使用 1.赋值 2.作为list元素 3.作为参数 4.作为返回值 2, 闭包 内部函数访问外部函数的 ...
- 180727-时序数据库InfluxDB之备份和恢复策略
influxdb 备份与恢复 参考: influxdb backup and restore 环境: influxdb v1.6.0 使用influx自动的控制台进行 I. 备份 备份命令 influ ...
- artDialog基本使用
artDialog是一个基于javascript编写的对话框组件,它拥有精致的界面与友好的接口l 自适应内容artDialog的特殊UI框架能够适应内容变化,甚至连外部程序动态插入的内容它仍然能自适 ...
- js判断PC端 移动端 并跳转到对应页面
一.PC端跳转到移动端 html页面: <script>var webroot="/",catid="{$catid}",murl="m/ ...