问题描述

奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光。作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿。这个巨大的旅馆一共有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.

输入输出样例

输入样例#1:

10 6
1 3
1 3
1 3
1 3
2 5 5
1 6
输出样例#1:

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(线段树)的更多相关文章

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

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

  2. 洛谷 P2894 [USACO08FEB]酒店Hotel-线段树区间合并(判断找位置,不需要维护端点)+分治

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

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

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

  4. 洛谷P2894 [USACO08FEB]酒店Hotel

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

  5. 洛谷 P2894 [USACO08FEB]酒店Hotel

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

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

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

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

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

  8. [USACO08FEB]酒店Hotel 线段树

    [USACO08FEB]酒店Hotel 线段树 题面 其实就是区间多维护一个lmax,rmax(表示从左开始有连续lmax个空房,一直有连续rmax个空房到最右边),合并时讨论一下即可. void p ...

  9. 洛谷 P2894 [USACO08FEB]酒店

    题目描述 用线段树维护三个值:区间最长空位长度,从左端点可以延伸的最长空位长度,从右端点可以延伸的最长空位长度. #include<complex> #include<cstdio& ...

随机推荐

  1. 成都Uber优步司机奖励政策(4月16日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  2. 4825: [Hnoi2017]单旋

    4825: [Hnoi2017]单旋 链接 分析: 以后采取更保险的方式写代码!!!81行本来以为不特判也可以,然后就总是比答案大1,甚至出现负数,调啊调啊调啊调~~~ 只会旋转最大值和最小值,以最小 ...

  3. httpclient在获取response的entity时报异常

    httpClient报异常:Premature end of chunk coded message body: closing chunk expected 首先这个异常提示直译过来就是:被编码信息 ...

  4. springboot中maven加入本地jar

    一.今天遇到一个问题,在使用springboot打jar的时候出现了本地依赖包打不进去的情况.然后在网上试了很多方式.这里做一个记录 二.加入本地依赖包 <dependency> < ...

  5. 利用PreparedStatement预防SQL注入

    1.什么是sql注入 SQL 注入是用户利用某些系统没有对输入数据进行充分的检查,从而进行恶意破坏的行为. 例如登录用户名采用  ' or 1=1 or username=‘,后台数据查询语句就变成 ...

  6. 第六章P2P技术及应用

    第六章P2P技术及应用 P2P技术在我们日常生活中非常实用,例如我们常用的QQ.PPLive.BitTorrent就是基于P2P技术研发.下面将本章中的重点内容进行归纳. 文章中的Why表示产生的背景 ...

  7. JMeter:全面的乱码解决方案【转】

    本文是转自https://www.cnblogs.com/mawenqiangios/p/7918583.html 感谢分享者   中文乱码一直都是比较让人棘手的问题,我们在使用Jmeter的过程中, ...

  8. PHASER3 设置场景SCENE SLEEPING休眠和WAKE唤醒

    A good way to set scene stop when hidden and run while visible again ! 使用sleep和wake方法的好处: 1.可以彻底让sce ...

  9. day06 再谈编码 and 作业讲解

    1. 小数据池,(其他语言又叫常量池) id() 查看变量的内存地址 is和== is 判断内存地址是否一致 == 判断内容是否一致 小数据池的作用: 为了快速的创建字符串对象, 可以减少内存的浪费 ...

  10. 『ACM C++』PTA浙大 | 基础题 - Have Fun with Numbers

    连着这两道都是开学前数构老师的“爱心作业”,还没上课开学就给我们布置作业了,这道题有点小坑,也经常遇到类似的问题,特地拿出来记录一下. -------------------------------- ...