Repository navigation
Expand file tree
/
Copy pathA_Range_Minimum_Query.cpp
More file actions
86 lines (65 loc) · 1.57 KB
/
Copy pathA_Range_Minimum_Query.cpp
File metadata and controls
86 lines (65 loc) · 1.57 KB
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
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define f first
#define s second
//typedef
typedef long long int lli ;
#define fore(i, a, b) for(int i = (a); i < (b); ++i)
#define pll pair<long long, long long>
#define vll vector<long long>
#define all(a) (a).begin(),(a).end()
#define sz(a) (int)(a.size())
#define IO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
// g++ main.cpp -o a && ./a < in > out
template <class T, class F = function<T(const T&, const T&)>>
struct Sparse {
vector<T> sp[21]; // n <= 2^21
F f;
int n;
Sparse(T* begin, T* end, const F& f) : Sparse(vector<T>(begin, end), f) {}
Sparse(const vector<T>& a, const F& f) : f(f), n(sz(a)) {
sp[0] = a;
for (int k = 1; (1 << k) <= n; k++) {
sp[k].resize(n - (1 << k) + 1);
fore (l, 0, sz(sp[k])) {
int r = l + (1 << (k - 1));
sp[k][l] = f(sp[k - 1][l], sp[k - 1][r]);
}
}
}
T query(int l, int r) {
#warning Can give TLE D:, change it to a log table
int k = __lg(r - l + 1);
return f(sp[k][l], sp[k][r - (1 << k) + 1]);
}
};
void solve() {
int n;
cin>>n;
vector<int>v(n);
for(int i=0; i<n; ++i)
{
cin>>v[i];
}
Sparse<int>sp(v, [&](int a , int b){return min(a, b);});
int q;
cin>>q;
while(q--)
{
int l, r ;
cin>>l>>r;
cout<<sp.query(l , r ) <<endl;
}
}
int main()
{
IO;
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int t = 1;
//cin >> t;
for (int i = 1; i <= t; i++) {
solve();
}
}