Topic 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.
Reference to the sample, the first line of input n, m, n represents n rooms, numbered 1 - n, the beginning is empty room, m means the following M lines of operation, the following line first input a number i, indicating an operation:
If I is 1, it means to inquire about the room, and then input a number x to indicate that a continuous room with length x is found i n a room 1-n, and output the room number of the left end of the continuous x rooms, so as to minimize the room number as far as possible. If no continuous room with length x is found, output 0.
If i is 2, it means check out, then enter two numbers x, y means check out of room number x - - x + Y - 1, that is to say, let the room be empty.
Input and output format
Input format:
-
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 format:
- 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.
Input and Output Samples
10 6 1 3 1 3 1 3 1 3 2 5 5 1 6
4 7 0 5
At first, I thought that the method of borrowing the line tree of the question in the classroom was very similar, but after careful consideration, I found that the two questions were not of the same grade at all.
The general idea of this question is:
Let not rent 1, rent 0
For each interval l,r
1. Store the longest length of 0 from l to back
2. Save the longest length from r to 0
3. Keep the longest length of 0 in the whole interval.
4. Length of preservation interval
For each update, we consider the original idle situation of the left child, the original idle situation of the right child, and the new idle situation of the middle part when they are put together.
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #define lli long long int 6 #define ls k<<1 7 #define rs k<<1|1 8 using namespace std; 9 const int MAXN=100001; 10 inline void read(int &n) 11 { 12 char c='+';int x=0;bool flag=0; 13 while(c<'0'||c>'9'){c=getchar();if(c=='-')flag=1;} 14 while(c>='0'&&c<='9'){x=x*10+c-48;c=getchar();} 15 flag==1?n=-x:n=x; 16 } 17 struct node 18 { 19 lli l,r,w,f,lfree,rfree,allfree,chang; 20 }tree[MAXN<<2]; 21 int n,m; 22 inline void update(int k) 23 { 24 if(tree[ls].allfree==tree[ls].chang) 25 tree[k].lfree=(tree[ls].chang+tree[rs].lfree); 26 else 27 tree[k].lfree=(tree[ls].lfree); 28 29 if(tree[rs].allfree==tree[rs].chang) 30 tree[k].rfree=(tree[rs].chang+tree[ls].rfree); 31 else 32 tree[k].rfree=tree[rs].rfree; 33 34 tree[k].allfree=max(tree[ls].allfree,tree[rs].allfree); 35 tree[k].allfree=max(tree[k].allfree,tree[ls].rfree+tree[rs].lfree); 36 37 return ; 38 } 39 inline void build_tree(int ll,int rr,int k) 40 { 41 tree[k].l=ll;tree[k].r=rr; 42 tree[k].chang=(tree[k].r-tree[k].l+1); 43 if(ll==rr) 44 { tree[k].lfree=tree[k].rfree=tree[k].allfree=(tree[k].r-tree[k].l+1); return ; } 45 lli mid=(tree[k].l+tree[k].r)>>1; 46 build_tree(ll,mid,ls); 47 build_tree(mid+1,rr,rs); 48 update(k); 49 } 50 inline void pushdown(int k,int how) 51 { 52 if(how==1) 53 { 54 tree[ls].lfree=tree[ls].rfree=tree[ls].allfree=0; 55 tree[rs].lfree=tree[rs].rfree=tree[rs].allfree=0; 56 tree[ls].f=tree[k].f; 57 tree[rs].f=tree[k].f; 58 tree[k].f=0; 59 } 60 else 61 { 62 tree[ls].lfree=tree[ls].rfree=tree[ls].allfree=tree[ls].chang; 63 tree[rs].lfree=tree[rs].rfree=tree[rs].allfree=tree[rs].chang; 64 tree[ls].f=tree[k].f; 65 tree[rs].f=tree[k].f; 66 tree[k].f=0; 67 } 68 return ; 69 } 70 inline int query(int k,int num) 71 { 72 if(tree[k].f) 73 pushdown(k,tree[k].f); 74 if(tree[k].r==tree[k].l) 75 return tree[k].l; 76 if(tree[ls].allfree>=num) 77 return query(ls,num); 78 if(tree[ls].rfree+tree[rs].lfree>=num) 79 return tree[ls].r-tree[ls].rfree+1; 80 else 81 return query(rs,num); 82 } 83 inline void change(int k,int ll,int rr,int how) 84 { 85 if(ll<=tree[k].l&&tree[k].r<=rr) 86 { 87 if(how==1) 88 tree[k].allfree=tree[k].lfree=tree[k].rfree=0; 89 else 90 tree[k].allfree=tree[k].lfree=tree[k].rfree=tree[k].chang; 91 92 tree[k].f=how; 93 return ; 94 } 95 int mid=(tree[k].l+tree[k].r)>>1; 96 if(tree[k].f) 97 pushdown(k,tree[k].f); 98 if(ll<=mid) 99 change(ls,ll,rr,how); 100 if(rr>mid) 101 change(rs,ll,rr,how); 102 update(k); 103 } 104 int main() 105 { 106 read(n);read(m); 107 build_tree(1,n,1); 108 for(int i=1;i<=m;i++) 109 { 110 int how; 111 read(how); 112 if(how==1) 113 { 114 int num; 115 read(num); 116 if(tree[1].allfree<num) 117 { 118 printf("0\n"); 119 continue; 120 } 121 int pos=query(1,num); 122 change(1,pos,pos+num-1,1);// Place 123 printf("%d\n",pos); 124 } 125 else 126 { 127 int xx,yy; 128 read(xx);read(yy); 129 change(1,xx,xx+yy-1,2);//empty 130 } 131 } 132 return 0; 133 }