Cod sursa(job #2534922)

Utilizator sichetpaulSichet Paul sichetpaul Data 31 ianuarie 2020 09:20:51
Problema Zeap Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.52 kb
#include <bits/stdc++.h>

using namespace std;

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

set<int> z;
multiset<int> s;
string cuv;

void in(int x) {
    z.insert(x);
    bool ok1 = 1, ok2 = 1;
    auto it1 = z.lower_bound(x), it2 = z.upper_bound(x);
    if (it1 == z.begin()) ok1 = 0;
    else it1--;
    if (it2 == z.end()) ok2 = 0;

    if (ok1) s.insert(x - *it1);
    if (ok2) s.insert(*it2 - x);
    if (ok1 && ok2) s.erase(s.find(*it2 - *it1));
}
void out(int x) {
    bool ok1 = 1, ok2 = 1;
    auto it1 = z.lower_bound(x), it2 = z.upper_bound(x);
    if (it1 == z.begin()) ok1 = 0;
    else it1--;
    if (it2 == z.end()) ok2 = 0;

    if (ok1) s.erase(s.find(x - *it1));
    if (ok2) s.erase(s.find(*it2 - x));
    if (ok1 && ok2) s.insert(*it2 - *it1);
    z.erase(z.find(x));
}
int main()
{
    while (f >> cuv) {
        if (cuv == "MAX" || cuv == "MIN") {
            if (z.size() < 2) g << -1 << '\n';
            else {
                if (cuv == "MAX") {
                    auto it = z.end();
                    --it;
                    g << *it - *z.begin() << '\n';
                }
               else g << *s.begin() << '\n';
            }
        }

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

    return 0;
}