首页 > 代码库 > poj 3667 Hotel
poj 3667 Hotel
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 10959 | Accepted: 4732 |
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, andDi
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"stdio.h" #define max(a,b) (a>b?a:b) #define N 50005 struct node { int l,r; int lmax,rmax,tmax,flag; }f[N*3]; void creat(int t,int l,int r) { f[t].flag=0; f[t].l=l; f[t].r=r; f[t].lmax=f[t].rmax=f[t].tmax=r-l+1; if(l==r) return ; int mid=(l+r)>>1; creat(t<<1,l,mid); creat(t<<1|1,mid+1,r); } int query(int t,int x) { if(f[t].tmax<x) //房间不够 return 0; if(f[t].lmax>=x) //最小的房间号 return f[t].l; if(f[t<<1].tmax>=x) //次小的房间号 return query(t<<1,x); if(f[t<<1].rmax+f[t<<1|1].lmax>=x) return f[t<<1].r-f[t<<1].rmax+1; return query(t<<1|1,x); } void update(int t,int l,int r,int d) { if(f[t].l==l&&f[t].r==r) { f[t].flag=d; f[t].lmax=f[t].rmax=f[t].tmax=d?0:r-l+1; return ; } int tt=t<<1,mid=(f[t].l+f[t].r)>>1; if(f[t].r>f[t].l) //向下更新区间值 { if(f[t].flag==0) { f[tt].flag=f[tt|1].flag=0; f[tt].lmax=f[tt].rmax=f[tt].tmax=f[tt].r-f[tt].l+1; f[tt|1].lmax=f[tt|1].rmax=f[tt|1].tmax=f[tt|1].r-f[tt|1].l+1; } if(f[t].flag==1) //该区间被完全占用 { f[tt].flag=f[tt|1].flag=1; f[tt].lmax=f[tt].rmax=f[tt].tmax=0; f[tt|1].lmax=f[tt|1].rmax=f[tt|1].tmax=0; } } if(mid>=r) update(tt,l,r,d); else if(mid<l) update(tt|1,l,r,d); else { update(tt,l,mid,d); update(tt|1,mid+1,r,d); } f[t].lmax=f[tt].lmax; //向上更新区间值 f[t].rmax=f[tt|1].rmax; if(f[tt].flag==0) f[t].lmax+=f[tt|1].lmax; if(f[tt|1].flag==0) f[t].rmax+=f[tt].rmax; f[t].tmax=max(f[tt].tmax,f[tt|1].tmax); f[t].tmax=max(f[t].tmax,f[tt].rmax+f[tt|1].lmax); if(f[tt].flag==f[tt|1].flag) f[t].flag=f[tt].flag; else f[t].flag=-1; //该区间被部分占用 } int main() { int n,m,x,y,t,q; while(scanf("%d%d",&n,&m)!=-1) { creat(1,1,n); while(m--) { scanf("%d",&q); if(q==1) { scanf("%d",&x); t=query(1,x); printf("%d\n",t); if(t) update(1,t,t+x-1,1); } else { scanf("%d%d",&x,&y); update(1,x,x+y-1,0); } } } return 0; }