[Review] balanced tree splay

#include<iostream>
#include<cstdio>
#include<cstring>
#define read(a) a=init()
using namespace std;
struct node{
    long long fa,ch[2],data,size,cnt;
}t[10000003];
long long n,root=0,tot=0,lei,thi;
inline long long init()//快读
{
    long long a=0,b=1;char ch=getchar();
    while(ch>'9'||ch<'0'){if(ch=='-')b=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){a=(a<<3)+(a<<1)+(ch-'0');ch=getchar();}
    return a*b;Long{
    basic rotation: Turn X//X)LongLongRotate (void
Inline
} 
 Long Y = T [x] .fa, z T = [Y] .fa, K T = [Y] .ch [ . 1 ] == x; // Y is the parent node of x, z is an ancestor of node x,
                                                 // K y of x is stored in which a son 
    T [Z] .ch [T [Z] .ch [ . 1 ] == y] = x; 
    T [x] .fa = Z; // progenitor parent: I become my son grandfather, grandfather I call my father tube 
    T [Y] .ch [K] T = [X] .ch [K ^ . 1 ]; 
    T [T [X] .ch [K ^ . 1 ]]. = the y-FA; // child in the Sun: my adoptive son to my father, my father shouted to my dad son 
    t [the X-] .ch [k ^ 1 ] = the y-; 
    t [the y-] .fa = the X-; // parent as a child: my father became my son, I called my dad pipe dad 
    t [the y-] .size = t [t [the y-] .ch [ 0 ]] size + t [t [the y-] .ch [. 1.]] size T + [Y] .cnt; // update the node size
                                         // size stored in the root node of the subtree size (including itself) 
    T [X] .size T = [T [X]. CH [ 0 .]] + size T [T [x] .ch [ . 1 ]] T + size [x] .cnt;. // Since this time is a child node x y, so that after the first x y 
} 
inline void the splay ( Long  Long x, Long  Long goal) // the splay operation: rotate the child node x goal of 
{
     the while (T [x] = .fa goal!) // parent node is not the destination node if x 
    {
         Long  Long Y T = [X] .fa, Z = T [Y] .fa; // remove the parent and ancestor nodes 
        IF (Z = Goal!) //If the ancestor node is still not the target node, it means we need to rotate at least twice 
            (t [z] .ch [ 0 ] == the y-) ^ (t [the y-] .ch [ 0 ] == the X-)? Rotate (the X- ): Rotate (Y);
                     // to disrupt a balance of the tree originally present chain in order to prevent the card, if the x, y, z in the same line, first turn Y 
        Rotate (X); // whether prior to transfection the x or y did not turn or is, we need a sub x 
    }
     IF (Goal!) x = the root; // update root 
} 
inline void iNSERT ( Long  Long x) // insert: inserting a number x, that the tree remains balanced equilibrium 
{
     Long  Long U = the root, fa = 0 ; // start the search from the root node, remember to update fa x storage node information fa 
    the while (U T && [U] x = .data!) / /u u still exists and the current node is not equal to the target node, look down iteration. 
    { 
        FA = U; 
        U = T [U] .ch [X> T [U] .data]; // X less than the current node information, look to the left, or to the right look. (Using a balanced tree itself properties) 
    }
     IF (U) T [U] .cnt ++; // If the node itself is present, a number of the node plus 
    the else  // Otherwise, a new node (individual information to be newly) 
    { 
        U = ++ TOT;
         IF (FA) T [FA] .ch [X> T [FA] .data] = U; 
        T [U] .ch [ 0 ] = T [U] .ch [ . 1 ] = 0 ; 
        T [TOT] .fa = FA; 
        T [TOT] .data = X; 
        T [TOT] .cnt =. 1 ; 
        T [TOT] .size = . 1 ; 
    } 
    The splay (U, 0 ); 
} 
inline void Find ( Long  Long x) // Find: Locate the element x and rotate it root 
{
     Long  Long U = the root; // U is the current node, starting from the root search. 
    IF (! U) return ; // if not the root, that is, no trees: the query is invalid. 
    the while (T [U] .ch [X> T [U] .data] X &&! = T [U] .data) // iteration continues down to see X 
        U = T [U] .ch [X> T [U ] .data]; 
    The splay (U, 0 ); // turn up 
} 
inlineLong  Long NXT ( Long  Long x, Long  Long Jud) // query: Query predecessor or successor 
{                                     // (Jud query precursor is represented by 0, jud time is a subsequent query. 1) 
    Find (x); // find x and he (or the point and its closest) to the root 
    Long  Long U = the root;
     IF (T [U] .data> X && Jud) return U;
     IF (T [U] .data <X Jud &&!) return U; // two words for the query itself does not balance x tree 
    U = T [U] .ch [Jud]; // if the root node has been rotated, its precursor rightmost left subtree son, rear-wheel drive is its right subtree leftmost son. 
    the while (T [U] .ch [Jud ^ . 1 ]) T U = [U] .ch [^ Jud. 1 ];
     return U; 
} 
inline void DELET ( Long  Long X) // delete: to delete a node 
{
     Long  Long pre NXT = (X, 0 );
     Long  Long NT = NXT (X, . 1 ); // predecessor and successor, respectively, and store the query 
    the splay (pre, 0 ), the splay (NT, pre); // the precursor transformed into the root, the subsequent transfer into sub-root node.
        // analysis can be obtained at this time is a right son pre nt, x is an x and a left son nt no subtree 
    Long  Long RES = T [nt] .ch [ 0 ];
     IF (T [RES] .cnt> . 1 )  
    {
        t [res] .cnt-; // if more than one node, a delete 
        The splay (RES, 0 ); // and the rotation of the node to the root 
    }
     the else T [NT] .ch [ 0 ] = 0 ; // otherwise directly it away (to null the left son nt) 
} 
inline Long  Long k_th ( Long  Long X) // k-th largest operations: query inserted node k-th largest numbers 
{
     Long  Long U = the root; // temporary node U 
    IF (T [U] .size <x) return  0 ; // if the size of the point is less than x, i.e. there can be a large number of x, returns 0 
    the while ( . 1 int )
    {
         Y = T [U] .ch [ 0 ]; // removed easily check the size of the left son, 
        IF (X> T [Y] .size + T [U] .cnt) // if the number is greater than the left son of the query scale 
        { 
            X - T = [Y] .size + T [U] .cnt; // right to find the first query, i.e., k is a right large numbers sons XT [Y] .size + T [U] .cnt 
            U T = [U] .ch [ . 1 ]; 
        } 
        the else  
            IF (T [Y] .size> = X) U = Y; // if the son query 
            the else  return T [U] .data; //
     } 
} 
int main ( ) 
{ 
    Read (n-); 
    INSERT ( 0x7FFFFFFF ); 
    INSERT ( - 0x7FFFFFFF );
    while(n--)
    {
        read(lei),read(thi);
        switch(lei)
        {
            case 1:{
                insert(thi);
                break;
            }
            case 2:{
                delet(thi);
                break;
            }
            case 3:{
                find(thi);
                printf("%lld\n",t[t[root].ch[0]].size);
                break;
            }
            case 4:{
                printf("%lld\n",k_th(thi+1));
                break;
            }
            case 5:{
                printf("%lld\n",t[nxt(thi,0)].data);
                break;
            }
            case 6:{
                printf("%lld\n",t[nxt(thi,1)].data);
                break;
            }
        }
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/xingmi-weiyouni/p/11140926.html