Cod sursa(job #2534474)

Utilizator sichetpaulSichet Paul sichetpaul Data 30 ianuarie 2020 16:58:44
Problema Zeap Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.48 kb
#include <bits/stdc++.h>

using namespace std;

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

set<int> z, zp;
multiset<int> s;
std::set<int>::iterator it1, it2;
string cuv;
void in(int x) {
    z.insert(x);
    zp.insert(-x);

    it2 = z.upper_bound(x), it1 = zp.upper_bound(-x);
    int x1 = -1 * (*it1), x2 = *it2;

    if (x1 < x && zp.count(-x1)) s.insert(x - x1);
    if (x2 > x && z.count(x2)) s.insert(x2 - x);
    if (x1 < x && x < x2) if (zp.count(-x1) && z.count(x2))
        s.erase(x2 - x1);
}
void out(int x) {
    it2 = z.upper_bound(x), it1 = zp.upper_bound(-x);
    int x1 = -1 * (*it1), x2 = *it2;

    if (x1 < x && zp.count(-x1)) s.erase(x - x1);
    if (x < x2 && z.count(x2)) s.erase(x2 - x);
    if (x1 < x && x < x2) if (zp.count(-x1) && z.count(x2))
         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.rbegin() - *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" && !z.count(x)) in(x);
            else if (cuv == "S") {
                 if (z.count(x)) out(x);
                 else g << -1 << '\n';
            }
        }
    }

    return 0;
}