Description

The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shores of Lake Superior. Bessie, ever the competent travel agent, has named the Bullmoose Hotel on famed Cumberland Street as their vacation residence. This immense hotel has N (1 ≤ N ≤ 50,000) rooms all located on the same side of an extremely long hallway (all the better to see the lake, of course).

The cows and other visitors arrive in groups of size Di (1 ≤ Di ≤ N) and approach the front desk to check in. Each group i requests a set of Di contiguous rooms from Canmuu, the moose staffing the counter. He assigns them some set of consecutive room numbers r..r+Di-1 if they are available or, if no contiguous set of rooms is available, politely suggests alternate lodging. Canmuu always chooses the value of r to be the smallest possible.

Visitors also depart the hotel from groups of contiguous rooms. Checkout i has the parameters Xi and Di which specify the vacating of rooms Xi ..Xi +Di-1 (1 ≤ Xi ≤N-Di+1). Some (or all) of those rooms might be empty before the checkout.

Your job is to assist Canmuu by processing M (1 ≤ M < 50,000) checkin/checkout requests. The hotel is initially unoccupied.

Input

* 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 D(b) Three space-separated integers representing a check-out: 2, Xi, and Di

Output

* 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.

Sample Input

10 6
1 3
1 3
1 3
1 3
2 5 5
1 6

Sample Output

1
4
7
0
5

Source

题目大意:有n个房间m个操作,每次操作有两中可能1.读入:1 a,查找长度为a的连续的房间(每个房间都是可入住的),输出最左端的编号,这长度为a的每个房间都入住即不能再用。2.读入2 a b,退房操作,以a为起点的b个房间都变为可入住。
做法:线段树区间合并模板,每个节点储存连续的最大值,两端区间合并时左区间的右端和右区间的左端也可以成为最大值。
注意:数据略水,可能没人入住就退房= =,然并卵。
第一次认真的对拍,对着一个ac的程序不断拍,第一次:updata时左儿子打成右儿子了,30min后,忘了把改完的程序换到对拍的文件夹了,。。。a了。
 #include <iostream>
#include <cstdio>
#include <cstring>
#define N 100000
using namespace std;
struct data{int sl,sr,maxs,ls,rs;}seg[N*];
int lazy[N*];
int n,m;
void pushsign(int now)
{
if (lazy[now]==)
{
seg[(now<<)].maxs=seg[(now<<)].ls=seg[(now<<)].rs=seg[(now<<)].sr-seg[(now<<)].sl+;
lazy[(now<<)]=lazy[now];
seg[(now<<)+].maxs=seg[(now<<)+].ls=seg[(now<<)+].rs=seg[(now<<)+].sr-seg[(now<<)+].sl+;
lazy[(now<<)+]=lazy[now];
lazy[now]=;
}
else if (lazy[now]==)
{
seg[(now<<)].maxs=seg[(now<<)].ls=seg[(now<<)].rs=;
lazy[(now<<)]=lazy[now];
seg[(now<<)+].maxs=seg[(now<<)+].ls=seg[(now<<)+].rs=;
lazy[(now<<)+]=lazy[now];
lazy[now]=;
}
}
void adddata(int now)
{
seg[now].maxs=seg[(now<<)].rs+seg[(now<<)+].ls;
seg[now].maxs=max(max(seg[(now<<)].maxs,seg[(now<<)+].maxs),seg[now].maxs);
seg[now].ls=seg[(now<<)].ls;
if (seg[(now<<)].ls==seg[(now<<)].sr-seg[(now<<)].sl+) seg[now].ls+=seg[(now<<)+].ls;
seg[now].rs=seg[(now<<)+].rs;
if (seg[(now<<)+].rs==seg[(now<<)+].sr-seg[(now<<)+].sl+) seg[now].rs+=seg[(now<<)].rs;
}
void buildtree(int now,int l,int r)
{ seg[now].sl=l; seg[now].sr=r; seg[now].maxs=seg[now].ls=seg[now].rs=r-l+;
if (l==r) return;
int mid=(l+r)>>;
buildtree((now<<),l,mid);
buildtree((now<<)+,mid+,r);
adddata(now);
}
int query(int now,int lon)
{
int l=seg[now].sl,r=seg[now].sr;
if (l==r) return l;
pushsign(now);
int pos=;
if (seg[(now<<)].ls>=lon) return seg[(now<<)].sl;
else if (seg[(now<<)].maxs>=lon) pos=query((now<<),lon);
else if (seg[(now<<)].rs+seg[(now<<)+].ls>=lon &&seg[(now<<)].rs!=) return seg[(now<<)].sr-seg[(now<<)].rs+;
else if (seg[(now<<)+].maxs>=lon)pos=query((now<<)+,lon);
else if (seg[now].maxs==r-l+) return l;
return pos;
}
void ichange1(int now,int begin,int end)
{
int l=seg[now].sl,r=seg[now].sr;
if (begin<=l && end>=r) {seg[now].maxs=seg[now].ls=seg[now].rs=r-l+; lazy[now]=; return;}
pushsign(now);
int mid=(l+r)>>;
if (begin<=mid) ichange1((now<<),begin,end);
if (end>mid) ichange1((now<<)+,begin,end);
adddata(now);
}
void ichange2(int now,int begin,int end)
{
int l=seg[now].sl,r=seg[now].sr;
if (begin<=l && end>=r) {seg[now].maxs=seg[now].ls=seg[now].rs=; lazy[now]=; return;}
pushsign(now);
int mid=(l+r)>>;
if (begin<=mid) ichange2((now<<),begin,end);
if (end>mid) ichange2((now<<)+,begin,end);
adddata(now);
}
int main()
{
int i;
int a,b,p;
while (~scanf("%d%d",&n,&m))
{
buildtree(,,n);
for (i=;i<=m;i++)
{
scanf("%d",&p);
if (p==)
{
scanf("%d",&a);
if (seg[].maxs<a) {printf("0\n");continue;}
int pos=query(,a);
printf("%d\n",pos);
ichange2(,pos,pos+a-);
}
else
{
scanf("%d%d",&a,&b);
ichange1(,a,a+b-);
}
}
}
//system("pause");
return ;
}

