## Description

For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.

Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.

## Input

Line 1: Two space-separated integers, N and Q.
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i
Lines N+2..N+Q+1: Two integers A and B (1 ≤ A ≤ B ≤ N), representing the range of cows from A toB inclusive.

## Output

Lines 1..Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.

6 3
1
7
3
4
2
5
1 5
4 6
2 2

## Sample Output

6
3
0
给你n 按一定顺序排列的个奶牛的高度，求给定区间内最高的奶牛和最矮的奶牛的差值。

我们可以开辟一个数组专门来保存f(i, j)的值。
例如，f(0, 0)表示[0,0]之间的最小值,就是num[0], f(0, 2)表示[0, 3]之间的最小值, f(2, 4)表示[2, 17]之间的最小值

`#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<vector>#include<stdlib.h>#include<queue>#include<map>#include<iomanip>#include<math.h>using namespace std;typedef long long ll;typedef double ld;int dpmin[50010][20];int dpmax[50010][20];int i,j,k;int n,m;int a[50010];void rmq_min(int N){    for(int i=1;i<=N;i++)        dpmin[i][0]=a[i];//初始化    for(int j=1;(1<<j)<=N;j++)        for(int i=1;i+(1<<j)-1<=N;i++)            dpmin[i][j]=min(dpmin[i][j-1],dpmin[i+(1<<j-1)][j-1]);            return ;}void rmq_max(int N){    for(int i=1;i<=N;i++)        dpmax[i][0]=a[i];//初始化    for(int j=1;(1<<j)<=N;j++)        for(int i=1;i+(1<<j)-1<=N;i++)            dpmax[i][j]=max(dpmax[i][j-1],dpmax[i+(1<<j-1)][j-1]);            return ;}int askmin(int l,int r){    int k=log2(r-l+1);    return min(dpmin[l][k],dpmin[r-(1<<k)+1][k]);}int askmax(int l,int r){    int k=log2(r-l+1);    return max(dpmax[l][k],dpmax[r-(1<<k)+1][k]);}int main(){    scanf("%d %d",&n,&m);    for(i=1;i<=n;i++)        scanf("%d",&a[i]);    rmq_min(n);    rmq_max(n);    int l,r;    int ans;    while(m--)    {        scanf("%d %d",&l,&r);        //cout<<askmax(l,r)<<" "<<askmin(l,r)<<endl;        ans=askmax(l,r)-askmin(l,r);        printf("%d\n",ans);    }    return 0;}`

## 线段树做法：

`#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;typedef long long ll;#define INF 0x3f3f3f3fconst int maxn=50005;struct node{    int l,r,Max,Min;}tr[maxn<<2];int n,m,a[maxn],ans1,ans2;void build(int v,int l,int r){    tr[v].l=l,tr[v].r=r;    if(l==r)    {        tr[v].Max=tr[v].Min=a[l];        return;    }    int mid=(tr[v].l+tr[v].r)>>1;    build(v<<1,l,mid);    build(v<<1|1,mid+1,r);    tr[v].Max=max(tr[v<<1].Max,tr[v<<1|1].Max);    tr[v].Min=min(tr[v<<1].Min,tr[v<<1|1].Min);}void query(int v,int l,int r){    if(l<=tr[v].l&&r>=tr[v].r)    {        ans1=max(ans1,tr[v].Max);        ans2=min(ans2,tr[v].Min);        return;    }    int mid=(tr[v].l+tr[v].r)>>1;    if(l<=mid)        query(v<<1,l,r);    if(r>mid)        query(v<<1|1,l,r);}int main(){    scanf("%d %d",&n,&m);    for(int i=1; i<=n; ++i)        scanf("%d",&a[i]);    build(1,1,n);    while(m--)    {        int l,r;        scanf("%d %d",&l,&r);        ans1=0,ans2=INF;        query(1,l,r);        printf("%d\n",ans1-ans2);    }    return 0;}`