There are many balls numbered in ascending order from 1. They are black at first, and now n operations (ai, bi, ci) are given in turn ), in each operation, all balls numbered from ai to bi are colored (black or white) in the ci representation. After n specified operations, evaluate the Left and Right endpoints of the longest continuous white interval.
Here is a trick, that is, we do not need to record all the information about the black interval. The information about the black interval is only used to update the white interval. The left and right ends of each white interval must be recorded.
That is to say, for each white operation, we will directly record this interval. For each black operation, we can see whether it will affect all existing white intervals. If not, ignore it directly. If the black operation has an effect on the existing white Range (for example, a black range covers a part of the white range, or if a black interval appears in the middle of an existing white interval and is divided into two white intervals), you must adjust the left and right values of the existing white interval.
Finally, traverse all the white intervals stored, merge the intersection or intersection intervals, and find the maximum value.
Thanks:
[Cpp]
# Include <stdio. h>
# Include <stdlib. h>
# Define N 2010
Int cnt;
Struct Node {
Int left, right;
} In [N];
Int cmp (const void * a, const void * B ){
Struct Node * c = (Node *);
Struct Node * d = (Node *) B;
Return c-> left-d-> left;
}
Void swap (int & a, int & B ){
If (a> B ){
Int tmp = a; a = B; B = tmp;
}
}
Void white (int a, int B ){
In [cnt]. left =;
In [cnt ++]. right = B;
}
Void black (int a, int B ){
// PS: in [N] contains white intervals.
Int I, tmp = cnt;
For (I = 0; I <tmp; I ++ ){
If (in [I]. left <){
If (in [I]. right> = ){
// The Black interval to be inserted has an intersection with the in [I] interval, overwrites the part of the in [I], and no white interval is added.
If (in [I]. right <= B ){
In [I]. right = A-1;
}
// The right value of the in [I] White interval is smaller than that of B, that is, the black interval to be inserted splits in [I] into two intervals.
Else {
In [cnt]. left = B + 1;
In [cnt ++]. right = in [I]. right;
In [I]. right = A-1;
}
}
}
Else if (in [I]. left <= B ){
// Same as above
If (in [I]. right <= B) {// in [I] is completely overwritten by the black range to be inserted.
In [I]. left = 0; // flag, indicating whether there are white points in the interval
In [I]. right =-1; // you can specify-1 to facilitate the calculation of the Interval Length.
}
Else {// overwrites a portion of the content. You only need to modify the boundary.
In [I]. left = B + 1;
}
}
}
}
Int main ()
{
Int a, B, n, I, index;
Char op [3];
While (~ Scanf ("% d", & n )){
// Memset (in, 0, sizeof (in ));
Cnt = 0;
For (I = 0; I <n; I ++ ){
Scanf ("% d % s", & a, & B, op );
Swap (a, B );
If (op [0] = 'W') white (a, B );
Else black (a, B );
}
Index = 0;
Qsort (in, cnt, sizeof (in [0]), cmp );
Int max = in [0]. right-in [0]. left + 1;
For (I = 1; I <cnt; I ++ ){
If (in [I]. left! = 0 ){
If (in [I]. left <= in [I-1]. right + 1 ){
If (in [I-1]. right <= in [I]. right) {// equivalent to intersection (or intersection) of two white intervals merged
In [I]. left = in [I-1]. left;
}
Else {// This is required. in [I] is updated for in [I + 1 ].
In [I]. left = in [I-1]. left;
In [I]. right = in [I-1]. right;
}
}
}
(In [I]. right-in [I]. left + 1> max )? (Max = in [I]. right-in [I]. left + 1, index = I): max;
}
Max = 0? Puts ("Oh, my god"): printf ("% d \ n", in [index]. left, in [index]. right );
}
Return 0;
}