2022-10-28 01:23:22 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cmath>
|
2020-12-30 08:41:43 +00:00
|
|
|
|
2022-10-28 01:23:22 +00:00
|
|
|
using std::cin;
|
|
|
|
using std::cout;
|
|
|
|
const char endl = '\n';
|
2020-12-30 08:41:43 +00:00
|
|
|
|
2022-10-28 01:23:22 +00:00
|
|
|
const int N = 1e5 + 5;
|
|
|
|
|
|
|
|
int n, m, k, t, f[N][25];
|
2020-12-30 08:41:43 +00:00
|
|
|
|
|
|
|
int main() {
|
2022-10-28 01:23:22 +00:00
|
|
|
std::ios::sync_with_stdio(false);
|
|
|
|
cin.tie(nullptr);
|
|
|
|
|
|
|
|
cin >> n >> m;
|
|
|
|
|
2020-12-30 08:41:43 +00:00
|
|
|
for (int i = 1; i <= n; i++) {
|
2022-10-28 01:23:22 +00:00
|
|
|
cin >> f[i][0];
|
2020-12-30 08:41:43 +00:00
|
|
|
}
|
2022-10-28 01:23:22 +00:00
|
|
|
|
|
|
|
t = std::__lg(n);
|
|
|
|
for (int i = 1; i <= t; i++) {
|
2020-12-30 08:41:43 +00:00
|
|
|
for (int j = 1; j <= n - (1 << i) + 1; j++) {
|
2022-10-28 01:23:22 +00:00
|
|
|
f[j][i] = std::max(f[j][i - 1], f[j + (1 << i - 1)][i - 1]);
|
2020-12-30 08:41:43 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-28 01:23:22 +00:00
|
|
|
|
|
|
|
for (int i = 1, l, r; i <= m; i++) {
|
|
|
|
cin >> l >> r;
|
|
|
|
|
|
|
|
k = std::__lg(r - l + 1);
|
|
|
|
|
|
|
|
cout << std::max(f[l][k], f[r - (1 << k) + 1][k]) << endl;
|
2020-12-30 08:41:43 +00:00
|
|
|
}
|
2022-10-28 01:23:22 +00:00
|
|
|
|
2020-12-30 08:41:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|