【POJ3667】Hotel的更多相关文章

  1. 【POJ3667】Hotel(线段树)

    题意:有n个依次编号的元素,要求维护以下两个操作: 1.询问整个数列中是否有长度>=x的连续的一段未被标记的元素,若无输出0,若有输出最小的开始编号ans并将[ans,ans+x-1]标记 2. ...

  2. 【BZOJ4543】Hotel加强版(长链剖分)

    [BZOJ4543]Hotel加强版(长链剖分) 题面 BZOJ,没有题面 洛谷,只是普通版本 题解 原来我们的\(O(n^2)\)做法是设\(f[i][j]\)表示以\(i\)为根的子树中,距离\( ...

  3. 【BZOJ4543】Hotel加强版

    [BZOJ4543]Hotel加强版 题面 bzoj 洛谷 $ps:$在洛谷看题在bzoj交... 题解 我们分析一下这个问题,要怎么样的点才满足三点距离两两相等呢? 1.存在三个点有共同的$LCA$ ...

  4. 【BZOJ】【3522】【POI2014】Hotel

    暴力/树形DP 要求在树上找出等距三点,求方案数,那么用类似Free Tour2那样的合并方法,可以写出: f[i][j]表示以 i 为根的子树中,距离 i 为 j 的点有多少个: g[i][j]表示 ...

  5. 【BZOJ3522】【BZOJ4543】【POI2014】Hotel 树形DP 长链剖分 启发式合并

    题目大意 ​ 给你一棵树,求有多少个组点满足\(x\neq y,x\neq z,y\neq z,dist_{x,y}=dist_{x,z}=dist_{y,z}\) ​ \(1\leq n\leq 1 ...

  6. 【bzoj4543】Hotel加强版(thr)

    Portal --> bzoj4543 Solution ​ 一年前的题== 然而一年前我大概是在划水qwq ​​ 其实感觉好像关键是..设一个好的状态?然后..你要用一种十分优秀的方式快乐转移 ...

  7. 【POJ1823】【线段树】Hotel

    Description The "Informatics" hotel is one of the most luxurious hotels from Galaciuc. A l ...

  8. 【BZOJ4543】[POI2014]Hotel加强版 长链剖分+DP

    [BZOJ4543][POI2014]Hotel加强版 Description 同OJ3522数据范围:n<=100000 Sample Input 7 1 2 5 7 2 5 2 3 5 6 ...

  9. 【BZOJ3522】[Poi2014]Hotel 树形DP

    [BZOJ3522][Poi2014]Hotel Description 有一个树形结构的宾馆,n个房间,n-1条无向边,每条边的长度相同,任意两个房间可以相互到达.吉丽要给他的三个妹子各开(一个)房 ...

随机推荐

  1. ASP.NET Web Api 服务器端变了,客户端该如何修改请求(转载)

    转载地址:http://www.cnblogs.com/fzrain/p/3558765.html 前言 一旦我们将API发布之后,消费者就会开始使用并和其他的一些数据混在一起.然而,当新的需求出现时 ...

  2. Arch Linux Installation Guide

    Arch Linux Installation Guide   timedatectl set-ntp true   sed -i '/Score/{/China/!{n;s/^/#/}}' /etc ...

  3. 【翻译四】java-并发之线程暂停

    Pausing Execution with Sleep Thread.sleep causes the current thread to suspend execution for a speci ...

  4. WCF批量打开服务

    WCF服务.利用循环,读取配置文件,打开所有的代理服务 和关闭代理服务的方法 //list列表 ,用于存储打开的服务列表 List<ServiceHost> _host = new Lis ...

  5. PHP json_encode中文乱码解决方法

    相信很多人在使用Ajax与后台php页面进行交互的时候都碰到过中文乱码的问题.JSON作为一种轻量级的数据交换格式,备受亲睐,但是用PHP作为后台交互,容易出现中文乱码的问题.JSON和js一样,对于 ...

  6. hdu 4031 2011成都赛区网络赛A题 线段树 ***

    就是不知道时间该怎么处理,想了好久,看了别人的题解发现原来是暴力,暴力也很巧妙啊,想不出来的那种  -_-! #include<cstdio> #include<iostream&g ...

  7. Ubuntu14.04LTS系统QQ的安装:pidgin-lwqq

    本人是轻度聊天工具使用者(大言不惭是轻度,偷笑),发现输入法到博主也有解决linux下QQ的解决方法,一并抄过来,有需要,请联系原作者 参考链接:http://www.cnblogs.com/zhj5 ...

  8. 配置ogg异构oracle-mysql(2)源端配置

    源端配置大致分为如下三个步骤:配置mgr,配置抽取进程,配置投递进程 在源端先创建一张表,记得带主键: SQL> create table ah4(id int ,name varchar(10 ...

  9. view的setTag() 和 getTag()应用 (转)

    原文地址:http://www.cnblogs.com/qingblog/archive/2012/07/03/2575140.html View中的setTag(Onbect)表示给View添加一个 ...

  10. 编译原理实习(应用预测分析法LL(1)实现语法分析)

    #include<iostream> #include<fstream> #include<iomanip> #include<cstdio> #inc ...