POJ 3667 Hotel 【线段树 区间合并】
来源:互联网 发布:张海山锐线体简 mac 编辑:程序博客网 时间:2024/05/16 01: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 groupi requests a set of Di contiguous rooms from Canmuu, the moose staffing the counter. He assigns them some set of consecutive room numbersr..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 ofr to be the smallest possible.
Visitors also depart the hotel from groups of contiguous rooms. Checkout i has the parameters Xi andDi which specify the vacating of roomsXi ..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 andDi (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 integerr, 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
恩,题目大意就是说,房间,只在同一侧,然后是 1 a 表示查询是否存在 a 个连续的空房间(左边优先),若存在,输出最左边的房间号,然后 2 a b 表示从 a 到 b 的房间住的人离开了,也就是这些房间空了可用。因为线段树数组大小扰了我好几天(粗心)。。。。。。
#include <iostream>#include<cstdio>#include<cstring>#define maxn 50000using namespace std;int n,m,c,x,d;struct lnode{ int l,r,len;//左右端点及长度 int ls,rs,ms;//ls从左边开始数最长的连续房间个数 rs从右边开始数 ms区间内最长的 int mark;//用于延迟标记}node[maxn<<2];int maxi(int a,int b){ return a>b?a:b;}void pushup(int o){ node[o].ls=node[o<<1].ls;//从左边开始数首先等于左子树从左边数的长度 if(node[o<<1].ls==node[o<<1].len)//若其长度等于左子树总长则可以加上右子树从左边开始数的 node[o].ls+=node[o<<1|1].ls; node[o].rs=node[o<<1|1].rs;//同上 if(node[o<<1|1].rs==node[o<<1|1].len) node[o].rs+=node[o<<1].rs; node[o].ms=maxi(maxi(node[o<<1].ms,node[o<<1|1].ms),(node[o<<1].rs+node[o<<1|1].ls));}void pushdown(int o){ if(node[o].mark!=-1) { node[o<<1].mark=node[o<<1|1].mark=node[o].mark; node[o].mark=-1; node[o<<1].rs=node[o<<1].ls=node[o<<1].ms=node[o<<1].mark?0:node[o<<1].len;//若为1则表示此时房间非空,为0表示为空,空房间长度即为区间长度 node[o<<1|1].ls=node[o<<1|1].rs=node[o<<1|1].ms=node[o<<1|1].mark?0:node[o<<1|1].len;//同上 }}void build(int o,int l,int r){ node[o].l=l; node[o].r=r; node[o].len=r-l+1; node[o].mark=-1; node[o].ls=node[o].rs=node[o].ms=r-l+1; if(l==r) return ; int mid=(l+r)>>1; build(o<<1,l,mid); build(o<<1|1,mid+1,r); pushup(o);}void update(int o,int l,int r,int c){ if(node[o].l==l&&node[o].r==r) { node[o].ms=node[o].ls=node[o].rs=c?0:(r-l+1);//同上 node[o].mark=c; return ; } pushdown(o); int mid=(node[o].l+node[o].r)>>1; if(r<=mid) update(o<<1,l,r,c); else if(l>mid) update(o<<1|1,l,r,c); else { update(o<<1,l,mid,c); update(o<<1|1,mid+1,r,c); } pushup(o);}int query(int o,int w){ if(node[o].l==node[o].r) return node[o].l; pushdown(o); int mid=(node[o].l+node[o].r)>>1; if(node[o<<1].ms>=w)//在左子树 return query(o<<1,w); else if(node[o<<1].rs+node[o<<1|1].ls>=w)//因为左边优先,所以要先检测中间的在检测右子树 return mid-node[o<<1].rs+1; return query(o<<1|1,w);}int main(){ while(~scanf("%d%d",&n,&m)) { build(1,1,n); while(m--) { scanf("%d",&c); if(c==1) { scanf("%d",&x); if(node[1].ms<x) printf("0\n"); else { int p=query(1,x); printf("%d\n",p); update(1,p,p+x-1,1); } } else { scanf("%d%d",&x,&d); update(1,x,x+d-1,0); } } } 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 区间合并+线段树 *
- 记录——《C Primer Plus (第五版)》第十章编程练习第四题
- Lucene分词器测试
- IsPostBack的用法
- Android视频播放时停止后台运行的方法
- [leetcode 118]Pascal's triangle
- POJ 3667 Hotel 【线段树 区间合并】
- 常用数据结构
- 记录——《C Primer Plus (第五版)》第十章编程练习第五题
- AltiumDesigner画PCB时的规则设置
- iOS CoreData之单表操作(增删改查)
- Redis概念
- CGAffineTransform对View的缩放、平移和旋转
- Fragment基础点滴学习---Fragment的生命周期
- 杭电 3792 Twin Prime Conjecture