Cod sursa(job #2066960)

Utilizator robx12lnLinca Robert robx12ln Data 15 noiembrie 2017 18:49:54
Problema Zeap Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.91 kb
#include<fstream>
#include<stdlib.h>
#include<cstring>
#include<time.h>
using namespace std;
ifstream fin("zeap.in");
ofstream fout("zeap.out");
const int INF = 2 * (0x3f3f3f3f);
struct treap{
    int val, prt, maxim, minim, difmin;
    treap *st, *dr;
    treap( int val, int prt, int maxim, int minim, int difmin, treap *st, treap *dr ){
        this->val = val;
        this->prt = prt;
        this->maxim = maxim;
        this->minim = minim;
        this->difmin = difmin;
        this->st = st;
        this->dr = dr;
    }
} *rad, *NILL;
int n, nodes, ok;
char s[100];
inline int get_number( int i ){
    int r = 0;
    while( '0' <= s[i] && s[i] <= '9' ){
        r = r * 10 + ( s[i] - '0' );
        i++;
    }
    return r;
}
inline void upd_inf( treap * &nod ){
    nod->maxim = max( max( nod->st->maxim, nod->dr->maxim ), nod->val );
    nod->minim = min( min( nod->st->minim, nod->dr->minim ), nod->val );
    nod->difmin = min( min( nod->st->difmin, nod->dr->difmin ), min( nod->val - nod->st->maxim, nod->dr->minim - nod->val ) );
}
inline void rotate_to_left( treap * &nod ){
    treap * aux = nod->dr;
    nod->dr = aux->st; aux->st = nod;
    nod = aux;
}
inline void rotate_to_right( treap * &nod ){
    treap * aux = nod->st;
    nod->st = aux->dr; aux->dr = nod;
    nod = aux;
}
inline void balance( treap * &nod ){
    if( nod->st != NILL && nod->st->prt > nod->prt )
        rotate_to_right( nod );
    else
        if( nod->dr != NILL && nod->dr->prt > nod->prt )
            rotate_to_left( nod );
}
void Insert( treap * &nod, int key, int priority ){
    if( nod == NILL )
        nod = new treap( key, priority, -INF, INF, INF, NILL, NILL ), nodes++;
    else
        if( key < nod->val )
            Insert( nod->st, key, priority );
        else
            if( key > nod->val )
                Insert( nod->dr, key, priority );
    upd_inf( nod );
    balance( nod );
    upd_inf( nod );
}
void Erase( treap * &nod, int key ){
    if( nod == NILL )
        return;
    if( key < nod->val ){
        Erase( nod->st, key );
    }else{
        if( key > nod->val )
            Erase( nod->dr, key );
        else{
            ok = 1;
            if( nod->st == NILL && nod->dr == NILL ){
                delete nod;
                nod = NILL;
            }else{
                if( nod->st->prt < nod->dr->prt )
                    rotate_to_left( nod );
                else
                    rotate_to_right( nod );
                Erase( nod, key );
            }
        }
    }
    if( nod != NILL )
        upd_inf( nod );
}
int Search( treap *nod, int key ){
    if( nod == NILL )
        return 0;
    if( key < nod->val )
        return Search( nod->st, key );
    else
        if( key > nod->val )
            return Search( nod->dr, key );
        else
            return 1;
}
int main(){
    srand( time(0) );
    rad = NILL = new treap( 0, 0, -INF, INF, INF, NULL, NULL );
    nodes = 0;
    while( fin.get( s, 100 ) ){
        if( s[0] == 'M' && s[1] == 'I' && s[2] == 'N' ){
            if( nodes >= 2 )
                fout << rad->difmin << "\n";
            else
                fout << "-1\n";
            fin.get();
            continue;
        }
        if( s[0] == 'M' && s[1] == 'A' && s[2] == 'X' ){
            if( nodes >= 2 )
                fout << rad->maxim - rad->minim << "\n";
            else
                fout << "-1\n";
            fin.get();
            continue;
        }
        int nr = get_number( 2 );
        if( s[0] == 'I' && Search( rad, nr ) == 0 )
            Insert( rad, nr, rand() + 1 );
        if( s[0] == 'S' ){
            ok = 0;
            Erase( rad, nr );
            nodes -= ok;
            if( ok == 0 )
                fout << "-1\n";
        }
        if( s[0] == 'C' )
            fout << Search( rad, nr ) << "\n";
        fin.get();
    }
    return 0;
}