Cod sursa(job #2899462)

Utilizator ruxiiiIftimi Ruxandra ruxiii Data 8 mai 2022 20:32:35
Problema Zeap Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.04 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("zeap.in");
ofstream fout("zeap.out");

set <int> z;

priority_queue<pair <int, pair<int, int>>> dif;

int x;

string s;

void inserare(int x)
{
    z.insert(x);
    auto iterator = z.find(x);
    if(iterator != z.end())
    {
        auto before = iterator, after = iterator;
        before--;
        after++;
        dif.push({-abs(*iterator - *before), {*iterator, *before}});
        dif.push({-abs(*iterator - *after), {*iterator, *after}});
    }
}

void stergere(int x)
{
    auto iterator = z.find(x);
    if(z.find(x) == z.end())
        fout<<-1<<'\n';
    else
    {
        auto after = iterator;
        after ++;
        dif.push({-abs(*after - *iterator), {*after, *iterator}});
        z.erase(x);
    }

}

void minim()
{

    while (!dif.empty() && ((dif.top().first == 0) || (z.find(dif.top().second.first) == z.end() || z.find(dif.top().second.second) == z.end())))
       dif.pop();
    fout <<(-1) * dif.top().first << "\n";
}

int main()
{
    while(fin>>s)
    {
        if(s[0] == 'I')
        {
            int x = 0, i = 2;
            while(s[i])
                x = x * 10 + (s[i++] - '0');
            inserare(x);
        }
        if(s[0] == 'S')
        {
            int x = 0, i = 2;
            while(s[i])
                x = x * 10 + (s[i++] - '0');
            stergere(x);
        }
        if(s[0] == 'C')
        {
            int x = 0, i = 2;
            while(s[i])
                x = x * 10 + (s[i++] - '0');
            if(z.find(x) == z.end())
                fout<<0<<'\n';
            else
                fout<<1<<'\n';
        }
        if(s == "MIN")
        {
            if(z.size() < 2)
                fout<<-1<<'\n';
            else
                minim();
        }
        if(s == "MAX")
        {
            if(z.size() < 2)
                fout<<-1<<'\n';
            else
                fout<<*(--z.end()) - *(z.begin())<<'\n';
        }
    }
    return 0;
}