#include<bits/stdc++.h>
using namespace std;
#define MAXN 1000+10
int dp[MAXN][MAXN], a[MAXN];
void InitRMQ(int l, int r, int n){
int k = floor((double)log(r-l+1)/log(2));
for (int i = 1; i <= n; ++i) dp[i][0] = a[i];
for (int j = 1; j <= r-l+1; ++j)
for (int i = 1; i+(1<<j-1) <= r; ++i)
dp[i][j] = max(dp[i][j-1], dp[i+(1<<j-1)][j-1]);
}
int getmax(int l, int r){
int k = floor((double)log(r-l+1)/log(2));
return max(dp[l][k], dp[r-(1<<k)][k]);
}
int main()
{
int n, q;
cin>>n>>q;
for (int i = 1; i <= n; ++i) cin>>a[i];
InitRMQ(1, n, n);
int l, r;
while(q--){
cin>>l>>r;
cout<<getmax(l, r)<<endl;
}
return 0;
}
RMQ问题模板
最新推荐文章于 2023-03-14 07:05:29 发布