Cod sursa(job #3375449)

Utilizator Mihai_OctMihai Octavian Mihai_Oct Data 11 noiembrie 2026 23:59:43
Problema Zeap Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 4.57 kb
#include <bits/stdc++.h>

using namespace std;

#define USE_STD_IO 0
#if USE_STD_IO
    #define fin cin
    #define fout cout
#else
    ifstream fin("zeap.in");
    ofstream fout("zeap.out");
#endif // USE_STD_IO

#define RAND (rand() % 666013)

namespace Utils {
    template<typename Tip>
    void Swap(Tip& a, Tip& b) {
        if(a == b) return;
        a ^= b;
        b ^= a;
        a ^= b;
    }

    template<typename Tip>
    Tip Min(const Tip a, const Tip b) {
        return (a < b ? a : b);
    }

    template<typename Tip>
    Tip Max(const Tip a, const Tip b) {
        return (a > b ? a : b);
    }
}
using namespace Utils;

struct Treap {
    struct NodTreap {
        int pri, st, dr, card;
        int val, mi, ma;
        int miDiff;

        NodTreap() : mi(INT_MAX) {}
        explicit NodTreap(int _val) :
            val(_val), mi(_val), ma(_val), miDiff(INT_MAX),
            pri(RAND), st(0), dr(0), card(1) {}
    };

    NodTreap treap[100002];
    int idxNodTreap = 0;
    int rad = 0;

    Treap() {}

    int Calc(const int nod) {
        if(0 == nod) return 0;
        NodTreap& nt = treap[nod];

        nt.card = 1;

        nt.mi = nt.val;
        nt.ma = nt.val;

        nt.miDiff = INT_MAX;

        if(0 != nt.st) {
            nt.card += treap[nt.st].card;

            nt.mi = Min(nt.mi, treap[nt.st].mi);
            nt.ma = Max(nt.ma, treap[nt.st].ma);

            nt.miDiff = Min(nt.miDiff, treap[nt.st].miDiff);
            nt.miDiff = Min(nt.miDiff, nt.val - treap[nt.st].ma);
        }
        if(0 != nt.dr) {
            nt.card += treap[nt.dr].card;

            nt.mi = Min(nt.mi, treap[nt.dr].mi);
            nt.ma = Max(nt.ma, treap[nt.dr].ma);

            nt.miDiff = Min(nt.miDiff, treap[nt.dr].miDiff);
            nt.miDiff = Min(nt.miDiff, treap[nt.dr].mi - nt.val);
        }

        return nod;
    }

    pair<int, int> Split(const int nod, const int val) {
        if(0 == nod) return {0, 0};

        Calc(nod);
        if(treap[nod].val < val) {
            auto ret = Split(treap[nod].dr, val);
            treap[nod].dr = 0;
            Calc(nod);

            return {Merge(nod, ret.first), ret.second};
        }
        else {
            auto ret = Split(treap[nod].st, val);
            treap[nod].st = 0;
            Calc(nod);

            return {ret.first, Merge(ret.second, nod)};
        }
    }

    int Merge(int st, int dr) {
        if(0 == st) return Calc(dr);
        if(0 == dr) return Calc(st);

        //Calc(st);
        //Calc(dr);
        if(treap[st].val > treap[dr].val) Swap(st, dr);
        if(treap[st].pri < treap[dr].pri) {
            treap[st].dr = Merge(treap[st].dr, dr);
            return Calc(st);
        }
        else {
            treap[dr].st = Merge(st, treap[dr].st);
            return Calc(dr);
        }
    }

    void Add(int& nod, const int val) {
        treap[++idxNodTreap] = NodTreap(val);

        auto [inc, sfar] = Split(nod, val);

        nod = Merge(Merge(inc, idxNodTreap), sfar);
        //Calc(nod);
    }

    void Del(int& nod, const int val) {
        auto [inc, rest] = Split(nod, val);
        auto [el , sfar] = Split(rest, 1 + val);
        nod = Merge(inc, sfar);
        //Calc(nod);
    }

    bool Cauta(const int nod, const int val) {
        if(0 == nod) return false;
        if(val == treap[nod].val) return true;
        if(val <  treap[nod].val)     return Cauta(treap[nod].st, val);
        /*if(val >  treap[nod].val)*/ return Cauta(treap[nod].dr, val);
    }

    int MaxDiff() {
        return treap[rad].ma - treap[rad].mi;
    }

    int MinDiff() {
        return treap[rad].miDiff;
    }
};

Treap treap;
string op;
int x;

int main() {
    #if USE_STD_IO
        ios_base::sync_with_stdio(false);
    #endif // USE_STD_IO
    fin.tie(NULL);
    fout.tie(NULL);

    srand(time(0));

    while(fin >> op) {
        if("I" == op) {
            fin >> x;
            if(false == treap.Cauta(treap.rad, x)) {
                treap.Add(treap.rad, x);
            }
        }
        else if("S" == op) {
            fin >> x;
            if(treap.Cauta(treap.rad, x)) {
                treap.Del(treap.rad, x);
            }
            else fout << "-1\n";
        }
        else if("C" == op) {
            fin >> x;
            fout << treap.Cauta(treap.rad, x) << '\n';
        }
        else if("MIN" == op) {
            if(treap.treap[treap.rad].card < 2) fout << "-1\n";
            else fout << treap.MinDiff() << '\n';
        }
        else /*if("MAX" == op)*/ {
            if(treap.treap[treap.rad].card < 2) fout << "-1\n";
            else fout << treap.MaxDiff() << '\n';
        }
    }

    return 0;
}