Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using lli = long long;
- const lli INF = 1e18;
- using pl = pair <lli, lli>;
- using pll = pair <lli, pl>;
- set <lli> num;
- priority_queue <pll, vector<pll>, greater<pll>> pq;
- bool Have(lli x){
- return num.find(x) != num.end();
- }
- bool check(pll x){
- return Have(x.second.first) and Have(x.second.second);
- }
- void Del(){
- while(!pq.empty() and !check(pq.top())) pq.pop();
- }
- int main(){
- int q;
- scanf("%d", &q);
- for(int i=1;i<=q;i++){
- int opr;
- lli x;
- scanf("%d", &opr);
- if(opr == 1){
- scanf("%lld", &x);
- num.insert(x);
- auto it = num.find(x);
- if(it == num.begin() and it == num.end()) continue;
- if(it == num.begin()) pq.push({*next(it) - x, {x, *next(it)}});
- else if(it == num.end()) pq.push({x - *prev(it), {*prev(it), x}});
- else {
- pq.push({*next(it) - x, {x, *next(it)}});
- pq.push({x - *prev(it), {*prev(it), x}});
- }
- }
- else if(opr == 2){
- scanf("%lld", &x);
- auto it = num.find(x);
- if(it != num.begin() and it != num.end()) pq.push({*next(it) - *prev(it), {*prev(it), *next(it)}});
- num.erase(it);
- }
- else if(opr == 3){
- Del();
- printf("%lld\n", pq.top().first);
- }
- else if(opr == 4){
- printf("%lld\n", (lli) *num.rbegin() - *num.begin());
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement