Balanced Lineup(线段树)

来源:互联网 发布:sql update多个值 编辑:程序博客网 时间:2024/05/22 15:29
A - Balanced Lineup
Time Limit:5000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u
SubmitStatusPracticePOJ 3264

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 andQ.
Lines 2.. N+1: Line i+1 contains a single integer that is the height of cowi
Lines N+2.. N+ Q+1: Two integers A and B (1 ≤ABN), 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.

Sample Input

6 31734251 54 62 2

Sample Output

630#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;const int MAXN = 50500;int hei[MAXN];struct Tree{    int l, r, ma, mi;} tree_a[MAXN*4], tree_i[MAXN*4];void bulid_ma(int root, int l, int r){    tree_a[root].l = l;    tree_a[root].r = r;    if(tree_a[root].r == tree_a[root].l)    {        tree_a[root].ma = hei[l];        return;    }    int mid = (l+r)/2;    bulid_ma(root<<1, l, mid);    bulid_ma(root<<1|1, mid+1, r);    tree_a[root].ma = max(tree_a[root<<1].ma, tree_a[root<<1|1].ma);}void bulid_mi(int root, int l, int r){    tree_i[root].l = l;    tree_i[root].r = r;    if(tree_i[root].r == tree_i[root].l)    {        tree_i[root].mi = hei[l];        return;    }    int mid = (l+r)/2;    bulid_mi(root<<1, l, mid);    bulid_mi(root<<1|1, mid+1, r);    tree_i[root].mi = min(tree_i[root<<1].mi, tree_i[root<<1|1].mi);}int query(int root, int l, int r, char f){    if( 'a' == f )    {        if(l<=tree_a[root].l && r>=tree_a[root].r)        {            return tree_a[root].ma;        }        int mid = (tree_a[root].l+tree_a[root].r)/2, ret = 0;        if(l<=mid) ret = max(ret,query(root<<1, l, r, f));        if(r>mid) ret = max(ret,query(root<<1|1,l, r, f));        return ret;    }    else if( 'i' == f )    {        if(l<=tree_i[root].l && r>=tree_i[root].r)        {            return tree_i[root].mi;        }        int mid = (tree_i[root].l+tree_i[root].r)/2, ans = 100000000;        if(l<=mid) ans = min(ans,query(root<<1, l, r, f));        if(r>mid) ans = min(ans,query(root<<1|1, l, r, f));        return ans;    }}int main(){    int n, q;    scanf("%d%d", &n, &q);    memset(hei, 0, sizeof(hei));    memset(tree_a, 0, sizeof(tree_a));    memset(tree_i, 0, sizeof(tree_i));    for(int i=1; i<=n; i++ )    {        scanf("%d", &hei[i]);    }    bulid_ma(1, 1, n);    bulid_mi(1, 1, n);    int r, l;    for(int i=0; i<q; i++ )    {        scanf("%d%d", &l, &r);        printf("%d\n", query(1, l, r, 'a') - query(1, l, r, 'i'));    }    return 0;}哎, 我就是个马大哈。。。
0 0
原创粉丝点击