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

Posted Wally的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 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是团队离开。如果有连续的房间够团队入住打印最小的房间,否则打印0。
思路:首先要用到5个函数作用分别是:建树、懒惰标记、对子节点进行合并、插入新的数据、搜索起始房间号lazy为-1时说明这个节点的子节点已经进行了更新,0时说明该节点内全为空房间,1时说明该节点房间全部住满
AC代码:
  1 #include <iostream>
  2 #include<cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 using namespace std;
  6 const int maxn=50000+10;
  7 struct note
  8 {
  9     int l,r,lm,rm,m,lazy;
 10 } a[maxn<<2];
 11 int build(int l,int r,int k)
 12 {
 13     a[k].l=l,a[k].r=r,a[k].lm=a[k].rm=a[k].m=r-l+1,a[k].lazy=-1;
 14     if(l==r)
 15         return 0;
 16     int mid=(l+r)/2;
 17     build(l,mid,k*2);
 18     build(mid+1,r,k*2+1);
 19     return 0;
 20 }
 21 int pushdown(int k,int d)
 22 {
 23     if(a[k].lazy!=-1)
 24     {
 25         a[k*2].lazy=a[k*2+1].lazy=a[k].lazy;
 26         a[k*2].rm=a[k*2].lm=a[k*2].m=a[k].lazy?0:d-d/2;
 27         a[k*2+1].rm=a[k*2+1].lm=a[k*2+1].m=a[k].lazy?0:d/2;
 28         a[k].lazy=-1;
 29     }
 30     return 0;
 31 }
 32 int pushup(int k,int d)
 33 {
 34     a[k].lm=a[k*2].lm;
 35     a[k].rm=a[k*2+1].rm;
 36     if(a[k].lm==d-d/2)
 37         a[k].lm+=a[k*2+1].lm;
 38     if(a[k].rm==d/2)
 39         a[k].rm+=a[k*2].rm;
 40     a[k].m=max(a[k*2+1].m,max(a[k*2].m,a[k*2].rm+a[k*2+1].lm));
 41     //注意max(左节点的最大值,右节点的最大值,左节点从右边算的最大值+右节点从左边算的最大值)
 42     return 0;
 43 }
 44 int ins(int l,int r,int n,int k)
 45 {
 46     if(l<=a[k].l&&a[k].r<=r)
 47     {
 48         a[k].lm=a[k].rm=a[k].m=n?0:(a[k].r-a[k].l+1);
 49         a[k].lazy=n;
 50         return 0;
 51     }
 52     pushdown(k,a[k].r-a[k].l+1);
 53     if(l<=a[k*2].r) ins(l,r,n,k*2);
 54     if(r>a[k*2].r) ins(l,r,n,k*2+1);
 55     pushup(k,a[k].r-a[k].l+1);
 56     return 0;
 57 }
 58 int query(int d,int k)
 59 {
 60     if(a[k].l==a[k].r)
 61         return 1;
 62     pushdown(k,a[k].r-a[k].l+1);
 63     if(a[k*2].m>=d)
 64         return query(d,k*2);
 65     else if(a[k*2].rm+a[k*2+1].lm>=d)
 66         return a[k*2].r-a[k*2].rm+1;
 67     else
 68         query(d,k*2+1);
 69 }
 70 int main()
 71 {
 72     int n,m,b,c,d;
 73     scanf("%d%d",&n,&m);
 74     {
 75         build(1,n,1);
 76         while(m--)
 77         {
 78             scanf("%d",&b);
 79 
 80             if(b==1)
 81             {
 82                 scanf("%d",&c);
 83                 if(a[1].m<c)
 84                     printf("0\\n");
 85                 else
 86                 {
 87                     int ans=query(c,1);
 88                     printf("%d\\n",ans);
 89                     ins(ans,ans+c-1,1,1);
 90                 }
 91             }
 92             else
 93             {
 94                 scanf("%d%d",&c,&d);
 95                 ins(c,c+d-1,0,1);
 96             }
 97         }
 98     }
 99     return 0;
100 }
View Code

心得体会:虽然代码不是按照自己的思路想出来的,但是感觉高兴的是对于线段树又有了更深的了解,无论是建树,懒惰标记。总之要把线段树的题,要用线段树进行进行各种操作(虽然这种说法挺傻的^_^)



以上是关于poj--3667 Hotel(线段树+区间合并)的主要内容,如果未能解决你的问题,请参考以下文章

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

poj3667-Hotel-线段树-区间合并

poj 3667 Hotel (线段树+区间合并)

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

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

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