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. 【翻译一】java-并发

    Lesson: Concurrency Computer users take it for granted that their systems can do more than one thing ...

  2. 【JAVA正则表达式综合练习】

    一.治疗口吃. 将字符串“我我我我我我我..........我.......要要要要要..................要要要要...学习习习习.......习习习习习习习习编程程程程程程..... ...

  3. [Nodejs]十分钟快速编写简单静态文件服务器

    学了几天Nodejs 后我又干上了前端的活.这次遇到的问题是,我想在不同的设备上方便的查看我编写的网页,很自然的就想到要是能在本地搭建一个简单的http服务器的话,那局域网内的所有设备都可以访问了,这 ...

  4. mysql的启动

    1.直接用mysqld手工启动 [root@ora11g bin]# ./mysqld --defaults-file=../my.cnf :: [ERROR] Fatal error: Please ...

  5. 批量删除SharePoint 2010的List中的item

    第一种方式:循环遍历List中的所有item,然后根据条件去判断当前item是否应该被删除[注:要用 i-- 方式去遍历,这也是删除集合里面item的常用做法,如果用 i++ 的方式去遍历删除,会出错 ...

  6. C语言中运算符的口决

  7. 深入解析结构化异常处理(SEH)

    jpg 改 rar

  8. 【java 上传+下载】

    一.先说说上传 第一步:pom.xml文件 加上 上传文件依赖架包 <dependency> <groupId>commons-fileupload</groupId&g ...

  9. PHPExcel设置数据格式的几种方法

    转自:http://www.cnblogs.com/guangxiaoluo/archive/2013/11/19/3431846.html 解决 PHPExcel 长数字串显示为科学计数 在exce ...

  10. Linux中查看jdk版本

    linux查看java jdk安装路径和设置环境变量 windows: set java_home:查看JDK安装路径 java -version:查看JDK版本 linux: whereis jav ...