# http://acm.nyist.net/JudgeOnline/problem.php?pid=119

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<climits>
#include<algorithm>
using namespace std;
const int MAX = 0xfffffff;
const int N = 100005;
int maxsum, minsum;
struct segtree
{  int l;
int r;
int max;
int min;
}seg[N << 2];

void Build_Tree(int t,int l,int r)
{
seg[t].l=l;
seg[t].r=r;
if(l == r)
{
scanf("%d", &seg[t].max);
seg[t].min = seg[t].max;
return ;
}

int m = (l + r) >> 1;
Build_Tree(t<<1,l,m);
Build_Tree(t<<1|1,m+1,r);
seg[t].max = max(seg[t << 1].max, seg[(t << 1) + 1].max);
seg[t].min = min(seg[t << 1].min, seg[(t << 1) + 1].min);
}

void Query(int t,int l,int r)
{
if(l<= seg[t].l && seg[t].r <= r)
{
maxsum = max(maxsum, seg[t].max);
minsum = min(minsum, seg[t].min);
return ;
}
if(r<=seg[t<<1].r)  Query(t<<1,l,r);
else if(l>seg[t<<1].r)  Query(t<<1|1,l,r);
else
{
Query(t<<1,l,seg[t<<1].r);
Query(t<<1|1,seg[t<<1|1].l,r);
}

}

int main()
{
int n, q;
int a, b;
while(scanf("%d%d", &n, &q) != EOF)
{
Build_Tree(1,1,n);
while(q--)
{
scanf("%d %d", &a, &b);
maxsum = -MAX, minsum = MAX;
Query(1,a,b);
printf("%d\n", maxsum-minsum);
}
}
return 0;
}


#### http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=301 (矩阵的用法)

2013-08-08 22:14:22

#### http://acm.nyist.net/JudgeOnline/problem.php?pid=401&&简单的博弈论

2011-10-25 10:48:34

#### I/O & Java in ACM

2015-11-08 22:21:51

#### http://acm.nyist.net/JudgeOnline/problem.php?pid=301&&递推求值

2012-03-02 21:02:36

#### http://acm.nyist.net/JudgeOnline/problem.php?pid=290

2011-10-22 21:31:46

#### 矩阵总结（矩阵若干类型题）

2014-08-23 01:42:51

#### [南阳]-19 擅长排列的小明 [STL/DFS]

2017-07-18 13:47:07

#### http://acm.nyist.net/JudgeOnline/problem.php?pid=221

2012-03-29 15:26:59

#### http://acm.nyist.net/JudgeOnline/problem.php?pid=129

2011-11-03 11:26:23

#### http://acm.nyist.net/JudgeOnline/problem.php?pid=90

2012-03-30 09:52:13