Pagini recente » Cod sursa (job #3030831) | Cod sursa (job #1391056) | Cod sursa (job #277345) | Cod sursa (job #2137299) | Cod sursa (job #2899294)
#include <iostream>
#include <fstream>
#include <set>
#include <string>
#include <queue>
#include <tuple>
using namespace std;
ifstream f ("zeap.in");
ofstream g ("zeap.out");
set<int> arbore;
string aux;
priority_queue<tuple<int,int,int>, vector<tuple<int,int,int>>, greater<tuple<int,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_tuple(*dr - *it, *it, *dr));
}
if(it != arbore.begin()){
dif.push(make_tuple(*it - *st, *st, *it));
}
}
}
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_tuple(*dr - *st, *st, *dr));
}
arbore.erase(it);
}
else g<<"-1\n";
}
void MAX() {
if (arbore.size() >= 2) {
auto final = arbore.end();
auto start = arbore.begin();
final--;
g << *final - *start << '\n';
}
else
g << "-1\n";
}
void MIN() {
if (arbore.size() >= 2) {
while(arbore.find(get<2>(dif.top())) == arbore.end() || arbore.find(get<1>(dif.top())) == arbore.end()) {
dif.pop();
}
g << get<0>(dif.top()) << '\n';
}
else {
g << "-1\n";
}
}
int main() {
int x;
while(!f.eof()){
f>>aux;
if(aux == "MAX")MAX();
else if(aux == "MIN")MIN();
else 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);
}
}
return 0;
}