Ural 1019 Line Painting

Source: Internet
Author: User

You can use a line segment tree to perform interval dyeing and merging.

Because the data range is relatively small, it is also possible to directly update the data by force after discretization.

# Include <stdio. h> # Include < String . H> # Include <Stdlib. h> # Define Maxd 10010 Int N, LC [ 4 * Maxd], RC [ 4 * Maxd], MC [ 4 * Maxd], [ 4 *Maxd];  Int  M, TX [maxd];  Struct  SEG {  Int  X, Y;  Char B [ 5  ];} Seg [maxd];  Int CMP ( Const   Void * _ P, Const   Void * _ Q ){ Int * P = ( Int *) _ P, * q = ( Int * ) _ Q;  Return * P <* Q? - 1 : 1  ;}  Void Build ( Int Cur, Int X, Int  Y ){  Int Mid = (x + y)>1 , Ls = cur < 1 , RS = (cur < 1 ) | 1  ; LC [cur] = RC [cur] = mc [cur] = TX [Y + 1 ]- TX [X]; To [cur] =- 1  ;  If (X = Y)  Return  ; Build (LS, X, mid); Build (RS, mid +1  , Y );}  Void  Init (){  Int  I, J, K;  For (I = 0 ; I <n; I ++ ) {Scanf (  "  % D % s  " , & Seg [I]. X ,& SEG [I]. Y, SEG [I]. B); TX [I < 1 ] = Seg [I]. X, TX [(I <1 ) | 1 ] = SEG [I]. Y;} TX [I < 1 ] = 0 , TX [(I < 1 ) | 1 ] = 1000000000  ; Qsort (TX, (n + 1 ) < 1 , Sizeof (TX [ 0 ]), CMP); m = 0  ;  For (I = 1 ; I <(n + 1 ) < 1 ); I ++ )  If (TX [I]! = TX [I- 1  ]) TX [ ++ M] = TX [I]; build (  1 , 0 , M-1  );}  Void Pushdown ( Int Cur, Int X, Int  Y ){  Int Mid = (x + y)> 1 , Ls = cur < 1 , RS = (cur < 1 ) | 1  ;  If (To [cur]! =-1  ) {To [ls] = To [RS] = To [cur]; Mc [ls] = Lc [ls] = RC [ls] = (to [cur]? 0 : TX [Mid + 1 ]- TX [x]); Mc [RS] = Lc [RS] = RC [RS] = (to [cur]? 0 : TX [Y + 1 ]-TX [Mid + 1  ]); To [cur] =- 1  ;}} Int Max ( Int X, Int  Y ){  Return X> Y? X: Y ;}  Void Update ( Int Cur, Int X, Int  Y ){  Int Mid = (x + y)> 1 , Ls = cur < 1 , RS = (cur < 1 ) | 1  ; Mc [cur] = Max (MC [ls], MC [RS]); Mc [cur] = Max (MC [cur], RC [ls] + LC [RS]); LC [cur] = Lc [ls] + (LC [ls] = TX [Mid + 1 ]-TX [x]? LC [RS]: 0  ); RC [cur] = RC [RS] + (RC [RS] = TX [Y + 1 ]-TX [Mid + 1 ]? RC [ls]: 0  );} Int BS ( Int  X ){  Int Mid, min = 0 , Max = m + 1  ;  For  (;) {Mid = (Min + max)> 1  ;  If (Mid = Min)  Break  ; If (TX [Mid] <= X) min = Mid;  Else  Max = Mid ;}  Return  Mid ;}  Void Color ( Int Cur, Int X, Int Y, Int S, Int T,Int  C ){  Int Mid = (x + y)> 1 , Ls = cur < 1 , RS = (cur < 1 ) | 1  ;  If (X> = S & Y <= T) {to [cur] = C; Mc [cur] = Lc [cur] = RC [cur] = (C? 0 : TX [Y + 1 ]- TX [x]);  Return  ;} Pushdown (cur, x, y );  If (Mid> = S) color (LS, X, mid, S, T, C );  If (Mid + 1 <= T) color (RS, mid + 1  , Y, S, T, C); Update (cur, x, y );}  Void Search ( Int Cur, Int X, Int Y, Int & S, Int & T ){  Int Mid = (x + y)> 1 , Ls = cur < 1 , RS = (cur < 1 ) | 1  ;  If (X = Y) {s = TX [X], t = TX [Y + 1 ];  Return  ;} Pushdown (cur, x, y );  If (MC [ls] = MC [cur]) Search (LS, X, mid, S, T );  Else   If (RC [ls] + Lc [RS] = MC [cur]) S = TX [Mid + 1 ]-RC [ls], t = TX [Mid + 1 ] + LC [RS];  Else  Search (RS, mid +1  , Y, S, T );}  Void  Solve (){  Int  I, J, K;  For (I = 0 ; I <n; I ++ ) {J = BS (SEG [I]. X), k = BS (SEG [I]. y );  If (J < K ){  If (SEG [I]. B [ 0 ] ='  B  '  ) Color (  1 , 0 , M- 1 , J, k- 1 , 1  );  Else  Color (  1 , 0 , M- 1 , J, k-1 , 0  ) ;}} Search (  1 , 0 , M- 1  , J, k); printf (  "  % D \ n  "  , J, k );}  Int  Main (){  While (Scanf ( "  % D " , & N) = 1  ) {Init (); solve ();}  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.