POJ 3667 Hotel (线段树)
2015-08-15 15:47
316 查看
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
Sample Output
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 6 1 3 1 3 1 3 1 3 2 5 5 1 6
Sample Output
1 4 7 0 5
#include<cstdio> #include<cstring> #define max(a,b) (a>b?a:b) using namespace std; struct f { int x,y,z,v; };f tree[200200]; void build(int l,int r,int p) { tree[p].x=tree[p].y=tree[p].z=r-l+1; tree[p].v=-1; if (l==r) return ; int m=(l+r)/2; build(l,m,p*2); build(m+1,r,p*2+1); } void pushdown(int x,int p) { if (tree[p].v!=-1) { tree[p*2].v=tree[p*2+1].v=tree[p].v; if (tree[p].v==0) { tree[p*2].x=tree[p*2].y=tree[p*2].z=x-(x/2); tree[p*2+1].x=tree[p*2+1].y=tree[p*2+1].z=x/2; } else { tree[p*2].x=tree[p*2].y=tree[p*2].z=0; tree[p*2+1].x=tree[p*2+1].y=tree[p*2+1].z=0; } tree[p].v=-1; } } void pushup(int x,int p) { tree[p].x=tree[p*2].x; tree[p].y=tree[p*2+1].y; if (tree[p].x==x-(x/2)) tree[p].x+=tree[p*2+1].x; if (tree[p].y==x/2) tree[p].y+=tree[p*2].y; tree[p].z=max(tree[p*2].y+tree[p*2+1].x,max(tree[p*2].z,tree[p*2+1].z)); } int find(int l,int r,int w,int p) { if (l==r) return l; pushdown(r-l+1,p); int m=(l+r)/2; if (tree[p*2].z>=w) return find(l,m,w,p*2); if (tree[p*2].y+tree[p*2+1].x>=w) return m-tree[p*2].y+1; return find(m+1,r,w,p*2+1); } void un(int ll,int rr,int l,int r,int p,int w) { if (ll<=l&&rr>=r) { if (w) tree[p].x=tree[p].y=tree[p].z=0; else tree[p].x=tree[p].y=tree[p].z=r-l+1; tree[p].v=w; return ; } pushdown(r-l+1,p); int m=(l+r)/2; if (m<ll) un(ll,rr,m+1,r,p*2+1,w); else if (m>=rr) un(ll,rr,l,m,p*2,w); else { un(ll,m,l,m,p*2,w); un(m+1,rr,m+1,r,p*2+1,w); } pushup(r-l+1,p); } int main() { int n,m,a,b; while (~scanf("%d%d",&n,&m)) { build(1,n,1); while (m--) { scanf("%d",&a); if (a==1) { scanf("%d",&b); if (b>tree[1].z) { printf("0\n"); continue; } a=find(1,n,b,1); printf("%d\n",a); un(a,b+a-1,1,n,1,1); } else { scanf("%d%d",&a,&b); un(a,b+a-1,1,n,1,0); } } } }
相关文章推荐
- UVA 624 (0 1背包 + 打印路径)
- Linux学习笔记(20) Linux系统管理
- Java基础——反射
- 2015年门户网站封杀IP名单
- Ubuntu
- mysql存储过程和函数,事件调度器
- jsp基础学习(五)----jsp指令
- 面向服务的体系结构(SOA)应用的总体技术要求-笔记
- JAVA面试题第一章:JAVA基础
- 批处理打开和关闭oracle11g 服务
- POJ 3687 Labeling Balls (逆向建边)
- Bash shell位置参数
- 使用Win2D在UWP程序中2D绘图(一)
- BZOJ 1084: [SCOI2005]最大子矩阵( dp )
- 初入 CLR - 阅读《CLR via C#》笔记
- 多线程环境下队列操作之锁的教训
- hpuoj 问题 C: 善良的国王【最小生成树kurskal】
- 10.1-7 两个队列实现一个栈
- WAMP/LAMP环境搭配错误解决
- 用Json实现PHP与JavaScript间数据交换