#include <bits/stdc++.h>
using namespace std;
int n,m,x[1000001],lg[1000001],st[1000001][50];
inline int read()
{
int xx=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){xx=xx*10+ch-48;ch=getchar();}
return xx*f;
}
int main()
{
n = read(),m = read();
lg[0] = -1;
for (int i = 1; i <= n; i++)
{
x[i] = read();
lg[i] = lg[i - 1] + ( ( 1 << (lg[i - 1] + 1) ) == i);
}
for (int i = 1; i <= n; i++)
{
st[i][0] = x[i];
}
for (int i = 1; i <= lg[n] + 1; i++)
{
for (int j = 1; j <= n; j++)
{
st[j][i] = max(st[j][i - 1],st[j + (1 << (i - 1))][i - 1]);
}
}
int a,b,l;
while(m--)
{
a = read(),b = read();
l = b - a + 1;
printf("%d\n",max( st[a][lg[l]],st[b - (1 << lg[l]) + 1][lg[l]] ));
}
return 0;
}
lg函数: l g i lg_{i} lgi表示满足 2 j < = i 2^{j}<=i 2j<=i的最大 j j j,可利用递推的方式得到,注意 l g 0 = − 1 lg_{0}=-1 lg0=−1,每一个新的数超过前一个的 2 j + 1 2^{j+1} 2j+1了,就加 1 1 1
st函数:类似倍增的转移