Pagini recente » Cod sursa (job #1658116) | Cod sursa (job #269649) | Cod sursa (job #844276) | Cod sursa (job #63758) | Cod sursa (job #3233477)
#include <fstream>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <cassert>
#include <set>
#include <map>
#include <cstdlib>
#include <ctime>
#define ll long long
using namespace std;
ifstream cin("zeap.in");
ofstream cout("zeap.out");
string op;
set<int> s;
multiset<int> diff;
bool Search(int x) {
auto it = s.lower_bound(x);
if (it == s.end() || *it != x) {
return 0;
}
return 1;
}
int Delete(int x) {
if (!Search(x)) {
return 0;
}
auto it = s.lower_bound(x);
if (it == s.begin()) {
it++;
if (it != s.end()) {
diff.erase(diff.find(*it - x));
}
}
else {
auto left = prev(it);
diff.erase(diff.find(x - *left));
it++;
if (it != s.end()) {
diff.erase(diff.find(*it - x));
diff.insert(*it - *left);
}
}
s.erase(x);
return 1;
}
void Insert(int x) {
if (Search(x)) {
return;
}
auto it = s.lower_bound(x);
if (it == s.end()) {
if (s.size() > 0) {
diff.insert(x - *prev(it));
}
}
else if (it == s.begin()) {
diff.insert(*s.begin() - x);
}
else {
diff.erase(diff.find(*it - *prev(it)));
diff.insert(*it - x);
diff.insert(x - *prev(it));
}
s.insert(x);
}
int MaxDiff() {
auto it = s.end();
it--;
return *it - *s.begin();
}
int MinDiff() {
return *diff.begin();
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
while (cin >> op) {
if (op == "I") {
int x;
cin >> x;
Insert(x);
}
else if (op == "S") {
int x;
cin >> x;
bool deleted = Delete(x);
if (!deleted) {
cout << -1 << '\n';
}
}
else if (op == "C") {
int x;
cin >> x;
cout << Search(x) << '\n';
}
else if (op == "MAX") {
cout << MaxDiff() << '\n';
}
else {
cout << MinDiff() << '\n';
}
}
return 0;
}