[BZOJ1636]&&[Usaco2007 Jan]Balanced Lineup

描述 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.

输入格式 InputFormat

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 to B inclusive.

输出格式 OutputFormat

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.

样例输入 SampleInput

50 50
16
17
16
5
5
5
49
3
10
30
31
6
25
14
30
27
19
32
30
2
47
22
11
21
46
8
17
10
32
2
29
13
39
26
44
18
29
33
6
35
37
31
25
42
40
36
5
36
34
30
33 40
44 50
25 45
46 49
4 48
13 46
26 41
28 33
40 48
28 31
43 47
22 45
35 38
42 44
23 30
15 15
8 36
18 40
43 47
43 50
31 38
47 50
19 30
11 41
37 44
15 38
48 49
32 34
30 38
13 43
24 37
30 44
3 17
12 47
12 30
49 49
5 39
31 48
45 46
32 38
34 36
32 38
46 47
21 28
35 41
17 23
38 44
31 43
9 32
40 46

样例输出 SampleOutput

38
37
44
31
47
45
42
37
37
30
37
44
26
17
44
0
45
45
37
37
31
31
45
45
36
45
2
26
42
45
44
42
46
45
45
0
47
39
4
31
26
31
31
39
38
45
36
38
45
17

来源 Source

Silver


BZOJ 1636


代码 Code

裸线段树。

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define L(s) s<<1
#define R(s) (s<<1)+1
const int inf=0x7fffffff;
struct tnode
{
    int l,r,maxa,mina;
}tree[200005];
int a[50005];
int i,j,t,n,m,k,z,y,x,amax,amin;
void build(int s,int x,int y)
{
    int i,j,t;
    tree[s].l=x;tree[s].r=y;
    if (x==y)
    {
        tree[s].maxa=tree[s].mina=a[x];
        return ;
    }
    build(L(s),x,(x+y)/2);
    build(R(s),(x+y)/2+1,y);
    tree[s].maxa=max(tree[L(s)].maxa,tree[R(s)].maxa);
    tree[s].mina=min(tree[L(s)].mina,tree[R(s)].mina);
}
void find(int s,int tl,int tr)
{
    int i,j,t;
    int mid=(tree[s].l+tree[s].r)/2;
    if (tree[s].l==tl && tree[s].r==tr)
    {
        amax=max(amax,tree[s].maxa);
        amin=min(amin,tree[s].mina);
        return ;
    }
    if (tl>tree[s].r || tr<tree[s].l) return ;
    if (tl>=tree[s].l && tr<=mid) find(L(s),tl,tr);
    else if (tl>mid && tr<=tree[s].r) find(R(s),tl,tr);
    else
    {
        find(L(s),tl,mid);
        find(R(s),mid+1,tr);
    }
}
void TEST()
{
    int i,j,t;
    for (i=1;i<=2*n+1;i++) cout<<""<<i<<"  "<<tree[i].l<<"~"<<tree[i].r<<" : "<<" max="<<tree[i].maxa<<" | min="<<tree[i].mina<<endl;
}
int main()
{
    scanf("%d%d",&n,&m);
    for (i=1;i<=n;i++) scanf("%d",&a[i]);
    build(1,1,n);
    for (i=1;i<=m;i++)
    {
        scanf("%d%d",&x,&y);
        if (x>y) swap(x,y);
        amax=-inf;amin=inf;
        find(1,x,y);
        printf("%d\n",amax-amin);
    }
    return 0;
}