Pagini recente » Cod sursa (job #3359627) | Borderou de evaluare (job #1151831) | Borderou de evaluare (job #2050251) | Cod sursa (job #3359619) | Cod sursa (job #3359618)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("zeap.in");
ofstream fout("zeap.out");
set<int> S;
multiset<int> dif;
void insertValue(int x) {
if (S.find(x) != S.end())
return;
auto it = S.lower_bound(x);
if (it != S.end() && it != S.begin()) {
auto last = it;
--last;
dif.erase(dif.find(*it - *last));
}
if (it != S.end())
dif.insert(*it - x);
if (it != S.begin()) {
auto last = it;
--last;
dif.insert(x - *last);
}
S.insert(x);
}
void deleteValue(int x) {
auto it = S.find(x);
if (it == S.end()) {
fout << -1 << "\n";
return;
}
auto nextIt = it;
++nextIt;
if (it != S.begin()) {
auto last = it;
--last;
dif.erase(dif.find(x - *last));
if (nextIt != S.end())
dif.insert(*nextIt - *last);
}
if (nextIt != S.end())
dif.erase(dif.find(*nextIt - x));
S.erase(it);
}
int main() {
string op;
while (fin >> op) {
if (op == "I") {
int x;
fin >> x;
insertValue(x);
}
if (op == "S") {
int x;
fin >> x;
deleteValue(x);
}
if (op == "C") {
int x;
fin >> x;
fout << (S.find(x) != S.end()) << "\n";
}
if (op == "MAX") {
if (S.size() < 2) {
fout << -1 << "\n";
} else {
fout << *S.rbegin() - *S.begin() << "\n";
}
}
if (op == "MIN") {
if (S.size() < 2) {
fout << -1 << "\n";
} else {
fout << *dif.begin() << "\n";
}
}
}
return 0;
}