CF 135 E. Parking Lot (line segment tree)

Source: Internet
Author: User

Question: 1-N parking lots are provided, and there are cars coming in one after another. Each time you select a location, the distance from the nearest car on both sides is required to be the farthest. If there is the same location, the minimum number is used.
Similar to the hotel of POJ, the maximum interval of the record interval, the maximum interval between the left endpoint, and the maximum interval between the right endpoint. And the starting point of the maximum interval.
The updates are complicated. I have been debugging for two hours...
Because the question requires the smallest number, the order of determination here cannot be messy, strictly from left to right.
For the maximum interval, it may start from the left endpoint, And the start point is the smallest, so it is necessary to determine the earliest
The next step may be the maximum interval of the Left interval.
Then the sum of the Left interval between the left interval and the right interval
Then the maximum interval of the right interval
The last is the right endpoint.
Start to feel the same, just YY, and Debug for two hours. If the interval is the same, the minimum number can be ensured in the above Order.
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <algorithm>
# Include <cmath>
# Define eps 1e-10
# Define N 200005
# Define inf 1 <20
# Define zero (a) (fabs (a) <eps)
# Define lson (step <1)
# Define rson (step <1 | 1)
Using namespace std;
Struct Node {
Int left, right, mid;
Int mx, lx, rx, val;
Int dist () {return right-left + 1 ;}
} L [N * 4];
Int pos [1000005];
Void Bulid (int step, int l, int r ){
L [step]. left = l;
L [step]. right = r;
L [step]. mid = (l + r)/2;
L [step]. mx = L [step]. rx = L [step]. lx = L [step]. dist ();
L [step]. val = l;
If (l = r)
Return;
Bulid (lson, l, L [step]. mid );
Bulid (rson, L [step]. mid + 1, r );
}
Void Push_Up (int step ){
L [step]. lx = L [lson]. lx + (L [lson]. lx = L [lson]. dist ()? L [rson]. lx: 0 );
L [step]. rx = L [rson]. rx + (L [rson]. rx = L [rson]. dist ()? L [lson]. rx: 0 );
// Initialize to the leftmost Interval
L [step]. mx = L [step]. lx; L [step]. val = L [step]. left;
// Maximum left Interval
If (L [lson]. mx> L [step]. mx + 1 | (L [lson]. mx> L [step]. mx & L [step]. mx % 2 = 0 )){
L [step]. mx = L [lson]. mx;
L [step]. val = L [lson]. val;
}
// Sum of the right and right ends of the Left Interval
If (L [lson]. rx + L [rson]. lx> L [step]. mx + 1 | (L [lson]. rx + L [rson]. lx> L [step]. mx & L [step]. mx % 2 = 0 )){
L [step]. mx = L [lson]. rx + L [rson]. lx;
L [step]. val = L [lson]. right-L [lson]. rx + 1;
}
// Maximum interval of the right interval
If (L [rson]. mx> L [step]. mx + 1 | (L [rson]. mx> L [step]. mx & L [step]. mx % 2 = 0 )){
L [step]. mx = L [rson]. mx;
L [step]. val = L [rson]. val;
}
// Right endpoint
If (L [step]. rx> L [step]. mx + 1 | (L [step]. rx> L [step]. mx & L [step]. mx % 2 = 0 )){
L [step]. mx = L [step]. rx;
L [step]. val = L [step]. right-L [step]. rx + 1;
}
}
Void update (int step, int p, int k ){
If (L [step]. left = L [step]. right ){
// If K is 1, a car is parked.
If (k ){
L [step]. mx = L [step]. rx = L [step]. lx = 0;
L [step]. val = N;
}
Else {
L [step]. mx = L [step]. rx = L [step]. lx = 1;
L [step]. val = L [step]. left;
}
Return;
}
If (p <= L [step]. mid) update (lson, p, k );
Else update (rson, p, k );
Push_Up (step );
}
Int main (){
Int n, q;
// Freopen ("1.in"," r ", stdin );
// Freopen ("1.out"," w ", stdout );
While (scanf ("% d", & n, & q )! = EOF ){
Bulid (1, 1, n );
Int cnt = 1;
While (q --){
Int ope, id;
Scanf ("% d", & ope, & id );
If (ope = 2) update (1, pos [id], 0 );
Else {
Int len = L [1]. mx, val = L [1]. val, ret;
// If the longest interval is connected to the right endpoint, special consideration is required.
If (val + len-1 = n ){
If (val = 1) ret = 1;
Else ret = n;
}
Else {
Ret = 1; // special consideration left endpoint
Int mmax = L [1]. lx-1; // The interval if placed on the left endpoint
// Consider the longest Interval
If (len-1)/2> mmax ){
Mmax = (len-1)/2;
Ret = val + (len-1)/2;
}
// Consider the right endpoint
If (L [1]. rx-1> mmax ){
Mmax = L [1]. rx-1;
Ret = n;
}
}
Printf ("% d \ n", ret );
Pos [id] = ret;
Update (1, ret, 1 );
}
}
}
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.