#include <cstdio> #include <cmath> #include <cstring> #include <iostream> #include <algorithm> using namespace std; int n, m, f[100010][21]; void init() { for (int j = 1; j <= 20; j++) for (int i = 1; i + (1 << j) - 1 <= n; i++) f[i][j] = max(f[i][j - 1],f[i + (1 << (j - 1))][j - 1]); } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &f[i][0]); init(); for (int i = 1; i <= m; i++) { int l, r, t; scanf("%d%d", &l, &r); t = (int)((log(r - l + 1)) / log(2.0)); printf("%d ", max(f[l][t], f[r - (1 << t) + 1][t])); } return 0; }