Cod sursa(job #2602662)

Utilizator segtreapMihnea Andreescu segtreap Data 17 aprilie 2020 16:48:36
Problema Zeap Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.54 kb
#include <iostream>
#include <cstdio>
#include <set>
using namespace std;
set<int> s;
multiset<int> difs;
int main() {
  freopen ("zeap.in", "r", stdin);
  freopen ("zeap.out", "w", stdout);
  string o;
  int x;
  while (cin >> o) {
    if (o == "I") {
      cin >> x;
      if (s.count(x)) {
        continue;
      }
      int l = -1, r = -1;
      auto it = s.lower_bound(x);
      if (it != s.end()) {
        r = *it;
      }
      if (it != s.begin()) {
        it--;
        l = *it;
      }
      if (l != -1 && r != -1) difs.erase(difs.find(r - l));
      if (l != -1) difs.insert(x - l);
      if (r != -1) difs.insert(r - x);
      s.insert(x);
    }
    if (o == "S") {
      cin >> x;
      if (!s.count(x)) {
        cout << "-1\n";
        continue;
      }
      s.erase(x);
      int l = -1, r = -1;
      auto it = s.lower_bound(x);
      if (it != s.end())  r = *it;
      if (it != s.begin()) {
        it--;
        l = *it;
      }
      if (l != -1 && r != -1) difs.insert(r - l);
      if (l != -1) difs.erase(difs.find(x - l));
      if (r != -1) difs.erase(difs.find(r - x));
    }
    if (o == "C") {
      cin >> x;
      cout << s.count(x) << "\n";
    }
    if (o == "MIN") {
      if (difs.empty())
        cout << "-1\n";
      else
        cout << *difs.begin() << "\n";
    }
    if (o == "MAX") {
      if (difs.empty())
        cout << "-1\n";
      else {
        auto it1 = s.begin(), it2 = s.end();
        it2--;
        cout << *it2 - *it1 << "\n";
      }
    }
  }
}