POJ Hotel (线段树--区间合并[区间赋值])
来源:互联网 发布:淘宝怎样设置运费模板 编辑:程序博客网 时间:2024/05/24 07:23
题意:
n间空房子, 操作1问你能否连续住x 个房子, 如果能 就输出最左边的编号。 如果不能输出0;
操作2 将给定的区间[x,y] 清空。
思路:
线段树区间合并。
像极了HDU 1540 点击打开博客链接
建树 和 那个题一样, 每个结点 维护 一个区间 从左边开始最大连续长度l, 从右边开始最大连续长度r, 中间最大连续长度m(只不过改成了区间赋值的形式,加一个懒惰标记即可)
关键是查询。 查询挺有意思的。
传一个要查询的长度len 参数,直接类似单点查询递归的形式。
因为是最左边的编号, 先看左儿子的最大连续长度m 是否大于等于len 是的话,解肯定在左儿子上了。 直接递归到左区间上去。
如果不在左儿子上 看看是不是在左+右的中间区间上。
在看看是否在右儿子上。
现在才意识到 查询不必pushup()= =
#include <cstdio>#include <cstring>#include <algorithm>#define Max(a,b) ((a)<(b)?(b):(a))using namespace std;const int maxn = 50000 + 10;struct Node{ int L,R,len; int l,r,m; /// the sum of the empty chair!!!!!!!!!!!! int setv;}nod[maxn<<2];void pushup(int o){ int lson = o << 1; int rson = o << 1 | 1; nod[o].l = nod[lson].l; if (nod[lson].l == nod[lson].len) nod[o].l += nod[rson].l; nod[o].r = nod[rson].r; if (nod[rson].r == nod[rson].len) nod[o].r += nod[lson].r; nod[o].m = Max(nod[o].l, nod[o].r); nod[o].m = Max(nod[lson].m, Max(nod[rson].m, nod[o].m)); nod[o].m = Max(nod[o].m, nod[lson].r + nod[rson].l);}void pushdown(int o,int l,int r){ if (nod[o].setv != -1){ int lson = o << 1; int rson = o << 1 | 1; int m = l + r >> 1; nod[lson].setv = nod[rson].setv = nod[o].setv; if (nod[o].setv == 1){ nod[lson].l = nod[lson].r = nod[lson].m = 0; nod[rson].l = nod[rson].r = nod[rson].m = 0; } else { nod[lson].l = nod[lson].r = nod[lson].m = m-l+1; nod[rson].l = nod[rson].r = nod[rson].m = r-m; } nod[o].setv = -1; }}void build(int l,int r,int o){ nod[o].L = l; nod[o].R = r; nod[o].len = r - l + 1; nod[o].setv = -1; if (l == r){ nod[o].l = nod[o].r = nod[o].m = 1; return; } int m = l + r >> 1; build(l,m,o<<1); build(m+1,r,o<<1|1); pushup(o);}void update(int L,int R,int c,int l,int r,int o){ if (L <= l && r <= R){ nod[o].setv = c; if (c == 1){ nod[o].l = nod[o].r = nod[o].m = 0; } else { nod[o].l = nod[o].r = nod[o].m = r-l+1; } return; } pushdown(o,l,r); int m = l + r >> 1; if (m >= L){ update(L,R,c,l,m,o<<1); } if (m < R){ update(L,R,c,m+1,r,o<<1|1); } pushup(o);}int query(int len, int l,int r,int o){ if (l == r){ return l; } int m = l + r >> 1; int lson = o << 1; int rson = o << 1 | 1; pushdown(o,l,r); if (nod[lson].m >= len){ return query(len, l,m,lson); } else if (nod[lson].r + nod[rson].l >= len){ return m - nod[lson].r + 1; } else { return query(len, m+1,r,rson); }}int main(){ int n, m; scanf("%d %d",&n, &m); build(1,n,1); for (int i = 0; i < m; ++i){ int q,x,y; scanf("%d",&q); if (q == 1){ scanf("%d",&x); int ans = 0; if (nod[1].m >= x) { ans = query(x,1,n,1); update(ans, ans + x - 1, 1, 1, n, 1); } printf("%d\n", ans); } else { scanf("%d%d",&x,&y); update(x,x+y-1,0,1,n,1); } } return 0;}
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 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
[Submit] [Go Back] [Status] [Discuss]
Home Page Go Back To top
- POJ Hotel (线段树--区间合并[区间赋值])
- POJ 3667 Hotel 线段树区间合并
- poj 3667 Hotel 线段树区间合并
- POJ 3667 Hotel 线段树 区间合并
- Poj 3667 Hotel 线段树 区间合并
- POJ Hotel 线段树 区间合并
- POJ 3667-hotel(线段树区间合并)
- Hotel+POJ+线段树区间合并
- POJ 3667 Hotel. (线段树 区间合并)
- 【线段树】POJ 3667 Hotel 区间合并
- [POJ 1823] Hotel 线段树区间合并
- POJ 3667 Hotel ( 线段树区间合并 )
- 线段树 区间合并 poj 3667 Hotel
- POJ 3667 Hotel 线段树 区间合并
- POJ - 3667 Hotel (线段树 + 区间合并)
- POJ 3667 Hotel 【线段树 区间合并】
- poj 3667 Hotel 【线段树区间合并】
- POJ 3667Hotel 线段树区间合并
- android studio 常见问题汇总
- Linux学习基础篇(八)
- Ubuntu Linux 环境变量PATH设置
- linux下PS1命令提示符设置
- 怎样将自己的工程改造成库,并上传到Github作为依赖
- POJ Hotel (线段树--区间合并[区间赋值])
- python操作mysql数据库基本类封装
- WebApp之增删改查(三层)
- bzoj1820: [JSOI2010]Express Service 快递服务
- 排序算法的时间复杂度以及空间复杂度 计数排序
- 第一章 Spring Boot 之Hello World
- Android数据库加解密逆向分析(三)——微信数据库密码破解
- bzoj1391 [Ceoi2008]order 网络流——最大权闭合子图
- [第四章]设计、执行与结项