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 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 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;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 单位没有抬头纸怎么办在职证明 出国签证无银行流水怎么办 铁路职工得癌症后工作怎么办 去泰国不会泰语和英语怎么办 签证状态一直没有更新怎么办 简理财不能身份信息确认怎么办 德国领事馆没有收到预约邮件怎么办 父母一方带孩子英国签证怎么办 去韩国自由行签证怎么办 韩国自由行签证的该怎么办 法院离婚判决书没了怎么办 离婚判决书对方没收到怎么办 法院判离怎么办离婚证 判决书下来后没钱还怎么办 拿调解书怎么办离婚证 判决书生效后对方拒不履行怎么办 收到民事判决公告该怎么办 苹果手机gps信号弱怎么办 二审败诉后拿到判决书怎么办 农商行房贷逾期一天怎么办 农商行房贷逾期怎么办 农发行车改司机怎么办 混泥土地泵排量不稳怎么办 改嫁上海老公孩子的户口怎么办 给小孩办社保卡怎么办 有上海居住证积分怎么办医保 未办理居住证积分新生儿医保怎么办 厦门中考居住证没满三年怎么办 海归落户过了两年期怎么办 借呗学历填错了怎么办 上海落户应届生分不够怎么办 应届生落户分数不够72分怎么办? 上海应届生落户时间延误怎么办 南京市区户口签江宁怎么办 深圳公司集体户口离职后怎么办 济南本地户口不符合入学条件怎么办 上海住亲戚家怎么办居住证 政府卖非农户口怎么办 90年代买了户口怎么办 上海应届大学生积分不够怎么办 广州居住证回执单丢了怎么办