Uestc_1425
For more information about the line segment tree related to interval merging, refer to Hu Hao's blog: http://www.notonlysuccess.com/index.php/segment-tree-complete /.
This question is very similar to hdu_3308, but we only need to deal with the interval addition operations. In order to facilitate modification and obtain the size relationship between the right and left endpoints of the Left range, you can use two tags lx [] and Rx [] to indicate the values of the left and right endpoints of the current range.
# Include <stdio. h> # Include < String . H> # Define Maxd 100010 Int N, Q, a [maxd], LC [ 4 * Maxd], RC [ 4 * Maxd], LX [ 4 * Maxd], RX [ 4 * Maxd], MC [ 4 * Maxd], add [ 4 * Maxd]; Int Getmax ( 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] = Getmax (MC [ls], MC [RS]); LC [cur] = Lc [ls], RC [cur] = RC [RS]; If (RX [ls] < Lx [RS]) { If (RC [ls] + Lc [RS]> MC [cur]) MC [cur] = RC [ls] + LC [RS]; If (LC [ls] = mid-x + 1 ) LC [cur] + = LC [RS]; If (RC [RS] = y- Mid) RC [cur] + = RC [ls];} lx [cur] = Lx [ls], RX [cur] = RX [RS];} Void Pushdown (Int Cur ){ Int Ls = cur < 1 , RS = (cur < 1 ) | 1 ; If (Add [cur]) {Add [ls] + = Add [cur], add [RS] + = Add [cur]; lx [ls] + = Add [cur], RX [ls] + = Add [cur]; lx [RS] + = Add [cur], RX [RS] + = Add [cur]; add [cur] =0 ;}} Void Build ( Int Cur, Int X, Int Y ){ Int Mid = (x + y)> 1 , Ls = cur < 1 , RS = (cur < 1 ) | 1 ; Add [cur] = 0 ; If (X = Y) {Mc [cur] = Lc [cur] = RC [cur] = 1 ; Lx [cur] = RX [cur] = A [x]; Return ;} Build (LS, X, mid); Build (RS, mid + 1 , Y); Update (cur, x, y );} Void Init (){ Int I, J, K; scanf ( " % D " , & N ,& Q ); For (I = 1 ; I <= N; I ++ ) Scanf ( " % D " ,& A [I]); Build ( 1 , 1 , N );} Int Query (Int Cur, Int X, Int Y, Int S, Int T, Int Fa, Int & Ans ){ Int Mid = (x + y)> 1 , Ls = cur < 1 , RS = (cur < 1 ) | 1 ; If (X> = S & Y <= T ){ If (MC [cur]> Ans) ans = MC [cur]; Return Fa =- 1 ? LC [cur]: RC [cur];} Pushdown (cur ); If (Mid> = T) Return Query (LS, X, mid, S, T ,- 1 , ANS ); Else If (Mid + 1 <= S) Return Query (RS, Mid + 1 , Y, S, T, 1 , ANS ); Else { Int Ln = query (LS, X, mid, S, T, 1 , ANS), Rn = query (RS, Mid + 1 , Y, S, T ,-1 , ANS ); If (RX [ls] < Lx [RS]) { If (LN + rn> Ans) ans = Ln + Rn; If (Fa =- 1 ) Return LC [ls] = mid-x + 1 ? LC [ls] + Rn: LC [ls]; Else Return RC [RS] = Y-mid? RC [RS] + LN: RC [RS];} Return Fa =- 1 ? LC [ls]: RC [RS] ;}} Void Refresh ( Int Cur, Int X, Int Y, Int S, Int T, Int V ){ Int Mid = (x + y)> 1 , Ls = cur < 1 , RS = (cur < 1 ) | 1 ; If (X> = S & Y <= T) {Add [cur] + = V, LX [cur] + = V, RX [cur] + = V; Return ;} Pushdown (cur ); If (Mid> =S) Refresh (LS, X, mid, S, T, V ); If (Mid + 1 <= T) Refresh (RS, mid + 1 , Y, S, T, V); Update (cur, x, y );} Void Solve (){ Int I, J, K, X, Y, Z, ans; Char B [ 5 ]; For (I =0 ; I <q; I ++ ) {Scanf ( " % S " , B ); If (B [ 0 ] = ' Q ' ) {Scanf ( " % D " , & X ,&Y); ans = 0 ; Query ( 1 , 1 , N, x, y ,- 1 , ANS); printf ( " % D \ n " , ANS );} Else {Scanf ( " % D " , & X, & Y ,& Z); refresh ( 1 , 1 , N, x, y, z );}}} Int Main (){ Int T, TT; scanf ( " % D " ,& T ); For (Tt = 0 ; TT <t; TT ++ ) {Init (); printf ( " Case # % d: \ n " , Tt + 1 ); Solve ();} Return 0 ;}