Cod sursa(job #2035616)

Utilizator LucaSeriSeritan Luca LucaSeri Data 9 octombrie 2017 18:04:01
Problema Zeap Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 4.78 kb
#include <bits/stdc++.h>

using namespace std;

const int MOD = 1e9 + 9;
const int MAX = 2e9;

ifstream f("zeap.in");
ofstream g("zeap.out");

class Treapuri{
    public:
    struct treap{
        treap*st, *dr;
        int val, g;
        int minim, maxim;
        int mindif;
        treap(treap* stanga, treap* dreapta, int v, int gr, int minn, int maxx, int minndif){
            this->st = stanga;
            this->dr = dreapta;
            this->val = v;
            this->g = gr;
            this->minim = minn;
            this->maxim = maxx;
            this->mindif = minndif;
        }
    };

    treap *NILL, *root;
    int random(){
        return (rand()<<15 + rand())%MOD + 1;
    }
    void Start(){
        NILL = new treap(NULL, NULL, 0, 0, 0, 0, MAX);
        root = NILL;
    }

    int maxdiff(){
        if(root->maxim != root->minim) return root->maxim - root->minim;
        return -1;
    }
    int mindiff(){
        if(root->mindif != MAX) return root->mindif;
        return -1;
    }
    void recheck(treap*& node){
        if(node == NILL) return;
        node->minim = node->val;
        node->maxim = node->val;
        node->mindif = MAX;
        if(node -> st != NILL){
            node->minim = min(node->minim, node->st->minim);
            node->maxim = max(node->maxim, node->st->maxim);
            node->mindif = min(node->mindif, node->val-node->st->maxim);
            node->mindif = min(node->mindif, node->st->mindif);
        }
        if(node -> dr != NILL){
            node->minim = min(node->minim, node->dr->minim);
            node->maxim = max(node->maxim, node->dr->maxim);
            node->mindif = min(node->mindif, node->dr->minim - node->val);
            node->mindif = min(node->mindif, node->dr->mindif);
        }
        return;
    }
    void rotateLeft(treap *& node){
        treap* aux;
        aux = node->st;
        node->st = aux->dr;
        aux->dr = node;
        node = aux;
        recheck(node->st);
        recheck(node->dr);
        recheck(node);
    }

    void rotateRight(treap *& node){
        treap* aux;
        aux = node->dr;
        node->dr = aux->st;
        aux->st = node;
        node = aux;
        recheck(node->st);
        recheck(node->dr);
        recheck(node);
    }

    void balance(treap *& node){
        recheck(node);
        if(node->st != NILL && node->st->g > node->g) rotateLeft(node);
        if(node->dr != NILL && node->dr->g > node->g) rotateRight(node);
    }

    void add(treap *& node, int valoare){
        if(node == NILL){
            node = new treap(NILL, NILL, valoare, random(), valoare, valoare, MAX);
            return;
        }

        if(valoare > node->val) add(node->dr, valoare);
        else add(node->st, valoare);
        balance(node);
    }

    bool findd(treap *& node, int valoare){
        if(node == NILL) return 0;
        if(node->val == valoare) return 1;
        if(valoare > node->val) return findd(node->dr, valoare);
        else return findd(node->st, valoare);
    }

    void errase(treap *&node, int valoare){
        if(node == NILL) return;
        if(node->val == valoare){
            if(node->st == NILL && node->dr == NILL){
                delete(node);
                node = NILL;
                return;
            }

            if(node->st != NILL && node->dr != NILL){
                if(node->st->g > node->dr->g){
                    rotateLeft(node);
                    errase(node->dr, valoare);
                }
                else{
                    rotateRight(node);
                    errase(node->st, valoare);
                }
            }
            if(node->st != NILL){
                rotateLeft(node);
                errase(node->dr, valoare);
            }
            if(node->dr != NILL){
                rotateRight(node);
                errase(node->st, valoare);
            }
        }


    if(valoare > node->val) errase(node->dr, valoare);
    else errase(node->st, valoare);

    balance(node);
    }
}*Tr;

int main(){
    char c;
    Tr = new Treapuri;
    Tr->Start();
    while((c = f.get()) && c!= EOF){
        int x;
        if(c == 'I'){
            f >> x;
            if(!Tr->findd(Tr->root, x)) Tr->add(Tr->root, x);
        }
        if(c == 'S'){
            f >> x;
            if(Tr->findd(Tr->root, x)) Tr->errase(Tr->root, x);
            else g << "-1\n";
        }

        if(c == 'C'){
            f >> x;
            g << Tr->findd(Tr->root, x) << '\n';
        }
        if(c == 'M'){
            c = f.get();
            if(c == 'A') g << Tr->maxdiff() << '\n';
            else g << Tr->mindiff() << '\n';
            c = f.get();
        }
        f.get();
    }
    return 0;
}