-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathd.cpp
90 lines (81 loc) · 2.1 KB
/
d.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
template <typename T1, typename T2>
ostream &operator <<(ostream &os, pair<T1, T2> p){os << p.first << " " << p.second; return os;}
template <typename T>
ostream &operator <<(ostream &os, vector<T> &v){for(T i : v)os << i << ", "; return os;}
template <typename T>
ostream &operator <<(ostream &os, multiset<T> s){for(T i : s) os << i << ", "; return os;}
template <typename T1, typename T2>
ostream &operator <<(ostream &os, map<T1, T2> m){for(pair<T1, T2> i : m) os << i << endl; return os;}
int N, Q;
multiset<int> p, gaps;
void add(int x){
auto it = p.insert(x);
auto it1 = it, it2 = it;
it1--;
it2++;
int n = *it, n1 = *it1, n2 = *it2;
if((int)p.size() == 1){}
else if(it == p.begin()){
gaps.insert(n2 - n);
}else if(it2 == p.end()){
gaps.insert(n - n1);
}else{
gaps.erase(gaps.find(n2 - n1));
gaps.insert(n2 - n);
gaps.insert(n - n1);
}
}
void remove(int x){
auto it = p.find(x);
auto it1 = it, it2 = it;
it1--;
it2++;
int n = *it, n1 = *it1, n2 = *it2;
if((int)p.size() == 1){}
else if(it == p.begin()){
gaps.erase(gaps.find(n2 - n));
}else if(it2 == p.end()){
gaps.erase(gaps.find(n - n1));
}else{
gaps.insert(n2 - n1);
gaps.erase(gaps.find(n2 - n));
gaps.erase(gaps.find(n - n1));
}
p.erase(it);
}
void print(){
if((int)p.size() == 0){
cout << 0 << endl;
}else if((int)gaps.size() == 0){
cout << *p.rbegin() - *p.begin() << endl;
}else{
cout << *p.rbegin() - *p.begin() - *gaps.rbegin() << endl;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> Q;
for(int i = 0; i < N; i++){
int x; cin >> x;
add(x);
}
print();
for(int i = 0; i < Q; i++){
int t, x; cin >> t >> x;
if(t == 0){
remove(x);
}else{
add(x);
}
print();
}
return 0;
}