Cod sursa(job #2741041)

Utilizator MateiAruxandeiMateiStefan MateiAruxandei Data 15 aprilie 2021 13:22:27
Problema Zeap Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.49 kb
#include <bits/stdc++.h>
#pragma GCC optimize("O3")

#define nozerous(x) (x & -x)
#define MOD 1000000007
using namespace std;

const int INF = (1 << 30), NMAX(30005);
using VI  = vector<int>;
using VVI = vector<VI>;
using VB  = vector<bool>;
using Point = array<int, 2>;

void BUNA(const string& task = "")
{
    if (!task.empty())
        freopen((task + ".in").c_str(), "r", stdin),
                freopen((task + ".out").c_str(), "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}
void PA()
{
    exit(0);
}

auto cmp = [&](pair<int, int> &a, pair<int, int> &b){return abs(a.first - a.second) < (b.first - b.second);};
set<int> s;
priority_queue<pair<int, int>, vector<pair<int, int> >, decltype(cmp) > pq(cmp);

int main()
{
    BUNA("zeap");
    string tip;
    while(cin >> tip){
        int x;
        if(tip == "I"){
            cin >> x;
            if(s.find(x) == s.end()){
                s.insert(x);
                auto pz = s.find(x);
                if(pz != s.begin())
                {
                    --pz;
                    pq.push({*pz, x});
                    ++pz;
                }
                if(pz != s.end()){
                    ++pz;
                    if(pz != s.end()){
                        pq.push({x, *pz});
                    }
                }
            }
        }
        else if(tip == "S"){
            cin >> x;
            if(s.find(x) != s.end()){
                auto it = s.find(x);
                auto it2 = it;
                ++it2;
                if(it != s.begin() && it2 != s.end()){
                    --it;
                    pq.push({*it, *it2});
                }
                s.erase(x);
            }
            else cout << -1 << '\n';
        }
        else if(tip == "C"){
            cin >> x;
            cout << (s.find(x) != s.end()) << '\n';
        }
        else if(tip == "MAX"){
            if(s.size() < 2)
                cout << -1 << '\n';
            else {
                auto it = s.end();
                --it;
                cout << *it - *s.begin() << '\n';
            }
        }
        else {
            if(s.size() < 2)
                cout << -1 << '\n';
            else{
                while(s.find(pq.top().first) == s.end() || s.find(pq.top().second) == s.end())
                    pq.pop();
                cout << abs(pq.top().first - pq.top().second) << '\n';
            }
        }
    }
    PA();
}