POJ P3667 Hotel——solution
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.
--by POJ
http://poj.org/problem?id=3667
给定初始值全0的区间[1,n],支持两种操作:
1 #include<cstdio> 2 using namespace std; 3 int n,m,L,R; 4 struct tree{ 5 int lmax,rmax,max,lz; 6 }line[200010]; 7 void builine(int ,int ,int ); 8 void up(int ,int ,int ); 9 void down(int ,int ,int ); 10 int search(int ,int ,int ,int ); 11 void change(int ,int ,int ,int ); 12 int main() 13 { 14 int i,j,k,ans; 15 scanf("%d%d",&n,&m); 16 builine(1,n,1); 17 for(i=1;i<=m;i++){ 18 scanf("%d",&j); 19 if(j==1){ 20 scanf("%d",&k); 21 ans=search(1,n,1,k); 22 printf("%d\n",ans); 23 L=ans;R=L+k-1; 24 if(L) 25 change(1,n,1,1); 26 } 27 else{ 28 scanf("%d%d",&L,&k); 29 R=L+k-1; 30 change(1,n,1,0); 31 } 32 } 33 } 34 void builine(int l,int r,int nu){ 35 if(l==r){ 36 line[nu].lmax=line[nu].rmax=line[nu].max=1; 37 return ; 38 } 39 int mid=(l+r)>>1; 40 builine(l,mid,nu<<1); 41 builine(mid+1,r,nu<<1|1); 42 line[nu].lmax=line[nu].rmax=line[nu].max=line[nu<<1].max+line[nu<<1|1].max; 43 } 44 void up(int l,int r,int nu){ 45 int mid=(l+r)>>1; 46 if(line[nu<<1].max>line[nu<<1|1].max) 47 line[nu].max=line[nu<<1].max; 48 else 49 line[nu].max=line[nu<<1|1].max; 50 if(line[nu].max<line[nu<<1].rmax+line[nu<<1|1].lmax) 51 line[nu].max=line[nu<<1].rmax+line[nu<<1|1].lmax; 52 line[nu].lmax=line[nu<<1].lmax==(mid-l+1)?line[nu<<1].lmax+line[nu<<1|1].lmax:line[nu<<1].lmax; 53 line[nu].rmax=line[nu<<1|1].rmax==(r-mid)?line[nu<<1|1].rmax+line[nu<<1].rmax:line[nu<<1|1].rmax; 54 } 55 void down(int l,int r,int nu){ 56 int mid=(l+r)>>1; 57 if(line[nu].lz){ 58 line[nu<<1].lmax=line[nu<<1].max=line[nu<<1].rmax=((line[nu].lz-1)^1)*(mid-l+1); 59 line[nu<<1|1].lmax=line[nu<<1|1].max=line[nu<<1|1].rmax=((line[nu].lz-1)^1)*(r-mid); 60 line[nu<<1].lz=line[nu<<1|1].lz=line[nu].lz; 61 line[nu].lz=0; 62 } 63 } 64 int search(int l,int r,int nu,int len){ 65 int mid=(l+r)>>1,ans; 66 down(l,r,nu); 67 if(l==r) 68 return line[nu].max*l; 69 if(line[nu<<1].max>=len){ 70 ans=search(l,mid,nu<<1,len); 71 return ans; 72 } 73 if(line[nu<<1].rmax+line[nu<<1|1].lmax>=len){ 74 ans=mid-line[nu<<1].rmax+1; 75 return ans; 76 } 77 if(line[nu<<1|1].max>=len){ 78 ans=search(mid+1,r,nu<<1|1,len); 79 return ans; 80 } 81 return 0; 82 } 83 void change(int l,int r,int nu,int x){ 84 if(L<=l&&r<=R){ 85 line[nu].lmax=line[nu].rmax=line[nu].max=(r-l+1)*(x^1); 86 line[nu].lz=x+1; 87 return ; 88 } 89 down(l,r,nu); 90 int mid=(l+r)>>1; 91 if(L<=mid) 92 change(l,mid,nu<<1,x); 93 if(R>mid) 94 change(mid+1,r,nu<<1|1,x); 95 up(l,r,nu); 96 }
祝AC