1 #include<bits/stdc++.h> 2 using namespace std; 3 int x,fp,f[200100][22],h[100100]; 4 char ch; 5 inline int read() 6 { 7 x=0;fp=1;ch=getchar(); 8 while(ch<'0'||ch>'9'){if(ch=='-')fp=-1;ch=getchar();} 9 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 10 return x*fp; 11 } 12 int n,m,a[1000100],s[1000100],ans=0,sum; 13 void STpre() 14 { 15 for(int i=1;i<=n;i++)f[i][0]=a[i]; 16 int t=log(n)/log(2)+1; 17 for(int j=1;j<=t;j++) 18 for(int i=1;i<=n;i++) 19 f[i][j]=max(f[i][j-1],f[i+(1<<(j-1))][j-1]); 20 int ss=1,sum=0; 21 for(int i=1;i<=n;i++) 22 {if(i==ss*2){sum++;ss*=2;}h[i]=sum;} 23 } 24 void work() 25 { 26 int cj,xx,yy; 27 while(m--) 28 { 29 xx=read();yy=read(); 30 cj=yy-xx+1; 31 printf("%d ",max(f[xx][h[cj]],f[yy-(1<<h[cj])+1][h[cj]])); 32 } 33 } 34 int main() 35 { 36 //freopen("xf.in","r",stdin); 37 //freopen("xf.out","w",stdout); 38 n=read();m=read(); 39 for(int i=1;i<=n;i++)a[i]=read(); 40 STpre(); 41 work(); 42 return 0; 43 } 44