fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define FAST ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
  4. #define ll long long
  5. #define all(a) a.begin(), a.end()
  6. void solve() {
  7. int n;
  8. cin >> n;
  9. multiset<ll> elm;
  10. multiset<ll> diff;
  11. while (n--) {
  12. int op;
  13. cin >> op;
  14.  
  15. if (op == 1) {
  16. ll x;
  17. cin >> x;
  18.  
  19. auto it = elm.insert(x);
  20. auto nxt = next(it);
  21.  
  22. if (it != elm.begin()) {
  23. auto prv = prev(it);
  24. diff.insert(x - *prv);
  25. }
  26. if (nxt != elm.end()) {
  27. diff.insert(*nxt - x);
  28. }
  29. if (it != elm.begin() && nxt != elm.end()) {
  30. auto prv = prev(it);
  31. diff.erase(diff.find(*nxt - *prv));
  32. }
  33. }
  34. else if (op == 2) {
  35. ll x;
  36. cin >> x;
  37.  
  38. auto it = elm.find(x);
  39. if (it == elm.end()) continue;
  40.  
  41. auto nxt = next(it);
  42.  
  43. if (it != elm.begin()) {
  44. auto prv = prev(it);
  45. diff.erase(diff.find(x - *prv));
  46. }
  47. if (nxt != elm.end()) {
  48. diff.erase(diff.find(*nxt - x));
  49. }
  50. if (it != elm.begin() && nxt != elm.end()) {
  51. auto prv = prev(it);
  52. diff.insert(*nxt - *prv);
  53. }
  54.  
  55. elm.erase(it);
  56. }
  57. else
  58. {
  59. if (elm.size() < 2) cout << -1 << endl;
  60. else cout << *diff.begin() << endl;
  61. }
  62. }
  63. }
  64.  
  65. int main() {
  66. FAST;
  67. int t = 1;
  68. // cin >> t;
  69. while (t--)
  70. solve();
  71. return 0;
  72. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Standard output is empty