Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 32359   Accepted: 15229
Case Time Limit: 2000MS

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

Source

USACO 2007 January Silver
 
线段树初步应用,构建线段树即可.
 
/*
* @author  Panoss
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<ctime>
#include<stack>
#include<queue>
#include<list>
using namespace std;
#define DBG 1
#define fori(i,a,b) for(int i = (a); i < (b); i++)
#define forie(i,a,b) for(int i = (a); i <= (b); i++)
#define ford(i,a,b) for(int i = (a); i > (b); i--)
#define forde(i,a,b) for(int i = (a); i >= (b); i--)
#define forls(i,a,b,n) for(int i = (a); i != (b); i = n[i])
#define mset(a,v) memset(a, v, sizeof(a))
#define mcpy(a,b) memcpy(a, b, sizeof(a))
#define dout  DBG && cerr << __LINE__ << " >>| "
#define checkv(x) dout << #x"=" << (x) << " | "<<endl
#define checka(array,a,b) if(DBG) { \
    dout << #array"[] | " << endl; \
    forie(i, a, b) cerr << "[" << i << "]=" << array[i] << " |" << ((i - (a)+1) % 5 ? " " : "\n"); \
if (((b)-(a)+1) % 5) cerr << endl; \
}
#define redata(T, x) T x; cin >> x
#define MIN_LD -2147483648
#define MAX_LD  2147483647
#define MIN_LLD -9223372036854775808
#define MAX_LLD  9223372036854775807
#define MAX_INF 18446744073709551615
inline int  reint() { int d; scanf("%d", &d); return d; }
inline long relong() { long l; scanf("%ld", &l); return l; }
inline char rechar() { scanf(" "); return getchar(); }
inline double redouble() { double d; scanf("%lf", &d); return d; }
inline string restring() { string s; cin >> s; return s; }

#define MAX_TREE_NODE 50000         ///树结点个数

struct Segment_Tree
{
    int L, R;                        ///左,右区间
    int min_value, max_value;        ///区间最小值,最大值;
    struct Segment_Tree * left;      ///左儿子
    struct Segment_Tree * right;     ///右儿子
};


Segment_Tree Tree[2 * MAX_TREE_NODE + 5];

int Tree_node_number = 1;                                   ///结点编号,1号为根结点

void Build_Tree(Segment_Tree * root, int left, int right )  ///建树
{
    root->L = left;
    root->R = right;
    root->max_value = MIN_LD;
    root->min_value = MAX_LD;
    root->left = NULL;
    root->right = NULL;
    if (left == right) return;
    Tree_node_number++;
    root->left = Tree + Tree_node_number;
    Tree_node_number++;
    root->right = Tree + Tree_node_number;
    Build_Tree(root->left, left, (left + right) / 2);
    Build_Tree(root->right, (left + right) / 2 + 1, right);
}

void Insert_node(Segment_Tree * root, int number, int value)   ///修改数据 a[number] = value;
{
    if (root->L == number && root->R == number)   ///叶子结点,直接修改,更新最大值最小值为value
    {
        root->max_value = root->min_value = value;
        return;
    }
    if (number <= (root->L + root->R) / 2)
    {
        Insert_node(root->left, number, value);
        root->max_value = max(root->max_value, root->left->max_value);
        root->min_value = min(root->min_value, root->left->min_value);
    }
    else
    {
        Insert_node(root->right, number, value);
        root->max_value = max(root->max_value, root->right->max_value);
        root->min_value = min(root->min_value, root->right->min_value);
    }
}


int Query_min_value(Segment_Tree * root, int i, int j)     ///查询区间i,j最小值
{
    if (root->L == i && root->R == j)
        return root->min_value;
    if (i > (root->L + root->R) / 2)
    {
        return  Query_min_value(root->right, i, j);
    }
    else if (j <= (root->L + root->R) / 2)
    {
        return Query_min_value(root->left, i, j);
    }
    else
    {
        return min(Query_min_value(root->left, i, (root->L + root->R) / 2), Query_min_value(root->right, (root->L + root->R) / 2 + 1, j));
    }
}

int Query_max_value(Segment_Tree * root, int i, int j)    ///查询区间i,j最大值
{
    if (root->L == i && root->R == j)
        return root->max_value;
    if (i > (root->L + root->R) / 2)
    {
        return  Query_max_value(root->right, i, j);
    }
    else if (j <= (root->L + root->R) / 2)
    {
        return Query_max_value(root->left, i, j);
    }
    else
    {
        return max(Query_max_value(root->left, i, (root->L + root->R) / 2), Query_max_value(root->right, (root->L + root->R) / 2 + 1, j));
    }
}

int main()
{
    //freopen("data.txt","r",stdin);
    int N, Q, A, B;
    cin>>N>>Q;
    Tree_node_number = 1;
    Build_Tree(Tree + 1,1,N);
    forie(j,1,N)
    {
        scanf("%d",&A);
        Insert_node(Tree+1,j,A);
    }

    forie(T,1,Q)
    {
        scanf("%d%d",&A,&B);
        printf("%d\n",Query_max_value(Tree+1,A,B)-Query_min_value(Tree+1,A,B));
    }
    return 0;
}

 

posted on 2014-05-22 22:16  Panoss  阅读(136)  评论(0编辑  收藏  举报