Advertisement
YEZAELP

SMMR-247: Leakage

Jun 13th, 2021
770
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. using lli = long long;
  5. const lli INF = 1e18;
  6. using pl = pair <lli, lli>;
  7. using pll = pair <lli, pl>;
  8. set <lli> num;
  9. priority_queue <pll, vector<pll>, greater<pll>> pq;
  10.  
  11. bool Have(lli x){
  12.     return num.find(x) != num.end();
  13. }
  14.  
  15. bool check(pll x){
  16.     return Have(x.second.first) and Have(x.second.second);
  17. }
  18.  
  19. void Del(){
  20.     while(!pq.empty() and !check(pq.top())) pq.pop();
  21. }
  22.  
  23. int main(){
  24.  
  25.     int q;
  26.     scanf("%d", &q);
  27.  
  28.     for(int i=1;i<=q;i++){
  29.         int opr;
  30.         lli x;
  31.         scanf("%d", &opr);
  32.         if(opr == 1){
  33.             scanf("%lld", &x);
  34.             num.insert(x);
  35.             auto it = num.find(x);
  36.             if(it == num.begin() and it == num.end()) continue;
  37.             if(it == num.begin()) pq.push({*next(it) - x, {x, *next(it)}});
  38.             else if(it == num.end()) pq.push({x - *prev(it), {*prev(it), x}});
  39.             else {
  40.                 pq.push({*next(it) - x, {x, *next(it)}});
  41.                 pq.push({x - *prev(it), {*prev(it), x}});
  42.             }
  43.         }
  44.         else if(opr == 2){
  45.             scanf("%lld", &x);
  46.             auto it = num.find(x);
  47.             if(it != num.begin() and it != num.end()) pq.push({*next(it) - *prev(it), {*prev(it), *next(it)}});
  48.             num.erase(it);
  49.         }
  50.         else if(opr == 3){
  51.             Del();
  52.             printf("%lld\n", pq.top().first);
  53.         }
  54.         else if(opr == 4){
  55.             printf("%lld\n", (lli) *num.rbegin() - *num.begin());
  56.         }
  57.     }
  58.  
  59.     return 0;
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement