POJ 3667 Hotel

Hotel

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 16782   Accepted: 7303

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.

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 D(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个房间,支持一下两种操作:1.将区间所有的房间清空。2.从左往右查询一段长度大于或等于w的连续空房间并将这些房间填满,查询的返回值为这段房间的左端点。
  相比之前提到的线段树例题,这道题目又有了很大的不同。由于这道题的操作与查询都是建立在一段区间上的,所以不难想到这道题是要用线段树来维护的。(其实是标签贴的是线段树)但是具体怎么实现呢?这就要牵涉到线段树的区间合并操作了。首先我们要多开几个数组维护区间的信息,lsum[]用来维护这段区间内从左端点开始的一段连续空闲的区间长;rsum[]用来维护这段区间从右端点开始的一段连续空闲的区间长;sum[]用来维护这段区间中最长的一段空闲的区间长。有了这三个数组,就只要考虑着重考虑pushup操作和pushdown操作了。

  pushup操作:
1 void pushup(int id){
2     int l=left[id];int r=right[id];
3     lsum[id]=lsum[id<<1];rsum[id]=rsum[id<<1|1];
4     int mid=(l+r)>>1;
5     if(lsum[id]==mid-l+1)lsum[id]+=lsum[id<<1|1];
6     if(rsum[id]==r-mid)rsum[id]+=rsum[id<<1];
7     sum[id]=max(max(sum[id<<1],sum[id<<1|1]),lsum[id<<1|1]+rsum[id<<1]);
8 }  
    具体来分析一下这个操作的具体含义显然有根的lsum与它左孩子的lsum相同,因为它们的左端点相同;同理也有根的rsum与根的右孩子的rsum相同。由于lsum或是rsum的长度可能越过了mid,所以我们判定一下lsum与rsum的长度,如果已经到了mid了,则还要加上另一截,才能保证正确。最后sum的大小也很容易确定,就是左边最长的一段、右边最长的一段、还有中间一段长的最大值。
  pushdown操作:
1 void pushdown(int id){
2     if(cover[id]!=-1){
3         cover[id<<1]=cover[id<<1|1]=cover[id];
4         sum[id<<1]=lsum[id<<1]=rsum[id<<1]=cover[id]?0:right[id<<1]-left[id<<1]+1;
5         sum[id<<1|1]=lsum[id<<1|1]=rsum[id<<1|1]=cover[id]?0:right[id<<1|1]-left[id<<1|1]+1;
6         cover[id]=-1;
7     }
8 }
    这个操作还是比较简单的了,首先lazy tag的思想是要有的,相信有了前面的线段树的基础,这点是不在话下的。那么就好办了,直接判断cover标记,如果为0,则表示清空,那么这段区间sum、lsum、rsum都会是这段区间的完整长度;如果为1,那么则表示这段区间没有空闲空间了,则全部赋值为0.最后别忘了还原标记,这个忘了打我调了半天。

最后附上AC代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5
 6 static const int maxm=1e6+10;
 7
 8 int left[maxm],right[maxm],lsum[maxm],rsum[maxm],sum[maxm],cover[maxm];
 9 int n,m,f,opt,pos;
10
11 void build(int id,int l,int r){
12     left[id]=l;right[id]=r;cover[id]=-1;
13     sum[id]=lsum[id]=rsum[id]=r-l+1;
14     int mid=(l+r)>>1;
15     if(l==r)return;
16     build(id<<1,l,mid);
17     build(id<<1|1,mid+1,r);
18 }
19
20 void pushdown(int id){
21     if(cover[id]!=-1){
22         cover[id<<1]=cover[id<<1|1]=cover[id];
23         sum[id<<1]=lsum[id<<1]=rsum[id<<1]=cover[id]?0:right[id<<1]-left[id<<1]+1;
24         sum[id<<1|1]=lsum[id<<1|1]=rsum[id<<1|1]=cover[id]?0:right[id<<1|1]-left[id<<1|1]+1;
25         cover[id]=-1;
26     }
27 }
28
29 void pushup(int id){
30     int l=left[id];int r=right[id];
31     lsum[id]=lsum[id<<1];rsum[id]=rsum[id<<1|1];
32     int mid=(l+r)>>1;
33     if(lsum[id]==mid-l+1)lsum[id]+=lsum[id<<1|1];
34     if(rsum[id]==r-mid)rsum[id]+=rsum[id<<1];
35     sum[id]=max(max(sum[id<<1],sum[id<<1|1]),lsum[id<<1|1]+rsum[id<<1]);
36 }
37
38 void update(int id,int l,int r,int c){
39     if(left[id]>=l&&right[id]<=r){
40         sum[id]=lsum[id]=rsum[id]=c?0:right[id]-left[id]+1;
41         cover[id]=c;
42         return;
43     }
44     if(left[id]>r||right[id]<l)return;
45     pushdown(id);
46     update(id<<1,l,r,c);
47     update(id<<1|1,l,r,c);
48     pushup(id);
49 }
50
51 int Query(int id,int c){
52     if(left[id]==right[id])return left[id];
53     pushdown(id);
54     int mid=(left[id]+right[id])>>1;
55     if(sum[id<<1]>=c)return Query(id<<1,c);
56     else if(rsum[id<<1]+lsum[id<<1|1]>=c)return mid-rsum[id<<1]+1;
57     else return Query(id<<1|1,c);
58 }
59
60 int main(){
61     scanf("%d%d",&n,&m);
62     build(1,1,n);
63
64     while(m--){
65         int x,y;
66         scanf("%d",&opt);
67         if(opt==1){
68             scanf("%d",&x);
69             if(sum[1]<x)printf("0\n");
70             else{
71                 printf("%d\n",pos=Query(1,x));
72                 update(1,pos,pos+x-1,1);
73             }
74         }else{
75             scanf("%d%d",&x,&y);
76             update(1,x,x+y-1,0);
77         }
78     }
79
80     return 0;
81 }

我是代码

AC通道:http://poj.org/problem?id=3667

时间: 2024-12-25 11:30:08

POJ 3667 Hotel的相关文章

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

题目传送门 1 /* 2 题意:输入 1 a:询问是不是有连续长度为a的空房间,有的话住进最左边 3 输入 2 a b:将[a,a+b-1]的房间清空 4 线段树(区间合并):lsum[]统计从左端点起最长连续空房间数,rsum[]类似,sum[]统计区间最长连续的空房间数, 5 它有三种情况:1.纯粹是左端点起的房间数:2.纯粹是右端点的房间数:3.当从左(右)房间起都连续时,加上另一个子节点 6 从左(右)房间起的数,sum[]再求最大值更新维护.理解没错,表达能力不足 7 详细解释:htt

POJ 3667 Hotel(线段树)

POJ 3667 Hotel 题目链接 题意:有n个房间,现在有两个操作 1.找到连续长度a的空房间,入住,要尽量靠左边,如果有输出最左边的房间标号,如果没有输出0 2.清空[a, a + b - 1]的房间 思路:线段树的区间合并,记录下左边连续最长和右边连续最长空房间,和每一段的最大值,这样pushup的时候就是进行区间合并,注意查询的时候由于是要尽量左,所以先查左孩子,在查横跨左右的,在查右孩子 代码: #include <cstdio> #include <cstring>

POJ 3667 Hotel 【线段树 区间合并 + Lazy-tag】

Hotel Time Limit: 3000MS Memory Limit: 65536K 链接:POJ 3667   Description The cows are journeying north to ThunderBay in Canada to gain cultural enrichment and enjoy a vacation on the sunnyshores of Lake Superior. Bessie, ever the competent travel agen

poj 3667 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 v

POJ 3667 Hotel(区间合并)

题目链接:http://poj.org/problem?id=3667 题意:宾馆有n个房间.有人来入住.共有2种操作 输入1和d,表示查询最左的连续d个空房间数的起始位置. 输入2,x和d,表示将从x开始长度为d的连续的房间清空. 思路:裸的区间合并.每个结点区间[l,r]存从左端点l开始向右最大连续空房间数lm,从右端点r开始向左最大连续空房间数rm和当前区间内最大连续空房间数. 代码: #include <iostream> #include <stdio.h> #inclu

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

题目传送:Hotel 思路:线段树,区间合并,区间替换,查询最左断点,看胡浩版本的线段树好几天了,今天看这个看了好久,慢慢来吧,具体都写在注释里了 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <stack> #inc

(简单) 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 Stree

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 Stree

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

Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13124   Accepted: 5664 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