望周知

本人因看到 UU 码风后感到非常好看,决定以后也这样打代码,望周知

主要是可读性非常高,甚至放到百度翻译都能跑

#include<bits/stdc++.h>
#define firein(a) freopen( a".in", "r", stdin )
#define fireout(a) freopen( a".out", "w", stdout )
#define fire(a) firein(a),fireout(a)

typedef long long valueType ;

namespace IO{

    inline valueType Abs( valueType x){
        return (x < 0) ? (-x) : (x);
    }
    
    inline valueType read( ){
        valueType s = 0;bool f = 1; char ch = getchar(); 

        while( !isdigit(ch) ) {
            if( ch == '-' )
                f = 0;
            ch=getchar();
        } 

        while( isdigit(ch) ){
            s = (s << 3) + (s << 1) + ch - '0';
            ch = getchar();
        }

        return f ? s : -s; 
    }

    inline void write( valueType x ){
        if ( x >= 10 ) 
            write( x );

        putchar ( x%10 ) ; 
    }

    inline void print( valueType x , char s){
        if( x < 0 ) 
            putchar ('-');

        write ( Abs(x) );
        putchar (s);
    }
}using namespace IO;

class FHQ_Treap{

 private:

    static constexpr int MaxSize = 1e6 + 5;

 public:

    #define Left_Child(q) Tree[q].Left
    #define Right_Child(q) Tree[q].Right

    class Node{
     public:
        valueType Value, Priority, Left, Right, Size; 
    } Tree[MaxSize] ; 

    valueType Total, Root; 

    inline valueType Random() { 
        return rand() * rand() % INT_MAX;
    } 

    inline void Push_Up(valueType root) {
        if( root != 0 )
            Tree[root].Size = Tree[ Left_Child(root) ].Size + Tree[ Right_Child(root) ].Size + 1; 
    }
    
    inline valueType Insert(valueType value) {
        valueType root = ++Total;
        Tree[ root].Value = value;
        Tree[ root].Size = 1;
        Left_Child( root) = Right_Child( root) = 0;
        Tree[ root].Priority = Random();
        return root;
    }

    inline void Split(valueType root, valueType value, valueType &x, valueType &y) {
        if (root == 0) {
            x = y = 0;
            return;
        }

        if (!(value < Tree[root].Value)) { 
            x = root; 
            Split( Left_Child(root), value, Right_Child(root), y);
        } else {
            y = root;
            Split( Left_Child(root), value, x,  Left_Child(root));
        }

        Push_Up(root);
    }

    inline valueType Merge(valueType x, valueType y) {
        if (x == 0 || y == 0)  
            return x + y;	 

        if (Tree[x].Priority > Tree[y].Priority) {
            Left_Child(x) = Merge(Left_Child(x), y);
            Push_Up(x);
            return x;
        } 
        
        else {
            Left_Child(y) = Merge(x, Left_Child(y));
            Push_Up(y);
            return y;
        }
    }

    inline void insert(valueType value) {
        valueType x, y;
        Split(Root, value - 1, x, y);
        Root = Merge( Merge( x, Insert( value ) ), y );
    }

    inline void Delete(valueType value) {
        valueType x, y, z;

        Split(Root, value, x, z);
        Split(x, value - 1, x, y);
        
        if (y)
            y = Merge( Left_Child(y), Right_Child(y));

        Root = Merge( Merge( x, y), z);
    }

    inline valueType Rank(valueType value) {
        valueType x, y, Ans;

        Split(Root, value - 1, x, y);
        Ans = Tree[x].Size + 1;
        Root = Merge(x, y);

        return Ans;
    }

    inline valueType Key(valueType rank) {
        valueType x, y, z;

        Split(Root, rank - 1, x, y);
        Split(y, 1, y, z);

        valueType Ans = Tree[y].Value;
        Root = Merge(Merge(x, y), z);
        
        return Ans;
    }

    inline valueType Prev(valueType value) {
        valueType x, y, root, Ans;

        Split(Root, value - 1, x, y);
        root = x;

        

        while (Tree[root].Right) 
            root = Tree[root].Right;

        Ans = Tree[root].Value;
        Root = Merge( x, y);

        return Ans;
    }

    inline valueType Next(valueType key) {
        valueType x, y, root, Ans;

        Split(Root, key, x, y);
        root = y;

        while (Tree[root].Left) 
            root = Tree[root].Left;

        Ans = Tree[root].Value;
        Root = Merge( x, y);

        return Ans;
    }

    #undef Left_Child
    #undef Right_Child
}T;

namespace solve{
    #define for_(a, b, c) for( int a = b ; a <= c ; ++a )
    #define _for(a, b, c) for( int a = b ; a >= c ; --a )

    inline void In(){
        valueType n = read( );

        for_(i , 1 , n){
            int opt = read( );
            valueType x = read(); 

            std::cerr << i ;
            if( opt == 1 )
                T.insert ( x ); 
            
            if( opt == 2 )
                T.Delete ( x );

            if( opt == 3 )
                print ( T.Rank ( x ) , '\n' );
            
            if( opt == 4)
                print ( T.Key ( x ) , '\n' );

            if( opt == 5 ){
                T.insert( x );
                print( T.Prev( x ) , '\n' );
                T.Delete( x );
            }

            if( opt == 6){
                T.insert( x );
                print( T.Next( x ) , '\n' );
                T.Delete( x );
            }
        }
    }

    #undef for_
    #undef _for
}using namespace solve;

signed main() {
    fire( "data" );

    In( );

    return 0 ; 
    
}
posted @ 2024-04-16 20:01  Vsinger_洛天依  阅读(18)  评论(4编辑  收藏  举报