Cod sursa(job #2534071)

Utilizator sichetpaulSichet Paul sichetpaul Data 30 ianuarie 2020 08:25:47
Problema Zeap Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.36 kb
#include <bits/stdc++.h>

using namespace std;

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

set<int> z, zp;
multiset<int> s;
string cuv;
void in(int x) {
    z.insert(x);
    zp.insert(-x);
    bool ok = 1;

    auto it2 = z.upper_bound(x), it1 = zp.upper_bound(-x);
    int x1 = -1 * (*it1), x2 = *it2;
    if (x1 < x) s.insert(x - x1);
    else ok = 0;

    if (x2 > x) s.insert(x2 - x);
    else ok = 0;

    if (ok) s.erase(x2 - x1);
}
void out(int x) {
    bool ok = 1;
    auto it2 = z.upper_bound(x), it1 = zp.upper_bound(-x);
    int x1 = -1 * (*it1), x2 = *it2;

    if (x1 < x) s.erase(x - x1);
    else ok = 0;
    if (x < x2) s.erase(x2 - x);
    else ok = 0;

    if (ok) s.insert(x2 - x1);
    z.erase(x);
    zp.erase(-x);
}
int main()
{
    while (f >> cuv) {
        if (cuv == "MAX" || cuv == "MIN") {
            if (z.size() < 2) g << -1 << '\n';
            else {
                if (cuv == "MAX") g << *z.crbegin() - *z.begin() << '\n';
                else g << *s.begin() << '\n';
            }
        }

        else {
            int x;
            f >> x;
            if (cuv == "C") g << z.count(x) << '\n';
            else if (cuv == "I") in(x);
            else {
                 if (z.count(x)) out(x);
                 else g << -1 << '\n';
            }
        }
    }

    return 0;
}