Color the Ball
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 3529 Accepted Submission (s): 874
Problem Description
There are infinite bils in a line (numbered 1 2 3 ....), and initially all of them are painting black. now Jim use a brush paint the bils, every time give two integers a B and follow by a char 'W' or 'B ', 'W' denotes the ball from a to B are painted white, 'B' denotes that be painted black. you are asking to find the longest white ball sequence.
Input
First line is an integer N (<= 2000), the times Jim paint, next N line contain a B c, c can be 'W' and 'B '.
There are multiple cases, process to the end of file.
Output
Two integers the left end of the longest white ball sequence and the right end of longest white ball sequence (If more than one output the small number one ). all the input are less than 2 ^ 31-1. if no such sequence exists, output "Oh, my god ".
Sample Input
3
1 4 w
8 11 w
3 5 B
Sample Output
8 11
// Good habits, I am about to vomit, the first line segment tree to be discretization # include <iostream> # include <cstdio> # include <cstring> # include <algorithm> using namespace std; # define N 404040int a [N], r [N], p [N], c [N], f [N]; // r [I] ranks I in the original position, p [I] int cmp (int I, int j) {return a [I] <a [j];} struct node {int l, r, lz; void init (int _ l, int _ r) {l = _ l; r = _ r; lz =-1 ;}} t [4 * N]; void build (int l, int r, int k) {if (l> = r) return; t [k]. init (l, r); if (l + 1> = r) return; int md = (l + r)/2; bui Ld (l, md, k * 2); build (md, r, k * 2 + 1); // not md + 1 (because md is overwritten )} void down (int k) {if (t [k]. l + 1 = t [k]. r) return; if (t [k]. lz =-1) return; t [k * 2]. lz = t [k * 2 + 1]. lz = t [k]. lz; t [k]. lz =-1;} void DownAll (int k) {if (t [k]. lz! =-1) {for (int I = t [k]. l; I <t [k]. r; I ++) f [I] = t [k]. lz; // The right endpoint is not marked (only marked when it is overwritten) return;} DownAll (k * 2); DownAll (k * 2 + 1 );} void update (int l, int r, int cr, int k) {if (t [k]. l> = t [k]. r) return; if (l = t [k]. l & r = t [k]. r) {t [k]. lz = cr; // mark return;} down (k); if (t [k]. l + 1> = t [k]. r) return; int md = (t [k]. l + t [k]. r)/2; if (md> = r) update (l, r, cr, k * 2); else if (l> md) update (l, r, cr, k * 2 + 1); else {update (l, md, cr, k * 2); update (md, r, cr, k * 2 + 1) ;}} Int main () {int I, j, k, n; int x, y; char ch; while (~ Scanf ("% d", & n) {for (I = j = 0; I <n; I ++) {scanf ("% d % c ", & x, & y, & ch); c [I] = (ch = 'W'); // L-1 or r + 1 [) r [j] = j; a [j] = x; ++ j; r [j] = j; a [j] = y + 1; + + j;} sort (r, r + j, cmp); // deduplicated p [r [0] = k = 0; // a with 0th bits, row 0th bits for (I = 1; I <j; I ++) {if (a [r [I]! = A [r [I-1]) a [r [+ k] = a [r [I]; p [r [I] = k ;} // for (I = 0; I <j; I ++) cout <p [r [I] <""; cout <endl; // initialization tree build (0, k, 1); memset (f, 0, sizeof (f); // 0 black 1 white // update for (I = 0, n + = n; I <n; I + = 2) {x = p [I]; y = p [I + 1]; update (x, y, c [I/2], 1); // Interval Update} DownAll (1); // for (I = 0; I <= k; I ++) cout <f [I] <"; cout <endl; int tx, ty; for (I = x = y = 0; I <= k; I ++) {if (f [I] = 0) continue; tx = a [r [I]; while (f [I] = 1) I ++; ty = a [r [I]; if (ty-tx> y-x) {x = tx; y = ty ;}} if (x = y) puts ("Oh, my god"); else printf ("% d \ n", x, Y-1);} return 0 ;}