看你太可怜了,给你一个火车头
#include <bits/stdc++.h>
#define mem(a,b) memset((a),(b),sizeof(a))
#define MP make_pair
#define pb push_back
#define fi first
#define se second
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define sqr(x) (x)*(x)
#define REP(i,j) for(int i=0;i<j;i++)
#define REP1(i,j) for(int i=1;i<=j;i++)
using namespace std;
using namespace __gnu_cxx;
typedef int I;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<I,I> PII;
typedef pair<ll,ll> PLL;
typedef pair<I,ll> PIL;
typedef pair<ll,I> PLI;
typedef vector<I> VI;
typedef vector<ll> VL;
typedef vector<PII> VPII;
typedef vector<string> VS;
typedef greater<I> GI;
typedef priority_queue<I> PQI;
typedef priority_queue<I,VI,GI> PQV;
/************* define end *************/
void println(VI x){for(I i=0;i<sz(x);i++) printf("%d%c",x[i]," \n"[i==sz(x)-1]);}
void println(VL x){for(I i=0;i<sz(x);i++) printf("%lld%c",x[i]," \n"[i==sz(x)-1]);}
void println(I *x,I l,I r){for(I i=l;i<=r;i++) printf("%d%c",x[i]," \n"[i==r]);}
void println(ll *x,I l,I r){for(I i=l;i<=r;i++) printf("%lld%c",x[i]," \n"[i==r]);}
void pri(ll x,ll y){printf("%lld %lld\n",x,y);}
void pri(I x,I y){printf("%d %d",x,y);}
void pri(ll x){printf("%lld\n",x);}
void pri(int x){printf("%d\n",x);}
/*************** IO end ***************/
void go();//主函数
int main(){
//#ifdef tokitsukaze
//freopen("TEST.txt","r",stdin);
//#endif
go();return 0;
}
const int INF=0x3f3f3f3f;
const ll LLINF=0x3f3f3f3f3f3f3f3fLL;
const double PI=acos(-1.0);
const double eps=1e-6;
const int MAX=2e5+10;
const ll mod=998244353;
/********************************* head *********************************/
int a[100005],n,q,l,r,f[100005][25],k[100005][25],lg[100005];
void go(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
f[i][0]=a[i];
k[i][0]=a[i];
if(i>1)lg[i]=lg[i>>1]+1;
}
for(int j=1;j<=20;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]);
k[i][j]=min(k[i][j-1],k[i+(1<<(j-1))][j-1]);
}
}
scanf("%d",&q);
while(q--){
scanf("%d%d",&l,&r);
int m=lg[r-l+1];
printf("%d\n",max(f[l][m],f[r-(1<<m)+1][m])-min(k[l][m],k[r-(1<<m)+1][m]));
}
}