Cod sursa(job #2745732)

Utilizator Alex_tz307Lorintz Alexandru Alex_tz307 Data 26 aprilie 2021 22:42:14
Problema Zeap Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.88 kb
#include <bits/stdc++.h>
#define ABS(x) ((x) >= 0 ? (x) : -(x))

using namespace std;

ifstream fin("zeap.in");
ofstream fout("zeap.out");

struct cmp {
  bool operator () (const pair<int,int> &A, const pair<int,int> &B) const {
    return ABS(A.first - A.second) > (B.first - B.second);
  }
};
set<int> S;
priority_queue<pair<int,int>,vector<pair<int,int>>,cmp> pq;
string op;
int x;

void solve() {
  while (fin >> op) {
    if (op == "I") {
      fin >> x;
      if (S.count(x))
        continue;
      S.emplace(x);
      auto it = S.find(x);
      if (it != S.begin()) {
        auto it2 = it;
        --it2;
        pq.emplace(*it, *it2);
      }
      auto it2 = it;
      ++it2;
      if (it2 != S.end())
        pq.emplace(*it, *it2);
      continue;
    }
    if (op == "S") {
      fin >> x;
      auto it = S.find(x);
      if (it == S.end()) {
        fout << "-1\n";
        continue;
      }
      auto it1 = it, it2 = it;
      ++it2;
      if (it != S.begin() && it2 != S.end()) {
        --it1;
        pq.emplace(*it1, *it2);
      }
      S.erase(it);
      continue;
    }
    if (op == "C") {
      fin >> x;
      if (S.count(x))
        fout << "1\n";
      else fout << "0\n";
      continue;
    }
    if (op == "MAX") {
      if ((int)S.size() < 2)
        fout << "-1\n";
      else fout << *(--S.end()) - *S.begin() << '\n';
      continue;
    }
    if (op == "MIN") {
      if ((int)S.size() < 2 || pq.empty()) {
        fout << "-1\n";
        continue;
      }
      while (!S.count(pq.top().first) || !S.count(pq.top().second))
        pq.pop();
      if (pq.empty())
        fout << "-1\n";
      else fout << ABS(pq.top().first - pq.top().second) << '\n';
      continue;
    }
  }
}

void close_files() {
  fin.close();
  fout.close();
}

int main() {
  solve();
  close_files();
  return 0;
}