P2894 [USACO08FEB]酒店Hotel

Posted HWIM

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了P2894 [USACO08FEB]酒店Hotel相关的知识,希望对你有一定的参考价值。

P2894 [USACO08FEB]酒店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 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.

参考样例,第一行输入n,m ,n代表有n个房间,编号为1---n,开始都为空房,m表示以下有m行操作,以下 每行先输入一个数 i ,表示一种操作:

若i为1,表示查询房间,再输入一个数x,表示在1--n 房间中找到长度为x的连续空房,输出连续x个房间中左端的房间号,尽量让这个房间号最小,若找不到长度为x的连续空房,输出0。

若i为2,表示退房,再输入两个数 x,y 代表 房间号 x---x+y-1 退房,即让房间为空。

输入输出格式

输入格式:

 

  • 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

 

输出格式:

 

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

 

输入输出样例

输入样例#1:
10 6
1 3
1 3
1 3
1 3
2 5 5
1 6
输出样例#1:
1
4
7
0
5

分析

线段树维护三个值,区间内最大的连续空位,从左边最大延伸的长度,从右边最大延伸的长度。

更新时,有人住让这些数组都为0,表示0个空位,没人住,就是区间长度,表示区间内的空位就是长度,都是空位。

其他的详见代码注释。

code

  1 #include<cstdio>
  2 #include<algorithm>
  3 #define lson l,m,rt<<1
  4 #define rson m+1,r,rt<<1|1
  5 
  6 using namespace std;
  7 
  8 const int MAXN = 400010;
  9 int len[MAXN],sum[MAXN],ml[MAXN],mr[MAXN],tag[MAXN];
 10 //分别区间的长度,区间内最大的连续空位,从左边延伸最长的空位,同理从右边,懒标记 
 11 int n,q;
 12 
 13 void pushup(int rt)
 14 {
 15     if (sum[rt<<1]==len[rt<<1])//如果左区间全是空位,大区间的ml就是左区间的全部 加 右区间的ml 
 16         ml[rt] = sum[rt<<1]+ml[rt<<1|1];
 17     else//否则就是最区间ml 
 18         ml[rt] = ml[rt<<1];
 19     if (sum[rt<<1|1]==len[rt<<1|1])//同理 
 20         mr[rt] = sum[rt<<1|1]+mr[rt<<1];
 21     else
 22         mr[rt] = mr[rt<<1|1];
 23     sum[rt] = max(max(sum[rt<<1],sum[rt<<1|1]),mr[rt<<1]+ml[rt<<1|1]);//大区间的最大连续长度,从两个子区间和中间的部分区最大的 
 24     return;
 25 }
 26 void pushdown(int rt)
 27 {
 28     if (tag[rt]==0) return;
 29     if (tag[rt]==1)//有人住,所以设为0 
 30     {
 31         tag[rt<<1] = tag[rt<<1|1] = 1;
 32         sum[rt<<1] = ml[rt<<1] = mr[rt<<1] = 0;
 33         sum[rt<<1|1] = ml[rt<<1|1] = mr[rt<<1|1] = 0;
 34     }
 35     if (tag[rt]==2)//没有人住,全是空,就是区间的长度 
 36     {
 37         tag[rt<<1] = tag[rt<<1|1] = 2;
 38         sum[rt<<1] = ml[rt<<1] = mr[rt<<1] = len[rt<<1];
 39         sum[rt<<1|1] = ml[rt<<1|1] = mr[rt<<1|1] = len[rt<<1|1];
 40     }
 41     tag[rt] = 0;
 42 }
 43 void build(int l,int r,int rt)
 44 {
 45     ml[rt] = mr[rt] = sum[rt] = len[rt] = r-l+1;
 46     tag[rt] = 0;
 47     if (l==r) return;
 48     int m = (l+r)>>1;
 49     build(lson);
 50     build(rson);
 51 }
 52 void update(int l,int r,int rt,int L,int R,int c)
 53 {
 54     pushdown(rt);
 55     if (L<=l&&r<=R)
 56     {
 57         if (c==1) sum[rt] = ml[rt] = mr[rt] = 0;
 58         else sum[rt] = ml[rt] = mr[rt] = len[rt];
 59         tag[rt] = c;
 60         return ;
 61     }
 62     int m = (l+r)>>1;
 63     if (L<=m) update(lson,L,R,c);
 64     if (R>m)  update(rson,L,R,c);
 65     pushup(rt);
 66 }
 67 int query(int l,int r,int rt,int x)
 68 {
 69     pushdown(rt);
 70     if (l==r) return l;
 71     int m = (l+r)>>1;
 72     if (sum[rt<<1]>=x) return query(lson,x);//如果左区间有x个连续空位,在左区间找 
 73     if (mr[rt<<1]+ml[rt<<1|1]>=x) return m-mr[rt<<1]+1;//如果左区间的右边 和 右区间的左边 加起来有x个连续空位,返回左区间右边最大时的起点 
 74     else return query(rson,x);//在右区间找,肯定会有的 
 75 }
 76 int main()
 77 {
 78     scanf("%d%d",&n,&q);
 79     build(1,n,1);
 80     while (q--)
 81     {
 82         int opt,x,y,p;
 83         scanf("%d",&opt);
 84         if (opt==1)
 85         {
 86             scanf("%d",&x);
 87             if (sum[1]<x)//如果总区间没有x个连续的空位,输出0 
 88             {
 89                 printf("0\n");
 90                 continue;
 91             }
 92             p = query(1,n,1,x);
 93             printf("%d\n",p);
 94             update(1,n,1,p,p+x-1,1);//区间不是0,所以数组都是0,见数组定义 
 95         }
 96         else
 97         {
 98             scanf("%d%d",&x,&y);
 99             update(1,n,1,x,x+y-1,2);//将区间设为0,所以数组都是区间的长度 
100         }
101     }
102     return 0;
103 }

 

以上是关于P2894 [USACO08FEB]酒店Hotel的主要内容,如果未能解决你的问题,请参考以下文章

[P2894][USACO08FEB] 酒店Hotel (线段树+懒标记下传)

luogu2894 [USACO08FEB]酒店Hotel

[USACO08FEB]酒店Hotel

[USACO08FEB] 酒店Hotel - 线段树

浅谈线段树 (例题:[USACO08FEB]酒店Hotel)By cellur925

poj3667(bzoj1593)--Usaco08Feb Hotel--线段树区间合并