Cod sursa(job #3135580)

Utilizator Sumurduc_TeodoraSumurduc Teodora Sumurduc_Teodora Data 3 iunie 2023 18:27:29
Problema Zeap Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.94 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("zeap.in");
ofstream g("zeap.out");

set<int> Z;
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> Q;
void Insert(int x)
{
    if (Z.find(x) == Z.end())
    {
        Z.insert(x);
        if (Z.size() >= 2)
        {
            auto it = Z.find(x);
            if (it != Z.begin())
            {
                it--;
                Q.push({abs(x - *it), {*it, x}});
            }
            it = Z.find(x);
            if (it != Z.end())
            {
                it++;
                Q.push({abs(*it - x), {*it, x}});
            }
        }
    }
}

void Delete(int x)
{
    if (Z.find(x) == Z.end())
        g << "-1"<<endl;
    else
    {
        auto it = Z.find(x), right = it;
        right++;
        if (it == Z.begin() || right == Z.end())
            Z.erase(x);
        else
        {
            auto left = it;
            left--;
            if (right != Z.end() && it != Z.begin())
                Q.push({abs(*right - *left), {*left, *right}});
            Z.erase(x);
        }
    }
}

void Search(int x)
{
    g <<Z.count(x)<< '\n';
}

void MIN()
{
    if (Z.size() < 2)
        g << -1 << '\n';
    else
    {
        while (Z.find(Q.top().second.first) == Z.end() || Z.find(Q.top().second.second) == Z.end())
            Q.pop();
        g << Q.top().first << '\n';
    }
}

void MAX()
{
    if (Z.size() < 2)
        g << -1 << '\n';
    else
    {
        auto left = Z.begin(), right = Z.end();
        right--;
        g << *right - *left << '\n';
    }
}

int main() {
    string s;
    int x;
    while (f >> s) {
        if (s == "I") {
            f >> x;
            Insert(x);
        } else if (s == "S") {
            f >> x;
            Delete(x);
        } else if (s == "C") {
            f >> x;
            Search(x);
        } else if (s == "MIN")
            MIN();
        else
            MAX();
    }
    return 0;
}