(简单) POJ 3667 Hotel,线段树+区间合并。
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.
题目大致就是说对一段区间进行两种操作,分别是找到能够容下D的最左边的位置,然后就是更新一段区间为0或1。
很典型的区间覆盖问题,维护最大空房间和前缀最大,后缀最大三个值。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring> #define lson L,M,po*2
#define rson M+1,R,po*2+1
#define lc po*2
#define rc po*2+1 using namespace std; const int maxn=; int msum[maxn*],lsum[maxn*],rsum[maxn*];
int COL[maxn*]; void pushDown(int po,int len)
{
if(COL[po]==)
{
COL[lc]=COL[rc]=;
msum[lc]=rsum[lc]=lsum[lc]=;
msum[rc]=rsum[rc]=lsum[rc]=; COL[po]=-;
}
else if(COL[po]==)
{
COL[lc]=COL[rc]=;
msum[lc]=rsum[lc]=lsum[lc]=len-(len/);
msum[rc]=rsum[rc]=lsum[rc]=len/; COL[po]=-;
}
} void pushUP(int po,int len)
{
msum[po]=max(msum[lc],msum[rc]);
msum[po]=max(msum[po],rsum[lc]+lsum[rc]); lsum[po]=lsum[lc];
if(lsum[lc]==(len-(len/)))
lsum[po]+=lsum[rc]; rsum[po]=rsum[rc];
if(rsum[rc]==len/)
rsum[po]+=rsum[lc];
} void build_tree(int L,int R,int po)
{
COL[po]=-;
msum[po]=rsum[po]=lsum[po]=R-L+; if(L==R)
return; int M=(L+R)/; build_tree(lson);
build_tree(rson);
} void update(int ul,int ur,int ut,int L,int R,int po)
{
if(ul<=L&&ur>=R)
{
COL[po]=ut;
msum[po]=lsum[po]=rsum[po]=(ut ? : R-L+); return;
} pushDown(po,R-L+); int M=(L+R)/; if(ul<=M)
update(ul,ur,ut,lson);
if(ur>M)
update(ul,ur,ut,rson); pushUP(po,R-L+);
} int query(int len,int L,int R,int po)
{
if(L==R)
return L; pushDown(po,R-L+); int M=(L+R)/; if(msum[lc]>=len)
return query(len,lson);
else if(rsum[lc]+lsum[rc]>=len)
return M-rsum[lc]+;
else
return query(len,rson);
} int main()
{
int M,N;
int a,b,c; while(~scanf("%d %d",&N,&M))
{
build_tree(,N,); for(int i=;i<M;++i)
{
scanf("%d %d",&a,&b); if(a==)
{
if(msum[]<b)
printf("%d\n",);
else
{
c=query(b,,N,);
update(c,c+b-,,,N,);
printf("%d\n",c);
}
}
else
{
scanf("%d",&c);
update(b,b+c-,,,N,);
}
}
} return ;
}
(简单) POJ 3667 Hotel,线段树+区间合并。的更多相关文章
- POJ 3667 Hotel(线段树 区间合并)
Hotel 转载自:http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html [题目链接]Hotel [题目类型]线段树 ...
- POJ 3667 Hotel (线段树区间合并)
题目链接:http://poj.org/problem?id=3667 最初给你n间空房,m个操作: 操作1 a 表示检查是否有连续的a间空房,输出最左边的空房编号,并入住a间房间. 操作2 a b ...
- POJ 3667 & 1823 Hotel (线段树区间合并)
两个题目都是用同一个模板,询问最长的连续未覆盖的区间 . lazy代表是否有人,msum代表区间内最大的连续长度,lsum是从左结点往右的连续长度,rsum是从右结点往左的连续长度. 区间合并很恶心啊 ...
- poj 3667 Hotel (线段树)
http://poj.org/problem?id=3667 Hotel Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 94 ...
- poj 3667 Hotel(线段树,区间合并)
Hotel Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 10858Accepted: 4691 Description The ...
- 线段树(区间合并) POJ 3667 Hotel
题目传送门 /* 题意:输入 1 a:询问是不是有连续长度为a的空房间,有的话住进最左边 输入 2 a b:将[a,a+b-1]的房间清空 线段树(区间合并):lsum[]统计从左端点起最长连续空房间 ...
- Poj 3667——hotel——————【线段树区间合并】
Hotel Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 13124 Accepted: 5664 Descriptio ...
- POJ 3667 线段树区间合并
http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html 用线段树,首先要定义好线段树的节点信息,一般看到一个问题,很难很 ...
- poj3667 Hotel (线段树 区间合并)
poj3667 HotelTime Limit: 3000MS Memory Limit: 65536KTotal Submissions: 18925 Accepted: 8242Descripti ...
- 【bzoj1593】[Usaco2008 Feb]Hotel 旅馆 线段树区间合并
题目描述 奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光.作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿.这个巨大的旅馆一共有N (1 <= N & ...
随机推荐
- 创建zend framework 项目要注意的
1.必须要设置变量环境 我的电脑右击-属性-高级-环境变量 则在环境变量中添加 变量名:PATH 环境值:D:\phpserver\php5.4;D:\ZendFramework\bin 把php.e ...
- 在Javascript中使用protobuf与c++进行通信
环境:Win7_64旗舰版,VS2013 最近在研究Webkit,已经编译成功,接下来就是Javascript与c++如何传输数据,立刻就想到了protobuf,但是谷歌不支持Javascript,百 ...
- 最小点集覆盖=最大匹配<二分图>/证明
来源 最小点集覆盖==最大匹配. 首先,最小点集覆盖一定>=最大匹配,因为假设最大匹配为n,那么我们就得到了n条互不相邻的边,光覆盖这些边就要用到n个点. 现在我们来思考为什么最小点击覆盖一定& ...
- java数据结构之二叉树的实现
java二叉树的简单实现,可以简单实现深度为n的二叉树的建立,二叉树的前序遍历,中序遍历,后序遍历输出. /** *数据结构之树的实现 *2016/4/29 * **/ package cn.Link ...
- LightOJ 1336 Sigma Function(数论 整数拆分推论)
--->题意:给一个函数的定义,F(n)代表n的所有约数之和,并且给出了整数拆分公式以及F(n)的计算方法,对于一个给出的N让我们求1 - N之间有多少个数满足F(x)为偶数的情况,输出这个数. ...
- opencart配置税率
1.System->Localisation->Geo Zones新增税收区域 2.System->Localisation->Taxes->Tax Rates新增税率 ...
- abstract class 与interface
一.抽象类(absteact class) 特点: 1.抽象方法只作说明,而不包含实现,可以看成是没有实现体的虚方法 2.抽象类不能被实例化.除此之外,具有类的其他特性 3.抽象类可以但不是必须有抽象 ...
- MOSFET与MOSFET驱动电路原理及应用(转)
源:http://www.micro-bridge.com/news/news.asp?id=258 在使用MOS管设计开关电源或者马达驱动电路的时候,大部分人都会考虑MOS的导通电阻,最大电压等,最 ...
- 一个不错的angular 字体库( 引用js文件就行)
https://klarsys.github.io/angular-material-icons/ 使用方法: 1.引入这个js文件, <script src="//cdnjs.clo ...
- Recover Polygon (easy)
Recover Polygon (easy) The zombies are gathering in their secret lair! Heidi will strike hard to des ...