Hotel

Time Limit: 3000MS
Memory Limit: 65536K

Total Submissions: 10858
Accepted: 4691

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.

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
 
题意:告诉你有n个房间,一开始都是空房间,接下来m次操作,每一次操作,如果第一个数是1,Di就表示有人需要租Di间连续的房间,如果没有输出0,如果有则房间租出,
如果有满足条件的有种情况,则租最左边的。如果第一个数是2,则Xi、Di分别表示从Xi号房间到Xi+Di-1号房间的人退房。
::这道题我是在参考hh神的代码慢慢敲出来的,现在有些理解
  房间       1  2  3  4  5  6  7  8
(1租出)    1  1  0  1  1  1  1  1 
lsum表示的是区间左端开始的连续房间个数,即lsum=2;
rsum表示的是区间右端开始的连续房间个数,即rsum=5;
msum表示中间部分连续房间的最大个数,即msum=5;
 
代码:
   1: #include <iostream>

   2: #include <cstdio>

   3: #include <cstring>

   4: #include <algorithm>

   5: #include <queue>

   6: using namespace std;

   7: #define lson l,m,rt<<1

   8: #define rson m+1,r,rt<<1|1

   9: typedef long long ll;

  10: const int N=55555;

  11: int lsum[N<<2],rsum[N<<2],msum[N<<2];

  12: int cover[N<<2];

  13:  

  14: void Down(int rt,int m)

  15: {

  16:     if(cover[rt]!=-1)

  17:     {

  18:         int ls=rt<<1,rs=ls|1;

  19:         cover[ls]=cover[rs]=cover[rt];

  20:         lsum[ls]=rsum[ls]=msum[ls]=cover[rt]?0:m-(m>>1);

  21:         lsum[rs]=rsum[rs]=msum[rs]=cover[rt]?0:(m>>1);

  22:         cover[rt]=-1;

  23:     }

  24: }

  25:  

  26: void Up(int rt,int m)

  27: {

  28:     lsum[rt]=lsum[rt<<1];

  29:     rsum[rt]=rsum[rt<<1|1];

  30:     if(lsum[rt]==m-(m>>1)) lsum[rt]+=lsum[rt<<1|1];

  31:     if(rsum[rt]==(m>>1)) rsum[rt]+=rsum[rt<<1];

  32:     msum[rt]=max(rsum[rt<<1]+lsum[rt<<1|1],max(msum[rt<<1],msum[rt<<1|1]));

  33: }

  34:  

  35: void build(int l,int r,int rt)

  36: {

  37:     lsum[rt]=rsum[rt]=msum[rt]=r-l+1;

  38:     cover[rt]=-1;

  39:     if(l==r) return ;

  40:     int m=(l+r)>>1;

  41:     build(lson);

  42:     build(rson);

  43: }

  44:  

  45: void update(int L,int R,int c,int l,int r,int rt)

  46: {

  47:     if(L<=l&&R>=r)

  48:     {

  49:         lsum[rt]=rsum[rt]=msum[rt]=c?0:r-l+1;

  50:         cover[rt]=c;

  51:         return ;

  52:     }

  53:     Down(rt,r-l+1);

  54:     int m=(l+r)>>1;

  55:     if(L<=m) update(L,R,c,lson);

  56:     if(R>m) update(L,R,c,rson);

  57:     Up(rt,r-l+1);

  58: }

  59:  

  60: int query(int len,int l,int r,int rt)

  61: {

  62:     if(l==r) return l;

  63:     Down(rt,r-l+1);

  64:     int m=(l+r)>>1;

  65:     if(msum[rt<<1]>=len) return query(len,lson);

  66:     else if(rsum[rt<<1]+lsum[rt<<1|1]>=len)

  67:         return m-rsum[rt<<1]+1;

  68:     return query(len,rson);

  69: }

  70:  

  71: int main()

  72: {

  73: //    freopen("in.txt","r",stdin);

  74:     int n,m;

  75:     while(scanf("%d%d",&n,&m)>0)

  76:     {

  77:         int c,a,b;

  78:         build(1,n,1);

  79:         while(m--)

  80:         {

  81:             scanf("%d",&c);

  82:             if(c==1)

  83:             {

  84:                 scanf("%d",&a);

  85:                 if(msum[1]<a) puts("0");

  86:                 else

  87:                 {

  88:                     int p=query(a,1,n,1);

  89:                     printf("%d\n",p);

  90:                     update(p,p+a-1,1,1,n,1);

  91:                 }

  92:             }

  93:             else

  94:             {

  95:                 scanf("%d%d",&a,&b);

  96:                 update(a,a+b-1,0,1,n,1);

  97:             }

  98:         }

  99:     }

 100:     return 0;

 101: }

 

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

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

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

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

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

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

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

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

  9. POJ 2482 Stars in Your Window (线段树区间合并+扫描线)

    这题开始一直被矩形框束缚了,想法一直都是枚举线,但是这样枚举都需要O(n^2)...但是看了别人的思路,感觉这题思想真心很好(PS:开头好浪漫的描述啊,可惜并没有什么用)  题意就是在平面上给你一些星 ...

