Hotel

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 ≤ XiN-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 Di (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

题意:有一个旅店,刚开始有一段连续的房间,有两种操作,1、预定房间,找最靠前的一段连续D个房间(如果存在的话);2、退订房间,将[x,x+D-1]区间的房间退订。 解析:线段树维护,每个节点维护以下几个信息:左、右端点(le,ri),区间长度(len),从左边开始的最大连续长度(lelen),从右边开始的最大长度(rilen),该区间内最大连续长度(maxlen)。是否被预定用1和0表示。每次pushup(更新)时,更新
lelen,rilen,maxlen.首先lelen=lson.lelen(左儿子的),如果lelen==lson.len,说明可以向右扩展,那么lelen+=rson.lelen;rilen同理更新,maxlen=max{lson.maxlen,rson.maxlen,lelen,rilen,lson.rilen+rson.lelen};
有了这些信息,就很好去查找答案。 代码如下:
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iterator>
#include<utility>
#include<sstream>
#include<iostream>
#include<cmath>
#include<stack>
using namespace std;
const int INF=;
const double eps=0.00000001;
const int maxn=;
int N,M;
struct node
{
int le,ri,len;
int maxlen,lelen,rilen; //区间最大长度,左边最大长度,右边最大长度
void init(int state) //根据是否被标记更新
{
maxlen=state*len;
lelen=rilen=maxlen;
}
}tree[*maxn];
void build_tree(int le,int ri,int id) //初始化
{
tree[id].le=le,tree[id].ri=ri;
tree[id].len=ri-le+;
tree[id].init();
if(le==ri) return;
int mid=(le+ri)/;
build_tree(le,mid,id*);
build_tree(mid+,ri,id*+);
return;
}
void pushdown(int id)
{
node& t=tree[id];
if(t.maxlen==t.len||t.maxlen==) //全被预定或是全未预定
{
int state=(t.maxlen==t.len);
tree[id*].init(state);
tree[id*+].init(state);
}
}
void pushup(int id)
{
node& fa=tree[id]; //父亲
node& lson=tree[id*]; //左儿子
node& rson=tree[id*+]; //右儿子
fa.lelen=lson.lelen;
if(fa.lelen==lson.len) fa.lelen+=rson.lelen; //可扩展
fa.rilen=rson.rilen;
if(fa.rilen==rson.len) fa.rilen+=lson.rilen;
fa.maxlen=max(lson.maxlen,rson.maxlen); //更新maxlen
fa.maxlen=max(fa.maxlen,max(fa.lelen,fa.rilen));
fa.maxlen=max(fa.maxlen,lson.rilen+rson.lelen);
}
int query(int id,int need)
{
if(tree[id].maxlen<need) return ; //无解
if(tree[id].lelen>=need) return tree[id].le; //最左边可行
if(tree[id*].maxlen>=need) return query(id*,need); //往左边找
if(tree[id*].rilen+tree[id*+].lelen>=need) return tree[id*].ri-tree[id*].rilen+; //中间
return query(id*+,need); //右边
}
void update(int x,int y,int id,int state)
{
int le=tree[id].le,ri=tree[id].ri;
if(x<=le&&ri<=y){ tree[id].init(state); return; }
pushdown(id);
int mid=(le+ri)/;
if(x<=mid) update(x,y,id*,state);
if(y>mid) update(x,y,id*+,state);
pushup(id);
}
void solve1()
{
int start;
scanf("%d",&start);
int ans=query(,start);
printf("%d\n",ans);
if(ans) update(ans,ans+start-,,); //找到才更新
}
void solve2()
{
int start,skip;
scanf("%d%d",&start,&skip);
update(start,start+skip-,,);
}
int main()
{
cin>>N>>M;
build_tree(,N,);
for(int i=;i<=M;i++)
{
int type;
scanf("%d",&type);
if(type==) solve1();
else solve2();
}
return ;
}

PKU 3667 Hotel(线段树)的更多相关文章

  1. poj 3667 Hotel (线段树)

    http://poj.org/problem?id=3667 Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 94 ...

  2. POJ 3667 Hotel(线段树 区间合并)

    Hotel 转载自:http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html [题目链接]Hotel [题目类型]线段树 ...

  3. poj 3667 Hotel(线段树,区间合并)

    Hotel Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 10858Accepted: 4691 Description The ...

  4. POJ 3667 Hotel (线段树区间合并)

    题目链接:http://poj.org/problem?id=3667 最初给你n间空房,m个操作: 操作1 a 表示检查是否有连续的a间空房,输出最左边的空房编号,并入住a间房间. 操作2 a b ...

  5. [USACO08FEB]酒店Hotel 线段树

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

  6. Hotel(线段树合并)

    Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 14958   Accepted: 6450 Descriptio ...

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

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

  8. POJ 1823 Hotel 线段树

    题目链接 线段树的区间合并. 和上一题差不多....第三种操作只需要输出maxx[1]的值就可以. #include <iostream> #include <vector> ...

  9. poj Hotel 线段树

    经典线段树的题. 每个节点存储的信息:左端点连续空房间的长度,右端点连续空房间长度,连续空房间的最大长度. 由于要求每次必须从尽量靠左边的位置进行居住,那么搜索时应尽量让区间起始位置更小: 1.如果当 ...

随机推荐

  1. UIScrollView入门与框架设计

    一.概述 1.UIScrollView的contentSize, contentOffSet, contentInsets的作用和使用. 2.UIScrollView的一整个滚动过程的生命周期(开始滚 ...

  2. softlayer virtual machine vhd磁盘镜像导入shell脚本

    脚本

  3. HHKB Professional 2

    今天买了一副号称程序员专用的静电容键盘 HHBK Pro2无刻版,简单上手了一下,确实名不虚传,打起字来酣畅淋漓,毫不拖泥带水,感觉自己的技术提高了不少呢!!!! 由于是无刻版,需要一些时间来适应,尤 ...

  4. SpringMVC(三)——其他知识

    这篇博客,看一下在Controller类中,进行结果的跳转方式,对于SpringMVC框架中异常,如何统一捕捉,还有就是S(SpringMVC)SH的整合. 一,框架默认情况下是通过转发进行跳转的,如 ...

  5. MyCat部署运行(Windows环境)与使用步骤详解

        目录(?)[+] 1.MyCat概念 1.1 总体架构 MyCAT的架构如下图所示: MyCAT使用MySQL的通讯协议模拟成一个MySQL服务器,并建立了完整的Schema(数据库).Tab ...

  6. iOS更新之DFU模式和恢复模式

    DFU模式和恢复模式的区别:DFU模式是在iPhone固件引导启动之前进行恢复的模式.所以用DFU模式刷机一般比较干净,不会有任何垃圾文件.想当于电脑重新格式化之后再安装系统. DFU模式进入方法:1 ...

  7. DreamWeaver文件保存时,提示"发生共享违例"问题的解决方法

    在学习牛腩老师的JS视频中,视频中的例子要求实现一个是23个3相乘的结果,在用Dreamweaver制作时,, <script language="javascript" t ...

  8. Mysql 分别按月, 日为组group,进行统计排序order

    在数据库中我们经经常使用sql语句对表进行简单的统计排序,对于日期字段.我们能够简单的对其进行order. 对于复杂一点的能够按日期中的年.月,日分别进行group,order. 按年份进行group ...

  9. According to TLD or attribute directive in tag file, attribute value does not accept any expressions

    1.错误描写叙述 2014-7-13 17:27:21 org.apache.jasper.compiler.TldLocationsCache tldScanJar 信息: At least one ...

  10. Xcode5和6上新建工程如何本地化启动页面

    建议阅读本篇文章前先具备iOS本地化的基本知识,Google中搜索“iOS本地化”,有成片的教程~~ 最近有个app需要支持英语.简体中文.繁体中文,由于启动页面上有文字,所以也不得不做下本地化处理. ...