POJ 3667 - Hotel(线段树+区间合并)
来源:互联网 发布:美国陆军3 知乎 编辑:程序博客网 时间:2024/04/28 18:56
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 Dicontiguous 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 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
题意:n个人要住房,如果旅馆有足够空闲房间,那么只住进最左边的那些客房。
最基础的区间合并了。弱鸡表示还是得好好学习。
#include <stdio.h>#include <string.h>#include <cmath>#include <algorithm>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1const int maxn = 1e5 + 10;int lsum[maxn << 2], rsum[maxn << 2], msum[maxn << 2], cover[maxn << 2];void PushUp(int rt, int m){lsum[rt] = lsum[rt << 1];rsum[rt] = rsum[rt << 1 | 1];if (lsum[rt << 1] == m - m / 2) lsum[rt] += lsum[rt << 1 | 1];if (rsum[rt << 1 | 1] == m / 2) rsum[rt] += rsum[rt << 1];msum[rt] = max(rsum[rt << 1] + lsum[rt << 1 | 1], max(msum[rt << 1], msum[rt << 1 | 1]));}void PushDown(int rt, int m){if (cover[rt] != -1){cover[rt << 1] = cover[rt << 1 | 1] = cover[rt];msum[rt << 1] = lsum[rt << 1] = rsum[rt << 1] = cover[rt] ? 0 : m - m / 2;msum[rt << 1 | 1] = lsum[rt << 1 | 1] = rsum[rt << 1 | 1] = cover[rt] ? 0 : m / 2;cover[rt] = -1;}}void build(int l, int r, int rt){lsum[rt] = rsum[rt] = msum[rt] = r - l + 1;cover[rt] = -1;//0表示里面没住人,1表示有,-1表示初始状态或者不确定。if (l == r) return;int m = (l + r) >> 1;build(lson);build(rson);}int query(int c, int l, int r, int rt){if (l == r) return l;PushDown(rt, r - l + 1);int m = (l + r) >> 1;if (c <= msum[rt << 1])return query(c, lson);else if (c <= rsum[rt << 1] + lsum[rt << 1 | 1])return m - rsum[rt << 1] + 1;elsereturn query(c, rson);}void update(int L, int R, int c, int l, int r, int rt){if (L <= l&&r <= R){lsum[rt] = rsum[rt] = msum[rt] = c ? 0 : r - l + 1;cover[rt] = c;return;}PushDown(rt, r - l + 1);int m = (l + r) >> 1;if (L <= m) update(L, R, c, lson);if (R > m) update(L, R, c, rson);PushUp(rt, r - l + 1);}int main(){int n, T;while (scanf("%d%d", &n, &T) != EOF){int op;build(1, n, 1);while (T--){scanf("%d", &op);if (op == 1){int a;scanf("%d", &a);if (a > msum[1])puts("0");else{int p = query(a, 1, n, 1);printf("%d\n", p);update(p, p + a - 1, 1, 1, n, 1);}}else{int a, b;scanf("%d%d", &a, &b);update(a, a + b - 1, 0, 1, n, 1);}}}return 0;}
- POJ 3667 Hotel 线段树区间合并
- poj 3667 Hotel 线段树区间合并
- POJ 3667 Hotel 线段树 区间合并
- Poj 3667 Hotel 线段树 区间合并
- POJ 3667-hotel(线段树区间合并)
- POJ 3667 Hotel. (线段树 区间合并)
- 【线段树】POJ 3667 Hotel 区间合并
- POJ 3667 Hotel ( 线段树区间合并 )
- 线段树 区间合并 poj 3667 Hotel
- POJ 3667 Hotel 线段树 区间合并
- POJ - 3667 Hotel (线段树 + 区间合并)
- POJ 3667 Hotel 【线段树 区间合并】
- poj 3667 Hotel 【线段树区间合并】
- POJ 3667Hotel 线段树区间合并
- POJ 3667 - Hotel(线段树+区间合并)
- poj 3667 Hotel 线段树区间合并
- poj 3667 hotel (线段树+区间合并)
- POJ 3667 Hotel 区间合并+线段树 *
- css 定位与行框&行内框
- PAT-B 1051. 复数乘法
- mysql中 character set 和collation关系
- leetcode题集——最长回文子串(待整理)
- Android--进度条--ProgressBar--SeekBar
- POJ 3667 - Hotel(线段树+区间合并)
- android开发 使用Properties读取数据
- Deeplink做不出效果,那是你不会玩!
- Android技术——进程的注入技术的应用
- cuda 安装
- MySQL 乐观锁和悲观锁
- 【高精度算法】A-B 要求:除数与被减数均为正整数
- linux教程——1.启动过程
- android studio build工程时报错agpbi{}