Cod sursa(job #3233477)

Utilizator BuzdiBuzdugan Rares Andrei Buzdi Data 3 iunie 2024 15:30:53
Problema Zeap Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.23 kb
#include <fstream>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <cassert>
#include <set>
#include <map>
#include <cstdlib>
#include <ctime>
#define ll long long

using namespace std;

ifstream cin("zeap.in");
ofstream cout("zeap.out");

string op;
set<int> s;
multiset<int> diff;

bool Search(int x) {
    auto it = s.lower_bound(x);
    if (it == s.end() || *it != x) {
        return 0;
    }
    return 1;
}

int Delete(int x) {
    if (!Search(x)) {
        return 0;
    }

    auto it = s.lower_bound(x);
    if (it == s.begin()) {
        it++;
        if (it != s.end()) {
            diff.erase(diff.find(*it - x));
        }
    }
    else {
        auto left = prev(it);
        diff.erase(diff.find(x - *left));
        it++;
        if (it != s.end()) {
            diff.erase(diff.find(*it - x));
            diff.insert(*it - *left);
        }
    }
    s.erase(x);
    return 1;
}

void Insert(int x) {
    if (Search(x)) {
        return;
    }

    auto it = s.lower_bound(x);
    if (it == s.end()) {
        if (s.size() > 0) {
            diff.insert(x - *prev(it));
        }
    }
    else if (it == s.begin()) {
        diff.insert(*s.begin() - x);
    }
    else {
        diff.erase(diff.find(*it - *prev(it)));
        diff.insert(*it - x);
        diff.insert(x - *prev(it));
    }
    s.insert(x);
}

int MaxDiff() {
    auto it = s.end();
    it--;
    return *it - *s.begin();
}

int MinDiff() {
    return *diff.begin();
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    while (cin >> op) {
        if (op == "I") {
            int x;
            cin >> x;
            Insert(x);
        }
        else if (op == "S") {
            int x;
            cin >> x;
            bool deleted = Delete(x);
            if (!deleted) {
                cout << -1 << '\n';
            }
        }
        else if (op == "C") {
            int x;
            cin >> x;
            cout << Search(x) << '\n';
        }
        else if (op == "MAX") {
            cout << MaxDiff() << '\n';
        }
        else {
            cout << MinDiff() << '\n';
        }
    }

    return 0;
}