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-n,现在需要你完成下面两种操作,
if op==1 表示需要找到一个连续的长度至少为d的区间,要求尽可能靠左,表示客人入住这些房间,并输出该区间最左边的端点值
else if op==2 表示客人check out 【l,l+d-1】这一区间
思路:用线段树维护每个区间是否被占用,由于这里要求找到一个连续区间的长度大于等于当前要找的区间长度,并且要尽量靠左,因此需要维护 四个值 ,
maxlen表示当前区间中最长的连续区间的长度
llen表示在当前区间 从区间左端点开始的连续区间长度,目的是判断会不会与上一区间连起来
rlen表示当前区间 从区间右端点往前 的连续区间长度 目的也是为了寻找与后面的区间的连续性
lazy表示当前区间的性质 0-表示当前区间全部都是空的 1-表示当前区间全部被占用 -1表示当前lazy标记为空 没被修改过或者已经push_down过
那么很显然 当前区间的maxlen=(lson.maxlen,rson.maxlen,lson.rlen+rson.llen);
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 }