poj3667-Hotel-线段树-区间合并
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 17533 | Accepted: 7588 |
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
1 #include<iostream> 2 #include<stdio.h> 3 4 using namespace std; 5 const int maxn=800000+10; 6 struct node 7 { 8 int l,r; 9 int maxlen;// 当前区间可用的最长连续长度 10 int llen;//当前区间从左端点开始的可用区间长度 11 int rlen;//当前区间从右端点开始的可用区间长度 12 int num;//num=0 区间未占用 num=1 区间全部被占用 num=-1区间既有被占用又有无占用 13 int lazy;//lazy=-1 区间的延迟修改标记为空 lazy=1 该区间全部被修改为1 lazy=0 该区间全部被修改为0 14 }seg[maxn*4]; 15 16 int Max(int a,int b,int c) 17 { 18 return max(max(a,b),c); 19 } 20 21 void buildtree(int x,int l,int r) 22 { 23 seg[x].l=l,seg[x].r=r; 24 seg[x].llen=seg[x].rlen=seg[x].maxlen=r-l+1; 25 seg[x].lazy=-1,seg[x].num=0; 26 if(l==r) return ; 27 else { 28 int mid=(l+r)>>1; 29 buildtree(x<<1,l,mid); 30 buildtree(x<<1|1,mid+1,r); 31 } 32 } 33 34 void push_down(int x) 35 { 36 if(seg[x].lazy!=-1){ 37 seg[x<<1].lazy=seg[x<<1|1].lazy=seg[x].lazy; 38 seg[x<<1].num=seg[x<<1|1].num=seg[x].lazy; 39 seg[x].lazy=-1; 40 seg[x<<1].llen=seg[x<<1].rlen=seg[x<<1].maxlen=(seg[x<<1].lazy==0?seg[x<<1].r-seg[x<<1].l+1:0); 41 seg[x<<1|1].llen=seg[x<<1|1].rlen=seg[x<<1|1].maxlen=(seg[x<<1|1].lazy==0?seg[x<<1|1].r-seg[x<<1|1].l+1:0); 42 } 43 } 44 45 46 void push_up(int x) 47 { 48 if(seg[x<<1].num==-1||seg[x<<1|1].num==-1||seg[x<<1].num!=seg[x<<1|1].num) seg[x].num=-1; 49 else if(seg[x<<1].num==seg[x<<1|1].num&&seg[x<<1].num==1) seg[x].num=1; 50 else if(seg[x<<1].num==seg[x<<1|1].num&&seg[x<<1].num==0) seg[x].num=0; 51 seg[x].maxlen=Max(seg[x<<1].maxlen,seg[x<<1|1].maxlen,seg[x<<1].rlen+seg[x<<1|1].llen); 52 seg[x].llen=seg[x<<1].llen; 53 seg[x].rlen=seg[x<<1|1].rlen; 54 if(seg[x<<1].llen==seg[x<<1].r-seg[x<<1].l+1) seg[x].llen+=seg[x<<1|1].llen; 55 if(seg[x<<1|1].rlen==seg[x<<1|1].r-seg[x<<1|1].l+1) seg[x].rlen+=seg[x<<1].rlen; 56 } 57 58 int query(int x,int l,int r,int len) 59 { 60 if(l!=r) push_down(x); 61 62 if(seg[x].llen>=len) return l; 63 if(seg[x<<1].maxlen>=len) {return query(x<<1,seg[x<<1].l,seg[x<<1].r,len);} 64 else if(seg[x<<1].rlen+seg[x<<1|1].llen>=len) return seg[x<<1].r-seg[x<<1].rlen+1; 65 else if(seg[x<<1|1].maxlen>=len) return query(x<<1|1,seg[x<<1|1].l,seg[x<<1|1].r,len); 66 else return 0; 67 } 68 69 void update(int x,int l,int r,int left,int right,int change) 70 { 71 if(l==r) { 72 seg[x].lazy=change,seg[x].num=change; 73 if(change==1) seg[x].llen=seg[x].rlen=seg[x].maxlen=0; 74 else seg[x].llen=seg[x].rlen=seg[x].maxlen=1; 75 return ; 76 } 77 push_down(x); 78 int mid=(l+r)>>1; 79 if(l>=left&&r<=right){ 80 seg[x].num=seg[x].lazy=change; 81 seg[x].llen=seg[x].rlen=seg[x].maxlen=(change==0?r-l+1:0); 82 83 return ; 84 } 85 if(left>=mid+1){ 86 update(x<<1|1,seg[x<<1|1].l,seg[x<<1|1].r,left,right,change); 87 } 88 else if(right<=mid){ 89 update(x<<1,seg[x<<1].l,seg[x<<1].r,left,right,change); 90 } 91 else { 92 update(x<<1,seg[x<<1].l,seg[x<<1].r,left,right,change); 93 update(x<<1|1,seg[x<<1|1].l,seg[x<<1|1].r,left,right,change); 94 } 95 push_up(x); 96 } 97 98 int main() 99 { 100 int n,m; 101 while(~scanf("%d%d",&n,&m)){ 102 buildtree(1,1,n); 103 for(int i=0;i<m;i++) 104 { 105 int x,y,z; 106 scanf("%d",&x); 107 if(x==1){ 108 scanf("%d",&y); 109 int ans=query(1,1,n,y); 110 if(ans) update(1,1,n,ans,ans+y-1,1); 111 printf("%d\n",ans); 112 } 113 else { 114 scanf("%d%d",&y,&z); 115 update(1,1,n,y,y+z-1,0); 116 } 117 } 118 119 } 120 }