-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonotonic-Queue.cpp
52 lines (43 loc) · 964 Bytes
/
Monotonic-Queue.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
#include<bits/stdc++.h>
using namespace std;
template<typename T>
class MinMaxQueue {
public:
deque<T> q, minq, maxq;
void push_back(T val) {
q.push_back(val);
while(!minq.empty() and minq.back() > val)
minq.pop_back();
while(!maxq.empty() and maxq.back() < val)
maxq.pop_back();
minq.push_back(val);
maxq.push_back(val);
}
void pop_front() {
T val = q.front(); q.pop_front();
if(val == minq.front())
minq.pop_front();
if(val == maxq.front())
maxq.pop_front();
}
T min() {
return minq.front();
}
T max() {
return maxq.front();
}
T front() {
return q.front();
}
int size() {
return q.size();
}
bool empty() {
return q.empty();
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
return 0;
}