[线段树]poj3667 Hotel(区间合并、更新、延迟/懒惰标记
来源:互联网 发布:合肥工业大学真空知乎 编辑:程序博客网 时间:2024/04/27 15:31
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 rto 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 Di (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 61 31 31 31 32 5 51 6
Sample Output
14705
Source
题意:
思路:
区间合并模板,思路都在代码里
代码:
#include <iostream>#include <stdio.h>#include <string.h>#define inf 1<<29using namespace std;const int N = 5e4+10;struct{ int l, r; int mark;//-1未标记,0入住,1退宿(之所以-1、1不合成一个是因为有延迟/懒惰标记 int lsum, rsum, msum;//左区间(下界为起点的连续区间),右区间(上界为终点的连续区间),最大连续区间}Tree[N*4];//建树void build(int root, int left, int right){ Tree[root].mark = -1; Tree[root].l = left; Tree[root].r = right; Tree[root].msum = Tree[root].lsum = Tree[root].rsum = right - left + 1; if(left == right) return; int mid = (left + right) >> 1; build(root<<1, left, mid); build(root<<1|1, mid+1, right);}//延迟标记向下传递,维护root节点之下区间的准确性void pushdown(int root){ int k = Tree[root].mark; if(k != -1) { int mid = (Tree[root].l + Tree[root].r) >> 1; Tree[root<<1].mark = Tree[root<<1|1].mark = k; Tree[root<<1].msum = Tree[root<<1].lsum = Tree[root<<1].rsum = k ? (mid-Tree[root].l+1) : 0; Tree[root<<1|1].msum = Tree[root<<1|1].lsum = Tree[root<<1|1].rsum = k ? (Tree[root].r-mid) : 0; Tree[root].mark = -1; }}int query(int root, int len){ if(Tree[root].l == Tree[root].r) return Tree[root].l; //向下维护,延迟/懒惰标记传递 pushdown(root); int mid = (Tree[root].l + Tree[root].r) >> 1; //左子节点的最大连续区间 if(Tree[root<<1].msum >= len) return query(root<<1, len); //左子节点的右区间+右子节点的左区间 else if(Tree[root<<1].rsum + Tree[root<<1|1].lsum >= len) return mid - Tree[root<<1].rsum + 1; return query(root<<1|1, len);}//延迟标记向上更新,维护root节点之上区间的准确性void pushup(int root){ int mid = (Tree[root].l + Tree[root].r) >> 1; //当前节点的左区间一定包含左节点的左区间 Tree[root].lsum = Tree[root<<1].lsum; //当前节点的右区间一定包含右节点的右区间 Tree[root].rsum = Tree[root<<1|1].rsum; if(Tree[root<<1].lsum == mid-Tree[root].l+1) //当前节点的左区间等于左节点的最大连续区间,也包含右节点的左区间 Tree[root].lsum += Tree[root<<1|1].lsum; if(Tree[root<<1|1].rsum == Tree[root].r-mid) //当前节点的右区间等于右节点的最大连续区间,也包含左节点的右区间 Tree[root].rsum += Tree[root<<1].rsum; //当前节点的最大连续区间 = max{左节点的最大连续区间,右节点的最大连续区间,左节点右区间+右节点左区间}; int t = max(Tree[root<<1].msum, Tree[root<<1|1].msum); Tree[root].msum = max(Tree[root<<1].rsum+Tree[root<<1|1].lsum, t);}void update(int root, int l, int r, int value){ if(l > Tree[root].r || r < Tree[root].l) return; if(l <= Tree[root].l && r >= Tree[root].r) { Tree[root].mark = value; Tree[root].msum = Tree[root].lsum = Tree[root].rsum = value ? (Tree[root].r-Tree[root].l+1) : 0; return; } //延迟标记向下传递 pushdown(root); update(root<<1, l, r, value); update(root<<1|1, l , r, value); //向上恢复标记 pushup(root);}int main(){ int n, m; int a, b, c; scanf("%d%d", &n, &m); build(1, 1, n); while(m-->0) { scanf("%d", &a); if(a == 1)//入住,区间查询+更新 { scanf("%d", &b); if(b > Tree[1].msum) printf("0\n"); else { int t = query(1, b); printf("%d\n", t); update(1, t, t+b-1, 0); } } else//退宿,区间更新 { scanf("%d%d", &b, &c); update(1, b, b+c-1, 1); } } return 0;}/*10 61 31 31 31 32 5 51 6------14705*/
- [线段树]poj3667 Hotel(区间合并、更新、延迟/懒惰标记
- poj3667 Hotel 线段树延迟更新 区间合并
- poj3667 Hotel (线段树区间合并)
- POJ3667:Hotel(线段树区间合并)
- poj3667 Hotel 线段树 区间合并
- POJ3667-Hotel-线段树区间合并(模板)
- poj3667-Hotel 线段树区间合并
- poj3667 Hotel (线段树+区间合并)
- POJ 3667 Hotel(线段树+区间合并+延迟标记)
- poj3667 Hotel (线段树--区间合并)转自网络
- poj3667线段树区间合并
- poj3667 线段树+区间合并
- poj3667(线段树区间合并)
- 线段树区间合并poj3667
- poj3667 线段树区间合并
- poj3667(线段树区间合并)
- POJ3667(线段树区间合并)
- poj3667(线段树区间合并)
- 汉诺塔小游戏
- zzuli 1919(二分)
- Java Process.waitFor()这个方法是做什么用的
- BZOJ2038: [2009国家集训队]小Z的袜子(hose) 莫队算法 莫队算法讲解及时间复杂度证明
- 内存分析工具MAT(Memory Analyzer Tool)从安装到使用
- [线段树]poj3667 Hotel(区间合并、更新、延迟/懒惰标记
- 实现安卓直播-第三方平台(暴风云视频直播)
- Android 自定义 ViewPager 打造千变万化的图片切换效果
- ZYF loves binary(dp)
- 解决ScrollView和GridView滑动冲突的问题
- 51nod 最大子段和
- 198. House Robber
- 机房收费系统(五)——结账
- Design Support Library (I): Navigation View的使用