#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int dp[500001][40],a[500001];
int fn (int l,int r)
{
int k=log2(r-l+1);
return max(dp[l][k],dp[r-(1<<k)+1][k]);
}
int main()
{
int n,q,i,j,l,r;
cin>>n>>q;
for(i=1;i<=n;i++)
{
cin>>a[i];
dp[i][0]=a[i];
}
int p=log2(n);
for(int k=1;k<=p;k++)
{
for(int s=1;s+(1<<k)<=n+1;s++)
dp[s][k]=max(dp[s][k-1],dp[s+(1<<(k-1))][k-1]);
}
for(i=1;i<=q;i++)
{
cin>>l>>r;
cout<<fn(l,r)<<'\n';
}
return 0;
}
找区间最小值同理,将max都改成min函数即可