-
Notifications
You must be signed in to change notification settings - Fork 439
/
Sparse-Table.cpp
67 lines (58 loc) · 854 Bytes
/
Sparse-Table.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
#include <cstdio>
using namespace std;
int n, m;
int st[1000010][21], pow2[21];
inline int max(int x, int y)
{
return x > y ? x : y;
}
inline int _log2(int x)
{
int res = 0;
while (x > 1)
{
x >>= 1;
res++;
}
return res;
}
void init_pow2()
{
pow2[0] = 1;
for (int i = 1; i <= 20; i++)
{
pow2[i] = pow2[i - 1] * 2;
}
}
void build_st()
{
init_pow2();
for (int i = 1; i <= 20; i++)
{
for (int j = 0; j < n; j++)
{
st[j][i] = max(st[j][i - 1], st[j + pow2[i - 1]][i - 1]);
}
}
}
int get_max(int l, int r)
{
int x = _log2(r - l + 1);
return max(st[l][x], st[r - pow2[x] + 1][x]);
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++)
{
scanf("%d", &st[i][0]);
}
build_st();
for (int i = 0; i < m; i++)
{
int a, b;
scanf("%d%d", &a, &b);
printf("%d\n", get_max(a - 1, b - 1));
}
return 0;
}