洛谷 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
線段樹區間合並
代碼:
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int maxn=5e5+10; 5 #define lson l,m,rt<<1 6 #define rson m+1,r,rt<<1|1 7 8 struct Tree{ 9 int lch,rch,val,lazy; 10 }tree[maxn<<2]; 11 12 void pushup(int l,int r,int rt) 13 { 14 int m=(l+r)>>1; 15 if(tree[rt<<1].val==(m-l+1)) tree[rt].lch=tree[rt<<1].val+tree[rt<<1|1].lch; 16 else tree[rt].lch=tree[rt<<1].lch; 17 if(tree[rt<<1|1].val==(r-m)) tree[rt].rch=tree[rt<<1|1].val+tree[rt<<1].rch; 18 else tree[rt].rch=tree[rt<<1|1].rch; 19 tree[rt].val=max(max(tree[rt<<1].val,tree[rt<<1|1].val),tree[rt<<1].rch+tree[rt<<1|1].lch); 20 } 21 22 void pushdown(int l,int r,int rt) 23 { 24 int m=(l+r)>>1; 25 if(tree[rt].lazy){ 26 if(tree[rt].lazy==1){//空房 27 tree[rt<<1].lazy=tree[rt<<1|1].lazy=tree[rt].lazy; 28 tree[rt<<1].lch=tree[rt<<1].rch=tree[rt<<1].val=m-l+1; 29 tree[rt<<1|1].lch=tree[rt<<1|1].rch=tree[rt<<1|1].val=r-m; 30 } 31 else if(tree[rt].lazy==2){//住滿 32 tree[rt<<1].lazy=tree[rt<<1|1].lazy=tree[rt].lazy; 33 tree[rt<<1].lch=tree[rt<<1].rch=tree[rt<<1].val=0; 34 tree[rt<<1|1].lch=tree[rt<<1|1].rch=tree[rt<<1|1].val=0; 35 } 36 tree[rt].lazy=0; 37 } 38 } 39 40 //兩種build的方式,直接在判斷前面寫,就不需要pushup,寫在判斷裏面就需要pushup,上傳到爸爸 41 void build(int l,int r,int rt) 42 { 43 tree[rt].lazy=0; 44 if(l==r){ 45 tree[rt].lch=tree[rt].rch=tree[rt].val=1; 46 return ; 47 } 48 49 int m=(l+r)>>1; 50 build(lson); 51 build(rson); 52 pushup(l,r,rt); 53 } 54 55 //void build(int l,int r,int rt) 56 //{ 57 // tree[rt].lch=tree[rt].rch=tree[rt].val=r-l+1; 58 // tree[rt].lazy=0; 59 // if(l==r){ 60 // return ; 61 // } 62 // 63 // int m=(l+r)>>1; 64 // build(lson); 65 // build(rson); 66 //} 67 68 void update(int L,int R,int c,int l,int r,int rt)//更新,節點標記已經下傳了,所以下面判斷就不對了 69 { 70 if(tree[rt].lazy!=0){ 71 pushdown(l,r,rt); 72 } 73 74 if(L<=l&&r<=R){ 75 if(c==1){ 76 tree[rt].lch=tree[rt].rch=tree[rt].val=r-l+1; 77 } 78 else if(c==2){ 79 tree[rt].lch=tree[rt].rch=tree[rt].val=0; 80 } 81 tree[rt].lazy=c; 82 return ; 83 } 84 85 int m=(l+r)>>1; 86 if(L<=m) update(L,R,c,lson); 87 if(R> m) update(L,R,c,rson); 88 pushup(l,r,rt); 89 } 90 91 int query(int c,int l,int r,int rt) 92 { 93 if(tree[rt].lazy!=0){ 94 pushdown(l,r,rt); 95 } 96 97 if(l==r){ 98 return l; 99 } 100 101 int m=(l+r)>>1; 102 if(tree[rt<<1].val>=c) return query(c,lson); 103 else if(tree[rt<<1].rch+tree[rt<<1|1].lch>=c) return m-tree[rt<<1].rch+1; 104 else return query(c,rson); 105 } 106 107 int main() 108 { 109 int n,m; 110 scanf("%d%d",&n,&m); 111 build(1,n,1); 112 // for(int i=1;i<=40;i++) 113 // cout<<i<<" "<<tree[i].val<<endl; 114 for(int i=1;i<=m;i++){ 115 int op; 116 scanf("%d",&op); 117 if(op==1){ 118 int x; 119 scanf("%d",&x); 120 if(tree[1].val>=x){ 121 int ans=query(x,1,n,1); 122 printf("%d\n",ans); 123 update(ans,ans+x-1,2,1,n,1);//住滿 124 } 125 else{ 126 printf("0\n"); 127 } 128 } 129 else{ 130 int x,y; 131 scanf("%d%d",&x,&y); 132 update(x,x+y-1,1,1,n,1);//清空 133 } 134 } 135 return 0; 136 }
洛谷 P2894 [USACO08FEB]酒店Hotel-線段樹區間合並(判斷找位置,不需要維護端點)+分治