#include<iostream>#include<cstdio>#include<cmath>usingnamespace std;constint M =16, N =1<< M;int n, m;int w[N], f[N][M], g[N][M];voidinit(){for(int i =1; i <= n; i ++) f[i][0]= w[i];for(int j =1;(1<< j)<= n; j ++)for(int i =1; i +(1<< j)-1<= n; i ++)
f[i][j]=max(f[i][j -1], f[i +(1<<(j -1))][j -1]);for(int i =1; i <= n; i ++) g[i][0]= w[i];for(int j =1;(1<< j)<= n; j ++)for(int i =1; i +(1<< j)-1<= n; i ++)
g[i][j]=min(g[i][j -1], g[i +(1<<(j -1))][j -1]);}intquery_max(int l,int r){int len = r - l +1;int k =log(len)/log(2);returnmax(f[l][k], f[r -(1<< k)+1][k]);}intquery_min(int l,int r){int len = r - l +1;int k =log(len)/log(2);returnmin(g[l][k], g[r -(1<< k)+1][k]);}intmain(){scanf("%d%d",&n,&m);for(int i =1; i <= n; i++)scanf("%d",&w[i]);init();while(m--){int l, r;scanf("%d%d",&l,&r);printf("%d\n",query_max(l, r)-query_min(l, r));}return0;}