1 #include<bits/stdc++.h> 2 using namespace std; 3 #define MAXN 1000+10 4 int dp[MAXN][MAXN], a[MAXN]; 5 6 void InitRMQ(int l, int r, int n){ 7 int k = floor((double)log(r-l+1)/log(2)); 8 for (int i = 1; i <= n; ++i) dp[i][0] = a[i]; 9 for (int j = 1; j <= k; ++j) 10 for (int i = 1; i+(1<<j-1) <= r; ++i) 11 dp[i][j] = max(dp[i][j-1], dp[i+(1<<j-1)][j-1]); 12 } 13 14 int getmax(int l, int r){ 15 int k = floor((double)log(r-l+1)/log(2)); 16 return max(dp[l][k], dp[r-(1<<k)][k]); 17 } 18 19 int main() 20 { 21 int n, q; 22 cin>>n>>q; 23 for (int i = 1; i <= n; ++i) cin>>a[i]; 24 InitRMQ(1, n, n); 25 int l, r; 26 while(q--){ 27 cin>>l>>r; 28 cout<<getmax(l, r)<<endl; 29 } 30 31 return 0; 32 }