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

Hotel

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 17533   Accepted: 7588

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

Source

USACO 2008 February Gold

题目大意:有一个区间1-n,现在需要你完成下面两种操作,

      if op==1 表示需要找到一个连续的长度至少为d的区间,要求尽可能靠左,表示客人入住这些房间,并输出该区间最左边的端点值

      else if op==2 表示客人check out  【l,l+d-1】这一区间

思路:用线段树维护每个区间是否被占用,由于这里要求找到一个连续区间的长度大于等于当前要找的区间长度,并且要尽量靠左,因此需要维护 四个值 ,

  maxlen表示当前区间中最长的连续区间的长度

  llen表示在当前区间 从区间左端点开始的连续区间长度,目的是判断会不会与上一区间连起来

  rlen表示当前区间 从区间右端点往前 的连续区间长度 目的也是为了寻找与后面的区间的连续性

  lazy表示当前区间的性质 0-表示当前区间全部都是空的 1-表示当前区间全部被占用 -1表示当前lazy标记为空 没被修改过或者已经push_down过

  那么很显然 当前区间的maxlen=(lson.maxlen,rson.maxlen,lson.rlen+rson.llen);

  

  

  1 #include<iostream>
  2 #include<stdio.h>
  3
  4 using namespace std;
  5 const int maxn=800000+10;
  6 struct node
  7 {
  8     int l,r;
  9     int maxlen;// 当前区间可用的最长连续长度
 10     int llen;//当前区间从左端点开始的可用区间长度
 11     int rlen;//当前区间从右端点开始的可用区间长度
 12     int num;//num=0 区间未占用 num=1 区间全部被占用 num=-1区间既有被占用又有无占用
 13     int lazy;//lazy=-1 区间的延迟修改标记为空 lazy=1 该区间全部被修改为1 lazy=0 该区间全部被修改为0
 14 }seg[maxn*4];
 15
 16 int Max(int a,int b,int c)
 17 {
 18     return max(max(a,b),c);
 19 }
 20
 21 void buildtree(int x,int l,int r)
 22 {
 23     seg[x].l=l,seg[x].r=r;
 24     seg[x].llen=seg[x].rlen=seg[x].maxlen=r-l+1;
 25     seg[x].lazy=-1,seg[x].num=0;
 26     if(l==r) return ;
 27     else {
 28         int mid=(l+r)>>1;
 29         buildtree(x<<1,l,mid);
 30         buildtree(x<<1|1,mid+1,r);
 31     }
 32 }
 33
 34 void push_down(int x)
 35 {
 36     if(seg[x].lazy!=-1){
 37         seg[x<<1].lazy=seg[x<<1|1].lazy=seg[x].lazy;
 38         seg[x<<1].num=seg[x<<1|1].num=seg[x].lazy;
 39         seg[x].lazy=-1;
 40         seg[x<<1].llen=seg[x<<1].rlen=seg[x<<1].maxlen=(seg[x<<1].lazy==0?seg[x<<1].r-seg[x<<1].l+1:0);
 41         seg[x<<1|1].llen=seg[x<<1|1].rlen=seg[x<<1|1].maxlen=(seg[x<<1|1].lazy==0?seg[x<<1|1].r-seg[x<<1|1].l+1:0);
 42     }
 43 }
 44
 45
 46 void push_up(int x)
 47 {
 48     if(seg[x<<1].num==-1||seg[x<<1|1].num==-1||seg[x<<1].num!=seg[x<<1|1].num) seg[x].num=-1;
 49     else if(seg[x<<1].num==seg[x<<1|1].num&&seg[x<<1].num==1) seg[x].num=1;
 50     else if(seg[x<<1].num==seg[x<<1|1].num&&seg[x<<1].num==0) seg[x].num=0;
 51     seg[x].maxlen=Max(seg[x<<1].maxlen,seg[x<<1|1].maxlen,seg[x<<1].rlen+seg[x<<1|1].llen);
 52     seg[x].llen=seg[x<<1].llen;
 53     seg[x].rlen=seg[x<<1|1].rlen;
 54     if(seg[x<<1].llen==seg[x<<1].r-seg[x<<1].l+1) seg[x].llen+=seg[x<<1|1].llen;
 55     if(seg[x<<1|1].rlen==seg[x<<1|1].r-seg[x<<1|1].l+1) seg[x].rlen+=seg[x<<1].rlen;
 56 }
 57
 58 int query(int x,int l,int r,int len)
 59 {
 60     if(l!=r) push_down(x);
 61
 62     if(seg[x].llen>=len) return l;
 63     if(seg[x<<1].maxlen>=len) {return query(x<<1,seg[x<<1].l,seg[x<<1].r,len);}
 64     else if(seg[x<<1].rlen+seg[x<<1|1].llen>=len) return seg[x<<1].r-seg[x<<1].rlen+1;
 65     else if(seg[x<<1|1].maxlen>=len) return query(x<<1|1,seg[x<<1|1].l,seg[x<<1|1].r,len);
 66     else return 0;
 67 }
 68
 69 void update(int x,int l,int r,int left,int right,int change)
 70 {
 71     if(l==r) {
 72         seg[x].lazy=change,seg[x].num=change;
 73         if(change==1) seg[x].llen=seg[x].rlen=seg[x].maxlen=0;
 74         else seg[x].llen=seg[x].rlen=seg[x].maxlen=1;
 75         return ;
 76     }
 77     push_down(x);
 78     int mid=(l+r)>>1;
 79     if(l>=left&&r<=right){
 80         seg[x].num=seg[x].lazy=change;
 81         seg[x].llen=seg[x].rlen=seg[x].maxlen=(change==0?r-l+1:0);
 82
 83         return ;
 84     }
 85     if(left>=mid+1){
 86         update(x<<1|1,seg[x<<1|1].l,seg[x<<1|1].r,left,right,change);
 87     }
 88     else if(right<=mid){
 89         update(x<<1,seg[x<<1].l,seg[x<<1].r,left,right,change);
 90     }
 91     else {
 92         update(x<<1,seg[x<<1].l,seg[x<<1].r,left,right,change);
 93         update(x<<1|1,seg[x<<1|1].l,seg[x<<1|1].r,left,right,change);
 94     }
 95     push_up(x);
 96 }
 97
 98 int main()
 99 {
100     int n,m;
101     while(~scanf("%d%d",&n,&m)){
102     buildtree(1,1,n);
103     for(int i=0;i<m;i++)
104     {
105         int x,y,z;
106         scanf("%d",&x);
107         if(x==1){
108             scanf("%d",&y);
109             int ans=query(1,1,n,y);
110             if(ans) update(1,1,n,ans,ans+y-1,1);
111             printf("%d\n",ans);
112         }
113         else {
114             scanf("%d%d",&y,&z);
115             update(1,1,n,y,y+z-1,0);
116         }
117     }
118
119 }
120 }
时间: 2024-10-10 08:50:18

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

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

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

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

poj3667 线段树 区间合并

1 //Accepted 3728 KB 1079 ms 2 //线段树 区间合并 3 #include <cstdio> 4 #include <cstring> 5 #include <iostream> 6 #include <queue> 7 #include <cmath> 8 #include <algorithm> 9 using namespace std; 10 /** 11 * This is a document

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

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

线段树 区间合并

poj3667 Hotel 区间合并入门题,照着代码打的, 题意:1 a:询问是不是有连续长度为a的空房间,有的话住进最左边       2 a b:将[a,a+b-1]的房间清空思路:记录区间中最长的空房间,开三个数组,msum[rt]表示节点rt内连续的1的个数的最大值,lsum[rt]表示从节点rt左端点开始连续1的个数,rsum[rt]表示从节点rt右端点开始连续1的个数..线段树操作:update:区间替换 query:询问满足条件的最左端点 1 #include<iostream>