POJ3667——Hotel(线段树)
2013-04-01 10:18
162 查看
Hotel
Time Limit: 3000MS
Memory Limit: 65536K
Total Submissions: 8531Accepted: 3626
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 6
1 3
1 3
1 3
1 3
2 5 5
1 6
Sample Output
1
4
7
0
5
Source
USACO 2008 February Gold
解析:
经典的线段树。。。
s表示最长连续空房间的长度,z表示从左端点起的最长空房间的长度,y表示从右端点起的最长空房间的长度。。。
lazy标记 1为满,0为空,-1为混合
找起始位置,s[左儿子]>长度,就在左儿子找 else if(s[右儿子]>长度) 就在右儿子找 如果跨区间 即y[p<<1]+z[(p<<1)+1]>长度 直接返回起始位置
代码:
Time Limit: 3000MS
Memory Limit: 65536K
Total Submissions: 8531Accepted: 3626
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 6
1 3
1 3
1 3
1 3
2 5 5
1 6
Sample Output
1
4
7
0
5
Source
USACO 2008 February Gold
解析:
经典的线段树。。。
s表示最长连续空房间的长度,z表示从左端点起的最长空房间的长度,y表示从右端点起的最长空房间的长度。。。
lazy标记 1为满,0为空,-1为混合
找起始位置,s[左儿子]>长度,就在左儿子找 else if(s[右儿子]>长度) 就在右儿子找 如果跨区间 即y[p<<1]+z[(p<<1)+1]>长度 直接返回起始位置
代码:
#include<cstdio> #include<iostream> #include<algorithm> #include<cstdlib> using namespace std; const int N=200010;//4*n int n,mm,z ,s ,y ; //z:从左端点起的最长空序列的长度 y:从右端点起的最长空序列 s:当前节点的最大一段空房的长度 int vis ; //lazy标记:0为空, 1为满, -1为混合 void doit(int p,int l,int r) { z[p]=s[p]=y[p]=vis[p]?0:r-l+1; } void up(int p,int l,int r) { int m=(l+r)>>1; if(m-l+1==s[p<<1])z[p]=s[p<<1]+z[(p<<1)+1];//左儿子全空 else z[p]=z[p<<1]; if(r-m==s[(p<<1)+1])y[p]=s[(p<<1)+1]+y[p<<1];//右儿子全空 else y[p]=y[(p<<1)+1]; s[p]=max(s[p<<1],s[(p<<1)+1]); s[p]=max(s[p],y[p<<1]+z[(p<<1)+1]); } void down(int p,int l,int r) { if(r==l || vis[p]==-1)return ; vis[p<<1]=vis[(p<<1)+1]=vis[p]; vis[p]=-1; int m=(l+r)>>1; doit(p<<1,l,m); doit((p<<1)+1,m+1,r); } void build(int p,int l,int r) { z[p]=y[p]=s[p]=r-l+1; vis[p]=-1; if(l==r)return ; int m=(l+r)>>1; build(p<<1,l,m); build((p<<1)+1,m+1,r); } int get(int p,int l,int r,int x) //查找客人从哪间开始入住 { down(p,l,r); if(r==l)return x=1; int m=(l+r)>>1; if(s[p<<1]>=x)return get(p<<1,l,m,x); if(y[p<<1]+z[(p<<1)+1]>=x)return m-y[p<<1]+1; if(s[(p<<1)+1]>=x) return get((p<<1)+1,m+1,r,x); return 0; } void update(int p,int ll,int rr,int l,int r,int x)//x=1:客人入住 x=0:清空客人 { if(l<=ll && r>=rr) { vis[p]=x; doit(p,ll,rr); return ; } down(p,ll,rr); int m=(ll+rr)>>1; if(r<=m)update(p<<1,ll,m,l,r,x); else if(m<l)update((p<<1)+1,m+1,rr,l,r,x); else update(p<<1,ll,m,l,m,x),update((p<<1)+1,m+1,rr,m+1,r,x); up(p,ll,rr); } void readdata() { freopen("poj3667.in","r",stdin); freopen("poj3667.out","w",stdout); scanf("%d%d",&n,&mm); } void work() { int x,l,len; build(1,1,n); for(int i=1;i<=mm;i++) { int op; scanf("%d",&op); if(op==1) { scanf("%d\n",&x); int xx=get(1,1,n,x); cout<<xx<<endl; if(xx)update(1,1,n,xx,xx+x-1,1); } if(op==2) { scanf("%d%d\n",&l,&len); update(1,1,n,l,l+len-1,0); } } } int main() { readdata(); work(); return 0; }
相关文章推荐
- 线段树 poj3667 Hotel
- 【poj3667】【线段树】Hotel
- poj3667 Hotel (线段树+区间合并)
- poj3667 hotel(线段树区间合并)
- poj3667---Hotel 线段树区间合并,区间更新
- poj3667 Hotel (线段树区间合并)
- 【POJ3667】Hotel-线段树
- 【线段树区间合并】POJ3667-Hotel
- [POJ3667]Hotel(线段树,区间合并,重写)
- poj3667 Hotel(线段树)
- [POJ3667]Hotel(线段树,区间合并)
- poj3667 Hotel (线段树--区间合并)转自网络【2】
- poj3667-Hotel 线段树区间合并
- POJ3667——线段树区间合并(未搞透)——Hotel
- poj3667 Hotel (线段树--区间合并)转自网络
- POJ3667:Hotel(线段树区间合并)
- Hotel---poj3667(线段树区间问题)
- 【POJ3667】Hotel(线段树)
- poj3667 Hotel 线段树延迟更新 区间合并
- [线段树]poj3667 Hotel(区间合并、更新、延迟/懒惰标记