Cod sursa(job #2899138)

Utilizator iioaaana777Ghergu Ioana iioaaana777 Data 8 mai 2022 00:34:01
Problema Zeap Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.39 kb
#include <fstream>
#include <math.h>
#include <set>
#include <queue>
using namespace std;
ifstream fin("zeap.in");
ofstream fout("zeap.out");

int main()
{
    string op;
    set<int> zeap;
    priority_queue<pair<int, pair<int, int>>> dif;

    while(fin>>op)
    {
        int x;

        if(op[0] == 'I')
        {
            fin>>x;
            if(zeap.find(x) == zeap.end())
                {
                    zeap.insert(x);
                    auto it = zeap.find(x);

                    if(it != zeap.begin())
                    {
                        auto p = it;
                        it--;
                        int d = abs(*it - *p);
                        dif.push(make_pair(-d, make_pair(*it, *p)));
                    }

                    if(it != zeap.end())
                    {
                        auto p = it;
                        it++;
                        int d = abs(*p - *it);
                        dif.push(make_pair(-d, make_pair(*it, *p)));
                    }
                }

        }
        else

        if(op[0] == 'S')
        {
            fin>>x;
            if(zeap.find(x) == zeap.end())
                fout<<"-1\n";

            auto it = zeap.find(x);
            if(it != zeap.begin()  && it != --zeap.end())
            {
                auto p = it;
                auto u = it;
                p--;
                u++;
                dif.push(make_pair(-abs(*u - *p), make_pair(*p, *u)));
            }

            zeap.erase(x);
        }
        else

        if(op[0] == 'C')
        {
            fin>>x;
            if(zeap.find(x) != zeap.end())
                fout<<"1\n";
            else
                fout<<"0\n";
        }
        else

        if(op == "MAX")
        {
            if(zeap.size() <= 1)
                fout<<"-1\n";
            else
            {
                fout<< *(--zeap.end()) - *(zeap.begin())<<"\n";
            }

        }
        else

        if(op == "MIN")
        {
            if(zeap.size() <= 1)
                fout<<"-1\n";
            else
            {
                while(zeap.find(dif.top().second.first) == zeap.end() || zeap.find(dif.top().second.second) == zeap.end())
                    dif.pop();

                fout<< -dif.top().first<<"\n";
            }

        }

    }

    return 0;
}