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

  题目大致就是说对一段区间进行两种操作,分别是找到能够容下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,线段树+区间合并。的更多相关文章

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

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

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

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

  3. POJ 3667 & 1823 Hotel (线段树区间合并)

    两个题目都是用同一个模板,询问最长的连续未覆盖的区间 . lazy代表是否有人,msum代表区间内最大的连续长度,lsum是从左结点往右的连续长度,rsum是从右结点往左的连续长度. 区间合并很恶心啊 ...

  4. poj 3667 Hotel (线段树)

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

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

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

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

    题目传送门 /* 题意:输入 1 a:询问是不是有连续长度为a的空房间,有的话住进最左边 输入 2 a b:将[a,a+b-1]的房间清空 线段树(区间合并):lsum[]统计从左端点起最长连续空房间 ...

  7. Poj 3667——hotel——————【线段树区间合并】

    Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13124   Accepted: 5664 Descriptio ...

  8. POJ 3667 线段树区间合并

    http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html 用线段树,首先要定义好线段树的节点信息,一般看到一个问题,很难很 ...

  9. poj3667 Hotel (线段树 区间合并)

    poj3667 HotelTime Limit: 3000MS Memory Limit: 65536KTotal Submissions: 18925 Accepted: 8242Descripti ...

  10. 【bzoj1593】[Usaco2008 Feb]Hotel 旅馆 线段树区间合并

    题目描述 奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光.作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿.这个巨大的旅馆一共有N (1 <= N & ...

随机推荐

  1. ssh登录nat模式的VMware虚拟机

    有时候本地PC是固定IP上网方式且无多余IP,而我们又希望使用putty登陆VMware中的虚拟机且虚拟机可以上外网,那么这时候就可以使用端口映射. 1.本地环境简述 本地PC IP:192.168. ...

  2. php如何获取本地手机号

    <?php function inquiry_number_infor($phonenumber) /* *传入手机号码,通过API的到xml格式数据,对xml进一步解析,最后返回相应的号码信息 ...

  3. Converting between IEEE 754 and Float (Format related

    The float can be converted to well known single-precision IEEE 754 number, why 754? It's the standar ...

  4. Learning Java characteristics (Java in a Nutshell 6th)

    Java characteristics: Java .class files are machine-independent, including the endianness. Java .cla ...

  5. 移动Web框架:jQuery Mobile VS Sencha Touch

    最近常被问到是用 jQuery Mobile还是Sencha Touch,本人也比较关注这两个框架,试图从以下两方面发表点儿见解: 身家背景,都系出名门 1.jQuery Mobile 建立在jQue ...

  6. [转]于Fragment和Activity之间onCreateOptionsMenu的问题

    Fragment和Activity一样,可以重写onCreateOptionsMenu方法来设定自己的菜单,其实这两个地方使用onCreateOptionsMenu的目的和效果都是完全一样的,但是由于 ...

  7. java 实例变量和类变量的区别

    Example4_10.java public class Example4_10 { public static void main(String args[]) { Lader.下底=100; / ...

  8. php简单命令代码集锦

    if(file_exists("file.htm"))// 检查是否存在此文件 if(file_exists("chat"))//检查是否存在此文件夹 rena ...

  9. 静态NAT、动态NAT

    静态NAT.动态NAT 实验拓扑: 实验目的:熟悉网络地址转换协议 掌握静态NAT 和动态NAT的配置 分析静态NAT 和动态NAT的区别 使用show命令来检查NAT的运行情况 实验要求:按拓扑图来 ...

  10. 简单查询plan

    -> alter session set statistics_level=all; select /*+ gathe_plan_statistics */ * from ts.ts_recor ...