Spoj 4487 can you answer these queries vi

Source: Internet
Author: User

Spoj_4487

In fact, this question is similar to gss1, but due to the addition and deletion operations, we can't solve it with the line segment tree. Therefore, we can maintain a splay to implement these operations.

But what I wrote at the beginningProgramIt's always TLE, but compared with some online AC programs, we found that there are only two very different points. One is that they use struct, and the other is that they use pointers. When I was puzzled, I used several arrays to write a struct like an AC program, as shown below:

StructSplay {IntLeft, right, pre, size, sum, MC, key, LC, RC;# DefineLeft (x) SP [X]. Left# DefineRight (x) SP [X]. Right# DefinePre (x) SP [X]. Pre# DefineSize (x) SP [X]. Size# DefineSum (x) SP [X]. Sum# DefineMC (x) SP [X]. MC# DefineKey (x) SP [X]. Key# DefineLC (x) SP [X]. LC# DefineRC (x) SP [X]. RC} SP [maxd];

Nima ...... Actually ac ......

I don't know if this is an underlying optimization ...... Colleagues from TLE may wish to try writing a struct ......

# Include <stdio. h> # Include < String . H> # Include <Cstdlib> # Define Maxd 200010 # Define INF 0x3f3f3f Int  N, t, a [maxd], node;  Struct  Splay {  Int  Left, right, pre, size, sum, MC, key, LC, RC; # Define Left (x) SP [X]. Left # Define Right (x) SP [X]. Right # Define Pre (x) SP [X]. Pre # Define Size (x) SP [X]. Size # Define Sum (x) SP [X]. Sum # Define MC (x) SP [X]. MC # Define Key (x) SP [X]. Key # Define LC (x) SP [X]. LC # Define RC (x) SP [X]. RC } SP [maxd];  Int Max (Int X, Int  Y ){  Return X> Y? X: Y ;}  Void Newnode ( Int & Cur, Int  V) {cur = ++ Node; size (cur) = 1  ; Key (cur) = Sum (cur) = MC (cur) = Lc (cur) = RC (cur) = V; left (cur) = Right (cur) =0  ;}  Void Update ( Int  Cur ){  Int Ls = left (cur), RS = Right (cur); size (cur) = Size (LS) + size (RS) + 1  ; Sum (cur) = Sum (LS) + sum (RS) + Key (cur); MC (cur) = Max (RC (LS ), 0 ) + Key (cur) + max (LC (RS ), 0  ); MC (cur) =Max (MC (cur), max (MC (LS), MC (RS); LC (cur) = Max (LC (LS), sum (LS) + key (cur) + max (LC (RS ), 0  ); RC (cur) = Max (RC (RS), sum (RS) + key (cur) + max (RC (LS ), 0  ));}  Void Leftrotate ( Int  Cur ){  Int K = right (cur), fa = Pre (cur); Right (cur) = Left (k); Pre (right (cur )) = Cur; left (k) = Cur; Pre (cur) = K; Pre (k) = Fa; right (FA) = Cur? Right (FA) = K: Left (FA) = K; Update (cur );}  Void Rightrotate ( Int  Cur ){  Int K = left (cur), fa = Pre (cur); left (cur) = Right (k); Pre (left (cur )) = Cur; right (k) = Cur; Pre (cur) = K; Pre (k) = Fa; right (FA) = Cur? Right (FA) = K: Left (FA) = K; Update (cur );}  Void Build ( Int & Cur, Int X, Int Y, Int  FA ){  Int Mid = (x + y)> 1  ; Newnode (cur, a [Mid]); Pre (cur) =Fa;  If (X = Y)  Return  ;  If (X < Mid) Build (left (cur), X, mid - 1  , Cur );  If (Mid < Y) Build (right (cur), mid + 1  , Y, cur); Update (cur );}  Void Splay (Int X, Int  Goal ){  Int  Y, Z;  For  (;;){  If (Y = pre (x) = Goal)  Break  ;  If (Z = pre (y) = Goal) Right (y) = X? Leftrotate (y): rightrotate (y ); Else  {  If (Right (z) = Y) Right (y) = X? (Leftrotate (z), leftrotate (y): (rightrotate (Y), leftrotate (z ));  Else  Left (y) = X? (Rightrotate (z), rightrotate (y): (leftrotate (Y), rightrotate (z ));}}  If (Goal = 0  ) T = X; Update (x );} Void Rotateto ( Int K, Int  Goal ){  Int Cur = T, N;  For  (;) {N = Size (left (cur) + 1  ;  If (N = K)  Break  ;  If (K < N) cur = Left (cur );  Else  K -= N, cur = Right (cur);} splay (cur, goal );}  Void  Init (){  Int  I;  For (I = 1 ; I <= N; I ++ ) Scanf (  "  % D " ,& A [I]); Node = Size ( 0 ) = Sum ( 0 ) = 0  ; MC (  0 ) = Lc ( 0 ) = RC ( 0 ) =- INF; newnode (t,  0 ), Newnode (right (t ), 0  ); Pre (right (t )) = T; Key (t) = Key (right (t) = 0  ;  If  (N) Build (left (right (t )),  1  , N, right (t); Update (right (t), update (t );}  Void Delete ( Int  X) {rotateto (X,  0 ), Rotateto (x + 2  , T); left (right (t )) = 0  ; Update (right (t), update (t );} Void Insert ( Int X, Int  Y) {rotateto (X,  0 ), Rotateto (x + 1  , T); newnode (left (right (t), Y); Pre (node) = Right (t); Update (right (t), update (t );}  Void Replace ( Int X, Int  Y) {rotateto (x + 1 ,0  ); Key (t) = Y; Update (t );}  Void Query ( Int X, Int  Y) {rotateto (X,  0 ), Rotateto (Y + 2  , T); printf (  "  % D \ n  "  , MC (left (right (t ))));}  Void Solve (){  Int  I, Q, X, Y;  Char OP [ 5  ]; Scanf (  "  % D  " ,& Q );  For (I = 0 ; I <q; I ++ ) {Scanf (  "  % S  " , OP );  If (OP [ 0 ] = '  D  '  ) {Scanf (  "  % D  " ,& X); Delete (x );}  Else  {Scanf (  "  % D " , & X ,& Y );  If (OP [ 0 ] = '  I  '  ) Insert (x, y );  Else   If (OP [ 0 ] = '  R  '  ) Replace (x, y ); Else  Query (x, y );}}}  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.