1. 程式人生 > >Balanced Lineup(線段樹區間查詢)

Balanced Lineup(線段樹區間查詢)

D - Balanced Lineup

Time Limit: 5000 MS Memory Limit: 0 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

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

Sample Input

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

Sample Output

6
3
0
#include<stdio.h>
#include<iostream>

int n,m;
int maxn,minn;

struct node
{
    int l,r;
    int maxn,minn;
}tree[200000];

int h[50005];

int Max(int a,int b)
{
    if(a>b)
        return a;
    else
        return b;
}

int Min(int a,int b)
{
    if(a<b)
        return a;
    else
        return b;
}

void build(int l,int r,int k)
{
    tree[k].l = l;
    tree[k].r = r;
    if(l == r)
    {
        tree[k].maxn = h[l];
        tree[k].minn = h[l];
        return ;
    }
    int mid = (tree[k].l + tree[k].r)/2;
    build(l,mid,k*2);
    build(mid+1,r,k*2+1);
    tree[k].maxn = Max(tree[2*k].maxn,tree[2*k+1].maxn);
    tree[k].minn = Min(tree[2*k].minn,tree[2*k+1].minn);
}

void findmax(int l,int r,int k)
{
    if(tree[k].l==l && tree[k].r == r)
    {
        if(tree[k].maxn>maxn)
        {
            maxn = tree[k].maxn;
        }
        return ;
    }
    int mid = (tree[k].l + tree[k].r)/2;
    if(r<=mid)
    {
        findmax(l,r,k*2);
    }
    else if(l>mid)
    {
        findmax(l,r,2*k+1);
    }
    else
    {
        findmax(l,mid,2*k);
        findmax(mid+1,r,2*k+1);
    }
}

void findminn(int l,int r,int k)
{
    if(tree[k].l==l && tree[k].r==r)
    {
        if(tree[k].minn<minn)
        {
            minn = tree[k].minn;
        }
        return ;
    }
    int mid = (tree[k].r+tree[k].l)/2;
    if(r<=mid)
        findminn(l,r,2*k);
    else if(l>mid)
        findminn(l,r,2*k+1);
    else
    {
        findminn(l,mid,2*k);
        findminn(mid+1,r,2*k+1);
    }
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        int a,b;
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&h[i]);
        }
        build(1,n,1);
        while(m--)
        {
            maxn = 0;
            minn = 99999999;
            scanf("%d%d",&a,&b);
            findmax(a,b,1);
            findminn(a,b,1);
            printf("%d\n",maxn-minn);
        }
    }
    return 0;
}