Cod sursa(job #3359618)

Utilizator rares89_Dumitriu Rares rares89_ Data 1 iulie 2026 01:09:12
Problema Zeap Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.86 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("zeap.in");
ofstream fout("zeap.out");

set<int> S;
multiset<int> dif;

void insertValue(int x) {
    if (S.find(x) != S.end())
        return;

    auto it = S.lower_bound(x);

    if (it != S.end() && it != S.begin()) {
        auto last = it;
        --last;

        dif.erase(dif.find(*it - *last));
    }

    if (it != S.end())
        dif.insert(*it - x);

    if (it != S.begin()) {
        auto last = it;
        --last;

        dif.insert(x - *last);
    }

    S.insert(x);
}

void deleteValue(int x) {
    auto it = S.find(x);

    if (it == S.end()) {
        fout << -1 << "\n";
        return;
    }

    auto nextIt = it;
    ++nextIt;

    if (it != S.begin()) {
        auto last = it;
        --last;

        dif.erase(dif.find(x - *last));

        if (nextIt != S.end())
            dif.insert(*nextIt - *last);
    }

    if (nextIt != S.end())
        dif.erase(dif.find(*nextIt - x));

    S.erase(it);
}

int main() {
    string op;

    while (fin >> op) {
        if (op == "I") {
            int x;
            fin >> x;

            insertValue(x);
        }

        if (op == "S") {
            int x;
            fin >> x;

            deleteValue(x);
        }

        if (op == "C") {
            int x;
            fin >> x;

            fout << (S.find(x) != S.end()) << "\n";
        }

        if (op == "MAX") {
            if (S.size() < 2) {
                fout << -1 << "\n";
            } else {
                fout << *S.rbegin() - *S.begin() << "\n";
            }
        }

        if (op == "MIN") {
            if (S.size() < 2) {
                fout << -1 << "\n";
            } else {
                fout << *dif.begin() << "\n";
            }
        }
    }

    return 0;
}