Cod sursa(job #2899367)

Utilizator deboradeleanuDebora Deleanu deboradeleanu Data 8 mai 2022 16:45:17
Problema Zeap Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.81 kb
#include <bits/stdc++.h>
using namespace std;
ifstream in("zeap.in");
ofstream out("zeap.out");
priority_queue<pair<int,pair<int,int>>, vector<pair<int,pair<int,int>>>, greater<pair<int,pair<int,int>>> > pq;
set<int> s;
int x;
string op;
char c;

void inserare()
{
    in >> x;
    s.insert(x);
    set<int>::iterator it = s.find(x);
    if(it != s.begin())
    {
        --it;
        pq.push({x-*it,{*it, x}});
        ++it;
    }
    if(++it != s.end())
    {
        pq.push({*it -x,{*it, x}});
    }
}

void stergere()
{
    in >> x;
    if(s.find(x) != s.end())
    {
        set<int>::iterator it = s.find(x);
        set<int>::iterator it1 = ++it;
        --it;
        set<int>::iterator it2 = --it;
        //it++;
        pq.push({*it1 - *it2, {*it1, *it2}});
        s.erase(x);
    }
    else
        out << -1 << '\n';
}

void cautare()
{
    in >> x;
    if(s.find(x) == s.end())
        out << 0 << '\n';
    else
        out << 1 << '\n';
}

void minii()
{
    if(s.size() <= 1)
        out << -1 << '\n';
    else
    {
        while(!pq.empty() && (s.find(pq.top().second.first) == s.end() || s.find(pq.top().second.second) == s.end()))
        {
            pq.pop();
        }
        out << pq.top().first << '\n';
    }
}

void maxii()
{
    if(s.size() <= 1)
        out << -1 << '\n';
    else
        out << *(--s.end()) - *(s.begin()) << '\n';
}

int main()
{
    while(in >> op)
    {
        c = op[0];
        if(c == 'I')
            inserare();
        if(c == 'S')
            stergere();
        if(c == 'C')
            cautare();
        if(c == 'M')
        {
            c = op[1];
            if(c == 'I')
                minii();
            if(c == 'A')
                maxii();
        }
    }
    return 0;
}