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. 《精通Hibernate:Java对象持久化技术详解》目录

    图书信息:孙卫琴 电子工业出版社 第1章 Java应用分层架构及软件模型: 1.1 应用程序的分层体系结构 1.1.1 区分物理层和逻辑层 1.1.2 软件层的特征 1.1.3 软件分层的优点 1.1 ...

  2. php array_intersect() 和 array_diff() 函数

    在PHP中,使用 array_intersect 求两个数组的交集比使用 array_diff 求同样两个数组的并集要快. 如果要求数组 $a 与数组 $b 的差集的个数,应该使用 count($a) ...

  3. WPF PRISM开发入门一( 初始化PRISM WPF程序)

    这篇博客将介绍在WPF项目中引入PRISM框架进行开发的一些基础知识.目前最新的PRISM的版本是Prism 6.1.0,可以在Github上获取PRISM的源码.这个系列的博客将选择PRISM 4. ...

  4. hdu 4044 2011北京赛区网络赛E 树形dp ****

    专题训练 #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm ...

  5. linux设备驱动概述,王明学learn

    linux设备驱动学习-1 本章节主要学习有操作系统的设备驱动和无操作系统设备驱动的区别,以及对操作系统和设备驱动关系的认识. 一.设备驱动的作用 对设备驱动最通俗的解释就是“驱使硬件设备行动” .设 ...

  6. jQuery Mobile Datepicker 使用

    插件引入文件: <meta name="viewport" content="width=device-width, initial-scale=1"&g ...

  7. HDU 5900 QSC and Master 区间DP

    QSC and Master Problem Description   Every school has some legends, Northeastern University is the s ...

  8. 遍历注册表回调函数(仿PCHunter CmpBack)

    遍历注册表回调函数(仿PCHunter CmpBack) typedef struct _CAPTURE_REGISTRY_MANAGER { PDEVICE_OBJECT deviceObject; ...

  9. Arduino101学习(一)——Windows下环境配置

    一.Arduino IDE下载 要开发Arduino 101/Genuino 101,你需要先安装并配置好相应的开发环境.下载地址 http://www.arduino.cn/thread-5838- ...

  10. Java学习笔记(十)——多态

    一.多态 1.对象的多种形态 (1)引用多态: 父类的引用可以指向本类的对象 父类的引用可以指向子类的对象 (2)方法多态: 创建本类对象时,调用的方法为本类方法: 创建子类对象时,调用的方法是子类方 ...