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. oracle-查询执行速度慢的sql

    Oracle 查询每天执行慢的SQL 2014-12-11 18:00:04 分类: Oracle 链接:http://blog.itpub.net/28602568/viewspace-136484 ...

  2. php 个推的例子

    个推   http://docs.getui.com/server/php/start/ <?php /** * Created by PhpStorm. * User: xiaochao * ...

  3. Android ViewDragHelper完全解析 自定义ViewGroup神器

    Android ViewDragHelper完全解析 自定义ViewGroup神器   转载请标明出处: http://blog.csdn.net/lmj623565791/article/detai ...

  4. Android中ViewPager如何设置不能通过屏幕左右滑动来切换页面

    //很多时候,我想禁止用户通过屏幕的左右滑动来切换界面!如何实现! //创建一个类继承viewpager,实现 onTouchEvent   和   onInterceptTouchEvent方法,都 ...

  5. img图片inline-block总结

    <div style="font-size:0;"> <img data-src="http://image.zhangxinxu.com/image/ ...

  6. git rebase 使用

    git rebase 不会取回代码 要用git fetch先取回, git rebase 是合并代码. (1)首先用git fetch返回服务器上的代码 (2)首先用git rebase origin ...

  7. PHP程序员学习路线

    注:本文是@黑夜路人的旧文,假设PHP程序员基础不是非常扎实,简单梳理了每个阶段PHP程序员的技术要求,来帮助很多PHP程序做对照设定学习成长目标.再次分享,共勉,欢迎补充. 第一阶段:基础阶段(基础 ...

  8. 后台前台json传递数据的方式两种方式 $.get, $.getJSON

    第一种getJSON方式: 前台调用: <td><input type="text" class="t" id="edutitle& ...

  9. javascript 中{}和[] 的理解

    下面的一段解释是摘抄的,基本理解正确,做个记录.其实js中数组其实就是对象,typeof(['a', 'b', 'c'])//测试之后结果为 :  "object" 一.{ } 大 ...

  10. 洛谷 U4704 函数

    设gcd(a,b)为a和b的最大公约数,xor(a,b)为a异或b的结果. 题目描述 kkk总是把gcd写成xor.今天数学考试恰好出到了gcd(a,b)=?这样的题目,但是kkk全部理解成了xor( ...