-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqrt decomposition.cpp
75 lines (70 loc) · 1.44 KB
/
sqrt decomposition.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
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
const int mod=1e9+7;
const int N=1e5+9;
int a[N];
int F[N];
int block;
// must be 0 based index
int get_min(int l, int r)
{
int LB = l / block;
int RB = r / block;
int mn = INT_MAX;
if(LB == RB)
{
for(int i = l; i <= r; i++) mn = min(mn,a[i]);
return mn;
}
for(int i = l; i < (LB+1) * block; i++) mn = min(mn,a[i]);
for(int i = LB+1; i < RB; i++) mn = min(mn,F[i]);
for(int i = (RB)*block; i <= r; i++) mn = min(mn,a[i]);
return mn;
}
void solve()
{
int n;
cin >> n;
for(int i = 0; i < n; i++) cin >> a[i];
block = sqrt(n);
int r = n / block;
for(int i = 0; i < r; i++)
{
int mn = INT_MAX;
for(int j = block * i ; j < (i+1)*block; j++)
{
mn = min(mn,a[j]);
}
F[i] = mn;
// cout << "i: " << i << " " << F[i] << endl;
}
if(n%block)
{
int mn = INT_MAX;
for(int j = r * block; j < n; j++) mn = min(mn,a[j]);
F[r] = mn;
}
int q;
cin >> q;
while(q--)
{
int l, r;
cin >> l >> r;
cout << get_min(l,r) << endl;
}
}
int main()
{
IOS;
int t = 1;
//cin >> t;
while(t--) solve();
return 0;
}