HDU 4302 (zkw line segment tree-maximum value of the single point modification interval)

Source: Internet
Author: User

Holedox Eating
Problem Description
Holedox can be moved online on an online line with a length of L. It may appear online from time to time (the whole point is guaranteed ). Holedox will go to the nearest vertex to eat the cake. if the distance between the nearest cake and the nearest cake is the same, it will take the last walk. If there is no cake, it will stay in the same place.
 

Input
Number of first behavior data groups T (1 <=t <= 10)
For each group of data, the first row has two integers, L, n (1 <= L, n <= 100000), indicating the length of the Line Segment and the number of parts.
In the next n rows, each row has one event: '0 x' indicates that a piece of cake appears at the position of x, and '1' indicates that Holedox will eat 1 piece of cake.
Holedox starts at 0.
 

Output
Output a line of 'Case I: dis 'for each group of data. dis indicates the moving distance of Holedox.
 

Sample Input
3
10 8
0 1
0 5
1
0 2
0 0
1
1
1

10 7
0 1
0 5
1
0 2
0 0
1
1

10 8
0 1
0 1
0 5
1
0 2
0 0
1
1
 

Sample Output
Case 1: 9
Case 2: 4
Case 3: 2
 
This is the zkw line segment tree (a [] indicates the number) with the maximum value of the single-point modification + interval)
There are several tips for this question:
1. Multiple line segments are opened;
2. '0' Point Inclusion (adding all numbers to 1 obviously does not affect the distance of Holedox)
3. Additional information record (to convert this question into a line segment tree)

[Cpp]
# Include <cstdio>
# Include <cstring>
# Include <cstdlib>
# Include <cmath>
# Include <cctype>
# Include <iostream>
Using namespace std;
# Define MAXT (10 + 10)
# Define MAXN (100000 + 10)
# Define MAXm (100000 + 10)
# Define INF (2139062143)
Int n, m, M, T, now, direction, ans;
Int t [2] [MAXN * 10]; // 0-> max 1-> min
Int a [MAXN];
Void dec (int x)
{
A [x] --;
If (a [x] = 0)
{
X + = M;
T [0] [x] = 0; t [1] [x] = INF;
For (x> = 1; x> = 1)
{
T [0] [x] = max (t [0] [x <1], t [0] [(x <1) ^ 1]);
T [1] [x] = min (t [1] [x <1], t [1] [(x <1) ^ 1]);
}
}
}
Void go_left (int Lans)
{
Ans + = now-Lans;
Now = Lans; direction =-1;
Dec (now );
}
Void go_right (int Rans)
{
Ans + = Rans-now;
Now = Rans; direction = 1;
Dec (now );
}
Int main ()
{
Freopen ("Holding. in", "r", stdin );
Scanf ("% d", & T );
For (int k = 1; k <= T; k ++)
{
Memset (t [0], 0, sizeof (t [0]); ans = 0;
Memset (t [1], 127, sizeof (t [1]);
Memset (a, 0, sizeof (a); now = 1; direction = 1; // 1-> right-1-> left
Scanf ("% d", & n, & m); n + = 2;
M = 1; while (M-2 <n) M <= 1;
For (int I = 1; I <= m; I ++)
{
Int p1, p2;
Scanf ("% d", & p1 );
If (p1 = 0)
{
Scanf ("% d", & p2); p2 + = 1;
A [p2] ++;
If (a [p2] = 1)
{
P2 + = M; t [0] [p2] = t [1] [p2] = p2-M;
Int p = p2;
For (p> = 1; p> = 1) t [0] [p] = max (t [0] [p <1], t [0] [(p <1) ^ 1]);
For (p2> = 1; p2; p2> = 1) t [1] [p2] = min (t [1] [p2 <1], t [1] [(p2 <1) ^ 1]);
}
}
Else
{
// (0, now + 1)-> [1, now]
Int l = M, r = now + 1 + M, Lans = 0, Rans = INF;
While (l ^ r ^ 1)
{
If (~ L & 1) Lans = max (Lans, t [0] [l + 1]);
If (r & 1) Lans = max (Lans, t [0] [r-1]);
L >>> = 1; r >>> = 1;
}
// (Now-1, n)-> [now, n-1]
L = now-1 + M; r = n + M;
While (l ^ r ^ 1)
{
If (~ L & 1) Rans = min (Rans, t [1] [l + 1]);
If (r & 1) Rans = min (Rans, t [1] [r-1]);
L >>> = 1; r >>> = 1;
} Www.2cto.com
If (Lans = 0 & Rans = INF) continue;
Else if (Lans = 0) go_right (Rans );
Else if (Rans = INF) go_left (Lans );
Else if (now-Lans <Rans-now) go_left (Lans );
Else if (now-Lans> Rans-now) go_right (Rans );
Else if (now-Lans = 0) dec (now );
Else if (direction = 1) go_right (Rans );
Else go_left (Lans );




}
/*
For (int j = 1; j <= M * 2; j ++) if (t [0] [j]) cout <j <": "<t [0] [j] <'';
Cout <endl;
For (int j = 1; j <= M * 2; j ++) if (t [1] [j] <INF) cout <j <": "<t [1] [j] <'';
Cout <endl;
*/




}
Printf ("Case % d: % d \ n", k, ans );

}
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.