#include <bits/stdc++.h> using namespace std; #define int long long const int M = 1e5 + 5; int dp[M][22]; signed main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int n, t; cin >> n >> t; for (int i = 1; i <= n; i++) cin >> dp[i][0]; for (int j = 1; j <= 22; j++) { for (int i = 1; i + (1 << j) - 1 <= n; i++) dp[i][j] = max(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]); } while (t--) { int x, y; cin >> x >> y; int k = log2(y - x + 1); cout << max(dp[x][k], dp[y - (1 << k) + 1][k]) << "\n"; } return 0; }