Cod sursa(job #2750064)

Utilizator realmeabefirhuja petru realmeabefir Data 9 mai 2021 19:40:31
Problema Zeap Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.51 kb
#include <iostream>
#include <bits/stdc++.h>
#include <fstream>
#include <cmath>

using namespace std;

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

set<int> s;
priority_queue<pair<int,pair<int,int>>> pq;

int main()
{
    int x;
    string instr;
    while (f >> instr)
    {
        if (instr == "I")
        {
            f >> x;
            s.insert(x);

            if (s.size() > 1)
            {
                auto x_it = s.find(x);
                if (x_it == s.begin())
                {
                    pq.push({-abs(*x_it-*next(x_it)), {*x_it, *next(x_it)}});
                }
                else if (x_it == prev(s.end()))
                {
                    pq.push({-abs(*x_it-*prev(x_it)), {*x_it, *prev(x_it)}});
                }
                else
                {
                    pq.push({-abs(*x_it-*next(x_it)), {*x_it, *next(x_it)}});
                    pq.push({-abs(*x_it-*prev(x_it)), {*x_it, *prev(x_it)}});
                }
            }
        }
        else if (instr == "S")
        {
            f >> x;
            auto x_it = s.find(x);
            if (x_it == s.end())
                g << -1 << '\n';
            else
            {
                if (x_it != s.begin() && x_it != prev(s.end()))
                {
                    auto pr = prev(x_it);
                    auto nx = next(x_it);
                    pq.push({-abs(*pr-*nx), {*pr, *nx}});
                }
                s.erase(x);
            }
        }
        else if (instr == "C")
        {
            f >> x;
            if (s.find(x) != s.end())
                g << 1 << '\n';
            else
                g << 0 << '\n';
        }
        else if (instr == "MAX")
        {
            if (s.size() < 2)
                g << -1 << '\n';
            else
                g << *(s.rbegin())-(*(s.begin())) << '\n';
        }
        else if (instr == "MIN")
        {
            if (s.size() < 2)
                g << -1 << '\n';
            else
            {
                pair<int,pair<int,int>> first_queue = pq.top();
                while (pq.size() && (s.find(first_queue.second.first) == s.end() || s.find(first_queue.second.second) == s.end() ))
                {
                    pq.pop();
                    if (pq.size())
                        first_queue = pq.top();
                }
                if (pq.size())
                    g << -pq.top().first << '\n';
            }
        }
    }
    return 0;
}