#include <iostream> #include <cmath> #include <algorithm> using namespace std; typedef long long ll; int dp[50005][31][2]; int main() { ios::sync_with_stdio(false); int n,m;cin>>n>>m; for(int i=1;i<=n;i++){ cin>>dp[i][0][0]; for(int j=0;j<=30;j++) dp[i][0][1]=100000000; dp[i][0][1]=dp[i][0][0]; } for(int j=1;j<=30;j++) for(int i=1;i+(1<<j)<=n+1;i++){ dp[i][j][0]=min(dp[i][j-1][0],dp[i+(1<<(j-1))][j-1][0]); dp[i][j][1]=max(dp[i][j-1][1],dp[i+(1<<(j-1))][j-1][1]); } while(m--){ int l,r; cin>>l>>r; int k=log2(r-l+1); cout<<max(dp[l][k][1],dp[r-(1<<k)+1][k][1])-min(dp[l][k][0],dp[r-(1<<k)+1][k][0])<<"\n"; } }