题目描述
奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光。作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿。这个巨大的旅馆一共有N (1 <= N <= 50,000)间客房,它们在同一层楼中顺次一字排开,在任何一个房间里,只需要拉开窗帘,就能见到波光粼粼的湖面。 贝茜一行,以及其他慕名而来的旅游者,都是一批批地来到旅馆的服务台,希望能订到D_i (1 <= D_i <= N)间连续的房间。服务台的接待工作也很简单:如果存在r满足编号为r..r+D_i-1的房间均空着,他就将这一批顾客安排到这些房间入住;如果没有满足条件的r,他会道歉说没有足够的空房间,请顾客们另找一家宾馆。如果有多个满足条件的r,服务员会选择其中最小的一个。 旅馆中的退房服务也是批量进行的。每一个退房请求由2个数字X_i、D_i 描述,表示编号为X_i..X_i+D_i-1 (1 <= X_i <= N-D_i+1)房间中的客人全部离开。退房前,请求退掉的房间中的一些,甚至是所有,可能本来就无人入住。 而你的工作,就是写一个程序,帮服务员为旅客安排房间。你的程序一共需要处理M (1 <= M < 50,000)个按输入次序到来的住店或退房的请求。第一个请求到来前,旅店中所有房间都是空闲的。
输入
* 第1行: 2个用空格隔开的整数:N、M
* 第2..M+1行: 第i+1描述了第i个请求,如果它是一个订房请求,则用2个数字 1、D_i描述,数字间用空格隔开;如果它是一个退房请求,用3 个以空格隔开的数字2、X_i、D_i描述
输出
* 第1..??行: 对于每个订房请求,输出1个独占1行的数字:如果请求能被满足 ,输出满足条件的最小的r;如果请求无法被满足,输出0
样例输入
10 6
1 3
1 3
1 3
1 3
2 5 5
1 6
样例输出
1
4
7
0
5
题解
线段树区间合并。
查询不是标准查询,需要自己脑补,其他的写法都差不多,反正别更改到不存在的节点就行。
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define lson l , mid , x << 1 5 #define rson mid + 1 , r , x << 1 | 1 6 using namespace std; 7 int ls[200010] , rs[200010] , ts[200010] , tag[200010]; 8 void pushup(int l , int r , int x) 9 { 10 int mid = (l + r) >> 1; 11 ls[x] = ls[x << 1] == mid - l + 1 ? ls[x << 1] + ls[x << 1 | 1] : ls[x << 1]; 12 rs[x] = rs[x << 1 | 1] == r - mid ? rs[x << 1 | 1] + rs[x << 1] : rs[x << 1 | 1]; 13 ts[x] = max(max(ts[x << 1] , ts[x << 1 | 1]) , rs[x << 1] + ls[x << 1 | 1]); 14 } 15 void pushdown(int l , int r , int x) 16 { 17 if(tag[x] != -1) 18 { 19 int mid = (l + r) >> 1; 20 ls[x << 1] = rs[x << 1] = ts[x << 1] = (mid - l + 1) * (tag[x] ^ 1); 21 ls[x << 1 | 1] = rs[x << 1 | 1] = ts[x << 1 | 1] = (r - mid) * (tag[x] ^ 1); 22 tag[x << 1] = tag[x << 1 | 1] = tag[x]; 23 tag[x] = -1; 24 } 25 } 26 void build(int l , int r , int x) 27 { 28 if(l == r) 29 { 30 ls[x] = rs[x] = ts[x] = 1; 31 return; 32 } 33 int mid = (l + r) >> 1; 34 build(lson); 35 build(rson); 36 pushup(l , r , x); 37 } 38 void update(int b , int e , int a , int l , int r , int x) 39 { 40 if(b <= l && r <= e) 41 { 42 ls[x] = rs[x] = ts[x] = (r - l + 1) * (a ^ 1); 43 tag[x] = a; 44 return; 45 } 46 pushdown(l , r , x); 47 int mid = (l + r) >> 1; 48 if(b <= mid) update(b , e , a , lson); 49 if(e > mid) update(b , e , a , rson); 50 pushup(l , r , x); 51 } 52 int query(int d , int l , int r , int x) 53 { 54 if(ts[x] < d) return 0; 55 if(ls[x] >= d) return l; 56 pushdown(l , r , x); 57 int mid = (l + r) >> 1; 58 if(ts[x << 1] >= d) return query(d , lson); 59 else if(rs[x << 1] + ls[x << 1 | 1] >= d) return mid - rs[x << 1] + 1; 60 else return query(d , rson); 61 } 62 int main() 63 { 64 int n , m , p , x , d; 65 scanf("%d%d" , &n , &m); 66 memset(tag , -1 , sizeof(tag)); 67 build(1 , n , 1); 68 while(m -- ) 69 { 70 scanf("%d" , &p); 71 if(p == 1) 72 { 73 scanf("%d" , &d); 74 x = query(d , 1 , n , 1); 75 printf("%d\n" , x); 76 if(x) update(x , x + d - 1 , 1 , 1 , n , 1); 77 } 78 else 79 { 80 scanf("%d%d" , &x , &d); 81 update(x , x + d - 1 , 0 , 1 , n , 1); 82 } 83 } 84 return 0; 85 }
【bzoj1593】[Usaco2008 Feb]Hotel 旅馆
原文:http://www.cnblogs.com/GXZlegend/p/6409636.html