Poj3264 Balanced Lineup 线段树基础题

来源:互联网 发布:阿里旅行软件 编辑:程序博客网 时间:2024/06/07 07:58

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 31734251 54 62 2

Sample Output

630

Source

USACO 2007 January Silver

求一个区间内最大值与最小值的差值,用线段树去维护即可,包括三个操作,建树,插入元素,查找。本题用数组来实现会比较方便。

对于线段树上任意一个结点,所需要记录的是这段区间内的最大最小值。

#include <iostream>#include <cstdio>#include <map>#include <set>#include <vector>#include <queue>#include <stack>#include <cmath>#include <algorithm>#include <cstring>#include <string>using namespace std;#define INF 0x3f3f3f3ftypedef long long LL;int maxn,minn;struct node{    int l,r;    int ma,mi;    int mid(){        return (l+r)/2;    }};node tree[800005];void buildtree(int root,int l,int r){    tree[root].l=l;    tree[root].r=r;    tree[root].mi=INF;    tree[root].ma=-INF;    if(l!=r){        buildtree(2*root+1,l,(l+r)/2);        buildtree(2*root+2,(l+r)/2+1,r);    }}void vinsert(int root,int i,int v){    if(tree[root].l==tree[root].r){        tree[root].mi=tree[root].ma=v;        return;    }    tree[root].ma=max(tree[root].ma,v);    tree[root].mi=min(tree[root].mi,v);    if(i<=tree[root].mid()){        vinsert(2*root+1,i,v);    }else{        vinsert(2*root+2,i,v);    }}void answersearch(int root,int l,int r){    if(tree[root].mi>=minn&&tree[root].ma<=maxn){        return;    }    if(tree[root].l==l&&tree[root].r==r){        minn=min(minn,tree[root].mi);        maxn=max(maxn,tree[root].ma);        return;    }    if(r<=tree[root].mid()){        answersearch(2*root+1,l,r);    }else if(l>tree[root].mid()){        answersearch(2*root+2,l,r);    }else{        answersearch(2*root+1,l,tree[root].mid());        answersearch(2*root+2,tree[root].mid()+1,r);    }}int main(){    int n,m,v,a,b;    scanf("%d%d",&n,&m);    buildtree(0,1,n);    for(int i=1;i<=n;i++){        scanf("%d",&v);        vinsert(0,i,v);    }    while(m--){        maxn=-INF;        minn=INF;        scanf("%d%d",&a,&b);        answersearch(0,a,b);        printf("%d\n",maxn-minn);    }    return 0;}



0 0