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 }

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



时间: 2024-08-09 01:07:59

poj--3667 Hotel(线段树+区间合并)的相关文章

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 ( 线段树区间合并 )

题目链接~~> 做题感悟:这题是接触线段树区间合并的第一题,做的很纠结. 解题思路: 注意线段树上节点代表的信息 : 每个节点需要维护 lc , rc , mc ,add ,见下图: add 为懒惰标记.假设 i 代表父亲节点编号,左儿子为  i * 2  ,右儿子为 i * 2  + 1 ,那么我们可以得到 : T [ i ] .lc 首先加上左儿子的左边的空格数,然后需要判断一下,如果左儿子的左节点占满了整个左区间时需要再加上右儿子的左边的空格数.同理 T [ i ] .rc 也可以这样得到

POJ 3667 Hotel 线段树 区间合并

题意: 1 输入a:询问是不是有连续长度为a的空房间,有的话住进最左边 2 输入a b:将[a,a+b-1]的房间清空 思路:记录区间中最长的空房间 线段树操作: update:区间替换 query:询问满足条件的最左端点 #include <cstdio> #include <iostream> #include <algorithm> #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1

POJ 3667(线段树区间合并)

http://poj.org/problem?id=3667 题意:两个操作 : 1 选出靠左的长度为a的区间. 2 把从 a到a+b的区间清空. 线段树区间合并+lazy // by caonima // hehe #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <cmath> using namespace std; co

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 【线段树 区间合并 + 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

题目传送门 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——————【线段树区间合并】

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

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

Language: Default Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12417   Accepted: 5346 Description The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shores of Lak

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

题意: 有一个线段,从1到n,下面m个操作,操作分两个类型,以1开头的是查询操作,以2开头的是更新操作 1 w 表示在总区间内查询一个长度为w的可用区间并且要最靠左,能找到的话返回这个区间的左端点并占用了这个区间,找不到返回0 2 a len , 表示从单位a开始,清除一段长度为len的区间(将其变为可用,不被占用),不需要输出. 思路: 这是第一次遇到线段树区间合并的题目,写下感悟,还是对线段的更新和查询工作,但是查询的对象的性质已经不像单点那样,查询的是某个线段的最大可用区间是多少,还要一并