P2894 [USACO08FEB]酒店Hotel
P2894 [USACO08FEB]酒店Hotel
題目描述
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.
參考樣例,第一行輸入n,m ,n代表有n個房間,編號為1---n,開始都為空房,m表示以下有m行操作,以下 每行先輸入一個數 i ,表示一種操作:
若i為1,表示查詢房間,再輸入一個數x,表示在1--n 房間中找到長度為x的連續空房,輸出連續x個房間中左端的房間號,盡量讓這個房間號最小,若找不到長度為x的連續空房,輸出0。
若i為2,表示退房,再輸入兩個數 x,y 代表 房間號 x---x+y-1 退房,即讓房間為空。
輸入輸出格式
輸入格式:
-
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
輸出格式:
- 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.
輸入輸出樣例
輸入樣例#1:10 6 1 3 1 3 1 3 1 3 2 5 5 1 6輸出樣例#1:
1 4 7 0 5
分析
線段樹維護三個值,區間內最大的連續空位,從左邊最大延伸的長度,從右邊最大延伸的長度。
更新時,有人住讓這些數組都為0,表示0個空位,沒人住,就是區間長度,表示區間內的空位就是長度,都是空位。
其他的詳見代碼註釋。
code
1 #include<cstdio> 2 #include<algorithm> 3 #define lson l,m,rt<<1 4 #define rson m+1,r,rt<<1|1 5 6 using namespace std; 7 8 const int MAXN = 400010; 9 int len[MAXN],sum[MAXN],ml[MAXN],mr[MAXN],tag[MAXN]; 10 //分別區間的長度,區間內最大的連續空位,從左邊延伸最長的空位,同理從右邊,懶標記 11 int n,q; 12 13 void pushup(int rt) 14 { 15 if (sum[rt<<1]==len[rt<<1])//如果左區間全是空位,大區間的ml就是左區間的全部 加 右區間的ml 16 ml[rt] = sum[rt<<1]+ml[rt<<1|1]; 17 else//否則就是最區間ml 18 ml[rt] = ml[rt<<1]; 19 if (sum[rt<<1|1]==len[rt<<1|1])//同理 20 mr[rt] = sum[rt<<1|1]+mr[rt<<1]; 21 else 22 mr[rt] = mr[rt<<1|1]; 23 sum[rt] = max(max(sum[rt<<1],sum[rt<<1|1]),mr[rt<<1]+ml[rt<<1|1]);//大區間的最大連續長度,從兩個子區間和中間的部分區最大的 24 return; 25 } 26 void pushdown(int rt) 27 { 28 if (tag[rt]==0) return; 29 if (tag[rt]==1)//有人住,所以設為0 30 { 31 tag[rt<<1] = tag[rt<<1|1] = 1; 32 sum[rt<<1] = ml[rt<<1] = mr[rt<<1] = 0; 33 sum[rt<<1|1] = ml[rt<<1|1] = mr[rt<<1|1] = 0; 34 } 35 if (tag[rt]==2)//沒有人住,全是空,就是區間的長度 36 { 37 tag[rt<<1] = tag[rt<<1|1] = 2; 38 sum[rt<<1] = ml[rt<<1] = mr[rt<<1] = len[rt<<1]; 39 sum[rt<<1|1] = ml[rt<<1|1] = mr[rt<<1|1] = len[rt<<1|1]; 40 } 41 tag[rt] = 0; 42 } 43 void build(int l,int r,int rt) 44 { 45 ml[rt] = mr[rt] = sum[rt] = len[rt] = r-l+1; 46 tag[rt] = 0; 47 if (l==r) return; 48 int m = (l+r)>>1; 49 build(lson); 50 build(rson); 51 } 52 void update(int l,int r,int rt,int L,int R,int c) 53 { 54 pushdown(rt); 55 if (L<=l&&r<=R) 56 { 57 if (c==1) sum[rt] = ml[rt] = mr[rt] = 0; 58 else sum[rt] = ml[rt] = mr[rt] = len[rt]; 59 tag[rt] = c; 60 return ; 61 } 62 int m = (l+r)>>1; 63 if (L<=m) update(lson,L,R,c); 64 if (R>m) update(rson,L,R,c); 65 pushup(rt); 66 } 67 int query(int l,int r,int rt,int x) 68 { 69 pushdown(rt); 70 if (l==r) return l; 71 int m = (l+r)>>1; 72 if (sum[rt<<1]>=x) return query(lson,x);//如果左區間有x個連續空位,在左區間找 73 if (mr[rt<<1]+ml[rt<<1|1]>=x) return m-mr[rt<<1]+1;//如果左區間的右邊 和 右區間的左邊 加起來有x個連續空位,返回左區間右邊最大時的起點 74 else return query(rson,x);//在右區間找,肯定會有的 75 } 76 int main() 77 { 78 scanf("%d%d",&n,&q); 79 build(1,n,1); 80 while (q--) 81 { 82 int opt,x,y,p; 83 scanf("%d",&opt); 84 if (opt==1) 85 { 86 scanf("%d",&x); 87 if (sum[1]<x)//如果總區間沒有x個連續的空位,輸出0 88 { 89 printf("0\n"); 90 continue; 91 } 92 p = query(1,n,1,x); 93 printf("%d\n",p); 94 update(1,n,1,p,p+x-1,1);//區間不是0,所以數組都是0,見數組定義 95 } 96 else 97 { 98 scanf("%d%d",&x,&y); 99 update(1,n,1,x,x+y-1,2);//將區間設為0,所以數組都是區間的長度 100 } 101 } 102 return 0; 103 }
P2894 [USACO08FEB]酒店Hotel