HDU 1890 stretch tree interval flip

Source: Internet
Author: User

 

It's still too weak. This dish, which is considered to be a question of stretching tree water, has been around for a long time...

N count

Each time you flip the number from position I to the position where the number I is big, the output is the position where the number I is big.

Nodes of the stretching tree do not need to record anything. They can directly create a tree in the order of each number in the array (that is, after the stretching tree is built, the result of the sequential traversal is the number of the original array)

Therefore, the relative size of the node number of the stretch tree represents the subscript of the number in the array.

Then, we will record the number of the node where the I-th largest number is located.

Finally, each time you rotate the number I to the rootSize of the Left subtreeThat is, the number of relative positions in the arrayLeftBecause every time I flip the number to the position I, this number will not be used,

Therefore, the root node is deleted directly after statistics are collected. (delete is also a little tricky. For details, seeCode), The answer isI + size [CH [root] [0]

Because there is no interval extraction operation, I have not added two other nodes, so I need to determine when n = 1

View code

# Include <cstdio># Include <Cstring> # Include <Algorithm> Using   Namespace  STD;  # Define Keytree (CH [CH [root] [1] [0]) Const   Int Maxn = 100010  ;  Struct  Node {  Int  Num, ID ;}  In [Maxn];  Int  Id [maxn];  Int  CMP (node A, Node B ){  If (A. num! = B. Num) Return A. Num < B. Num;  Return A. ID < B. ID ;}  Struct  Splaytree {  Int  Tot, root;  Int Pre [maxn];  Int  Size [maxn];  Int Ch [maxn] [ 2  ]; Inline  Void Rotate ( Int X, Int C ){ //  Rotate, c = 0 left, c = 1 right          Int Y = Pre [X]; Pushdown (y); Pushdown (x); ch [y] [ ! C] =Ch [x] [c];  If (CH [x] [c]) Pre [CH [x] [c] = Y; Pre [x] = Pre [y];  If (Pre [y]) CH [pre [y] [CH [pre [y] [ 1 ] = Y] = X; ch [x] [C] = Y; Pre [y] = X; pushup (y);} inline  Void Splay ( Int X, Int F ){//  Convert node X to node F.  Pushdown (X );  While (Pre [x]! = F ){  Int Y = pre [X], Z = Pre [y]; Pushdown (z); Pushdown (y); Pushdown (X );  //  The reverse mark must be removed before rotation.              If (Pre [pre [x] = F) {rotate (x, CH [pre [x] [  0 ] = X );} Else  {  If (CH [Z] [ 0 ] = Y ){  If (CH [y] [ 0 ] = X) rotate (Y,  1 ), Rotate (X, 1  );  Else  Rotate (X,  0 ), Rotate (X, 1  );}  Else  {  If (CH [y] [ 0 ] = X) rotate (X,  1 ), Rotate (X, 0  );  Else  Rotate (Y,  0 ), Rotate (X,0  ) ;}} Pushup (X );  If (F = 0 ) Root = X;} inline  Void Select ( Int K, Int F ){ //  Rotate the k point to the bottom of F.          Int X = Root;  While (1  ) {Pushdown (X );  If (K = size [CH [x] [ 0 ] + 1  )  Break  ;  If (K <= size [CH [x] [ 0  ]) X = CH [x] [ 0  ];  Else {K -= (Size [CH [x] [ 0 ] + 1  ); X = CH [x] [ 1  ] ;}} Splay (x, f) ;}inline  Void Del_root (){ //  Delete Root Node          Int T = Root;  If (CH [root] [ 1 ]) {Root = CH [root] [ 1  ]; Select (  1 , 0 ); //  Rotate the first vertex of the sequential traversal in the right subtree to the root (because the left son of this vertex must be empty) Ch [root] [ 0 ] = CH [T] [ 0 ]; //  Link the left subtree of the original root node to the left subtree of the current root node.              If (CH [T] [ 0 ]) Pre [CH [T] [0 ] = Root ;}  Else  Root = CH [root] [ 0  ]; Pre [root] = 0  ; Pushup (Root);} inline  Void Pushup ( Int  X) {size [x] = Size [CH [x] [ 0 ] + Size [CH [x] [ 1 ] +1  ;} Inline  Void Pushdown ( Int  X ){  If  (FLIP [x]) {flip [x] = 0  ; Flip [CH [x] [  0 ] ^ = 1  ; Flip [CH [x] [  1 ] ^ = 1  ; Swap (CH [x] [ 0 ], CH [x] [ 1  ]) ;}}  Void Newnode ( Int & X, Int  F) {x = ++ TOT; Pre [x] = F; ch [x] [  0 ] = CH [x] [ 1 ] = 0  ; Flip [x] = 0 ; Size [x] = 1  ;}  Void Build ( Int & X, Int L, Int R, Int  F ){  If (L> r) Return  ;  Int Mid = (L + r)> 1 ; Newnode (x, f); map [ID [Mid] = X; build (CH [x] [  0 ], L, mid- 1  , X); Build (CH [x] [  1 ], Mid + 1  , R, x); pushup (x );}  Void Init ( Int  N ){  Int  I; Pre [  0 ] = CH [0 ] [ 0 ] = CH [ 0 ] [ 1 ] = 0  ; Size [  0 ] = Flip [ 0 ] = Tot = 0  ;  For (I = 1 ; I <= N; I ++ ) {Scanf (  "  % D " ,& In  [I]. Num );  In [I]. ID = I;} Sort (  In + 1 , In + N + 1  , CMP );  For (I = 1 ; I <= N; I ++) ID [ In [I]. ID] = I; Map [ID [ 1 ] = 1  ; Map [ID [N] = 2  ; Newnode (root,  0  ); Newnode (CH [root] [  1  ], Root); Build (keytree,  2 , N- 1 , CH [root] [ 1  ]); Pushup (CH [root] [  1 ]); Pushup (Root );}  Void Solve ( Int  N ){  For ( Int I = 1 ; I <= N; I ++ ) {Splay (Map [I],  0  ); Printf (  "  % D  " , I + size [CH [root] [ 0 ]);  If (I <n) printf ( "   "  ); Flip [CH [root] [  0 ] ^ = 1  ; Del_root ();} puts (  ""  );}  Int  Map [maxn];  Int  Flip [maxn];} SPT;  Int Main (){  Int  N;  While (Scanf ( "  % D  " ,& N), n ){  If (N = 1 ) {Scanf ( "  % * D  " ); Printf ( "  1 \ n  " ); Continue  ;} SPT. INIT (n); SPT. Solve (n );}  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.