Cod sursa(job #2899321)

Utilizator Petrica81Simion Petrica Petrica81 Data 8 mai 2022 15:16:56
Problema Zeap Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.89 kb
#include <iostream>
#include <fstream>
#include <set>
#include <string>
#include <queue>
using namespace std;
ifstream f ("zeap.in");
ofstream g ("zeap.out");
set<int> arbore;
string aux;
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> dif;
void inserare(int x){
    if(arbore.find(x) == arbore.end()){
        arbore.insert(x);
        auto it = arbore.find(x);
        auto st = it;
        auto dr = it;
        dr++;
        st--;
        if(dr != arbore.end()){
            dif.push(make_pair(*dr - *it, *it));
        }
        if(it != arbore.begin()){
            dif.push(make_pair(*it - *st, *st));
        }
    }
}
void stergere(int x){
    if(arbore.find(x) != arbore.end()){
        auto it = arbore.find(x);
        auto st = it;
        auto dr = it;
        dr++;
        st--;
        if(it != arbore.begin() && dr != arbore.end()){
            dif.push(make_pair(*dr - *st, *st));
        }
        arbore.erase(it);
    }
    else g<<"-1\n";
}
void MAX() {
    if (arbore.size() >= 2) {
        auto final = arbore.end();
        final--;
        g << *final - *arbore.begin() << '\n';
    }
    else
        g << "-1\n";
}
void MIN() {
    if (arbore.size() >= 2) {
        while(!dif.empty() && (arbore.find(dif.top().second) == arbore.end() || arbore.find(dif.top().second+dif.top().first) == arbore.end())) {
            dif.pop();
        }
        g << dif.top().first << '\n';
    }
    else {
        g << "-1\n";
    }
}
int main() {
    int x;
    while(!f.eof()){
        f>>aux;
        if(aux == "S"){
            f>>x;
            stergere(x);
        }
        else if(aux == "C"){
            f>>x;
            if (arbore.find(x) != arbore.end()) g << "1\n";
            else g << "0\n";
        }
        else if(aux == "I"){
            f>>x;
            inserare(x);
        }
        else if(aux == "MAX")MAX();
        else if(aux == "MIN")MIN();
    }
    return 0;
}