Cod sursa(job #2898472)

Utilizator SteanfaDiaconu Stefan Steanfa Data 6 mai 2022 21:54:40
Problema Zeap Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.22 kb
#include <fstream>
#include <iostream>
#include <algorithm>
#include <set>
#include <queue>
using namespace std;
set<long long> v;

priority_queue<pair<int, pair<long long, long long>>> heap;

int main()
{
    ifstream cin("zeap.in");
    ofstream cout("zeap.out");
    // ifstream cin("dec.py");
    string c;
    int n, k;
    while (cin >> c)
    {

        if ("I" == c)
        {
            cin >> n;
            v.insert(n);
            auto c = v.find(n);
            if (c != v.end())
            {
                auto minn = c;
                auto mm = c;
                minn--;
                mm++;
                heap.push({-abs(*c - *(minn)), {*c, *(minn)}});
                heap.push({-abs(*c - *(mm)), {*c, *(mm)}});
            }
        }
        else if ("S" == c)
        {

            cin >> n;
            auto c = v.find(n);
            if (c != v.end())
            {
                c++;
                v.erase(n);
                auto minn = c;
                minn--;
                heap.push({-abs(*c - *(minn)), {*c, *(minn)}});
            }
            else
            {
                cout << -1 << '\n';
            }
        }
        else if ("C" == c)
        {
            cin >> n;
            if (v.find(n) != v.end())
            {
                cout << 1 << '\n';
            }
            else
            {
                cout << 0 << '\n';
            }
        }
        else if ("MIN" == c)
        {
            // cout << " --- " << -heap.top().first << endl;
            if (v.size() > 1)
            {
                while (!heap.empty() and ((heap.top().first == 0) or (v.find(heap.top().second.first) == v.end() or v.find(heap.top().second.second) == v.end())))
                {
                    heap.pop();
                }

                // cout << " ---x " << -heap.top().first << ' ' << heap.top().second.first << ' ' << heap.top().second.second << endl;
                cout << -heap.top().first << endl;
            }
            else
                cout << -1 << '\n';
        }
        else
        {
            if (v.size() > 1)
            {
                cout << *(--v.end()) - *v.begin() << '\n';
            }
            else
                cout << -1 << '\n';
        }
    }
}