-
Notifications
You must be signed in to change notification settings - Fork 0
/
RMQ.cpp
53 lines (37 loc) · 852 Bytes
/
RMQ.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;
#define pii pair<int, int>
int main(){
priority_queue<pii, vector<pii>, greater<pii>> p;
deque<pii> q;
vector<int> res;
pii temp;
int qu, i, n, a[100], m;
cin>>n>>qu;
for(i=0; i<n; i++) cin>>a[i];
for(i=0; i<qu; i++){
cin>>temp.second>>temp.first;
p.push(temp);
}
for(i=0; i<n; i++) {
if(p.empty()) break;
while(!q.empty() && q.front().second<p.top().second) q.pop_front();
if(i==p.top().first) {
res.push_back(q.front().first);
p.pop();
while(!q.empty() && q.front().second<p.top().second) q.pop_front();
}
while(!q.empty() && q.back().first>=a[i]) q.pop_back();
q.push_back(pii(a[i],i));
}
for(i=0; i<res.size(); i++) cout<<res[i]<<" ";
cout<<endl;
return 0;
}
/*
8 3
10 15 3 8 11 13 18 19
5 7
3 6
1 3
*/