Cod sursa(job #2899242)

Utilizator LucaMihaiLM10Luca Ilie LucaMihaiLM10 Data 8 mai 2022 12:49:28
Problema Zeap Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 4.42 kb
#include <bits/stdc++.h>

#define MULT (1 << 30)
#define PUTIN (-MULT)

using namespace std;

struct TREAP {
    struct treap {
        int key;
        int priority;
        int size, maxKey, minKey, minDif;

        treap* left;
        treap* right;
    };

    treap* root = NULL;

    void dinamica( treap* t ) {
        t->size = 1;
        t->maxKey = t->minKey = t->key;
        t->minDif = MULT;
        if ( t->left != NULL ) {
            t->size += t->left->size;
            t->maxKey = max( t->maxKey, t->left->maxKey );
            t->minKey = min( t->minKey, t->left->minKey );
            t->minDif = min( t->minDif, t->key - t->left->maxKey );
        }
        if ( t->right != NULL ) {
            t->size += t->right->size;
            t->maxKey = max( t->maxKey, t->right->maxKey );
            t->minKey = min( t->minKey, t->right->minKey );
            t->minDif = min( t->minDif, t->right->minKey - t->key );
        }
    }

    pair <treap*, treap*> split( treap* t, int key ) {
        if ( t == NULL )
            return { NULL, NULL };

        if ( key < root->key ) {
            auto p = split( t->left, key );
            t->left = p.second;
            return { p.first, t };
        } else {
            auto p = split( t->right, key );
            t->right = p.first;
            return { t, p.second };
        }
    }

    treap* insert( treap *t, treap* newT ) {
        if ( t == NULL )
            return newT;

        if ( newT->priority > t->priority ) {
            auto p = split( t, newT->key );
            newT->left = p.first;
            newT->right = p.second;

            dinamica( newT );

            return newT;
        }

        if ( newT->key < t->key )
            t->left = insert( t->left, newT );
        else
            t->right = insert( t->right, newT );
        dinamica( t );
        return t;
    }

    void insert( int key ) {
        root = insert( root, new treap{ key, rand(), 1, key, key, MULT } );
    }

    treap* merge( treap *t1, treap* t2 ) {
        if ( t1 == NULL )
            return t2;
        if ( t2 == NULL )
            return t1;

        if ( t1->priority > t2->priority ) {
            t1->right = merge( t1->right, t2 );
            dinamica( t1 );
            return t1;
        } else {
            t2->left = merge( t1, t2->left );
            dinamica( t2 );
            return t2;
        }
    }

    treap* erase( treap* t, int key ) {
        if ( t == NULL )
            return t;

        if ( key == t->key )
            return merge( t->left, t->right );

        if ( key < t->key )
            t->left = erase( t->left, key );
        else
            t->right = erase( t->right, key );
        dinamica( t );
        return t;
    }

    void erase( int key ) {
        root = erase( root, key );
    }

    bool searchKey( treap* t, int key ) {
        if ( t == NULL )
            return false;

        if ( key == t->key )
            return true;

        if ( key < t->key )
            return searchKey( t->left, key );
        else
            return searchKey( t->right, key );
    }

    bool searchKey( int key ) {
        return searchKey( root, key );
    }


    int minKey() {
        return root->minKey;
    }


    int maxKey() {
        return root->maxKey;
    }

    int size() {
        if ( root == NULL )
            return 0;
        return root->size;
    }

    int minDif() {
        return root->minDif;
    }
};

TREAP zeap;

int main() {
    ifstream cin( "zeap.in" );
    ofstream cout( "zeap.out" );

    int x;
    string s;

    while ( cin >> s ) {
        if ( s == "I" ) {
            cin >> x;
            if ( !zeap.searchKey( x ) )
                zeap.insert( x );
        } else if ( s == "S" ) {
            cin >> x;
            if ( zeap.searchKey( x ) )
                zeap.erase( x );
            else
                cout << "-1\n";
        } else if ( s == "C" ) {
            cin >> x;
            cout << zeap.searchKey( x ) << "\n";
        } else {
            if ( zeap.size() < 2 )
                cout << "-1\n";
            else {
                if ( s == "MAX" )
                    cout << zeap.maxKey() - zeap.minKey() << "\n";
                else if ( s == "MIN" )
                    cout << zeap.minDif() << "\n";
            }
        }
    }

    return 0;
}