随机推荐

  1. AEAI BPM流程集成平台V3.0.2版本开源发布

    本次开源发布的是AEAI BPMV3.0.2版流程平台,该版本是数通畅联首次正式对外发布的版本,产品现已开源并上传至开源社区http://www.oschina.net/p/aeai-bpm. 产品说 ...

  2. (二)Protobuf的C#使用

    [转]http://blog.csdn.net/shantsc/article/details/50729402 protobuf  c#版本分成两个版本,一个是protobuf-net,另一个是pr ...

  3. IIS 503日志文件在哪

    概述  503:“服务不可用”错误是一个非自定义的错误,该错误表示服务器当前无法处理该请求. 可能原因:1.管理员可能关闭应用程序池以执行维护.2.当请求到达时应用程序池队列已满.3.应用程序池标识没 ...

  4. 论httpclient上传带参数【commons-httpclient和apache httpclient区别】

    需要做一个httpclient上传,然后啪啪啪网上找资料 1.首先以前系统中用到的了commons-httpclient上传,找了资料后一顿乱改,然后测试 PostMethod filePost = ...

  5. 不是语言之争--Go vs Erlang

    因为 云巴 系统对高并发.低延迟的需求,我们对各个语言.平台做了很多的调研比较工作.这自然就包括致力于开发高并发应用的 Go 和 Erlang. 并发 Go 对高并发的支持通过 goroutine 实 ...

  6. quartz使用(一)

    在项目中经常会碰到定时任务,quartz是一款非常优秀的开源框架, 提供了定时任务的支持,还支持任务的持久化,并且提供了对数据库的支持.下面首先对quartz做一个简单介绍,并附上一个小例子. 1.下 ...

  7. 终端&作业控制&会话启动过程

    进程组 每个进程除了有个进程id外,还属于一个进程组.进程组是一个或者多个进程的集合.通常他们与同一个作业相关联,可以接受来自同一终端的信号.进程组id等于其进程组长id.进程组的终止与进程组长是否存 ...

  8. [mysql] 一次sql耗时高引发报警的分析和处理

    1.现象: 最近两天在每天的凌晨0:15-20分左右收到报警短息,报警内容: JDBC-SQL请求最近三分钟内平均耗时时间过高的报警,监控类型:SQL... 2.分析: 从现象来看 每天凌晨15分,可 ...

  9. 高性能JS笔记1——加载执行

    一.脚本位置 1.Script标签尽可能放到Body底部,以减少脚本文件下载对整个页面UI渲染的影响. 2.Script标签永远不要紧跟Link标签后面. 二.组织脚本 1.合并多个文件在一个Scri ...

  10. android 保存 用户名和密码 设置等应用信息优化

    1.传统的保存用户名,密码方式 SharedPreferences Editor editor = shareReference.edit(); editor.putString(KEY_NAME,& ...