欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

POJ(3264):Balanced Lineup

程序员文章站 2022-04-08 09:14:15
...
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 64402   Accepted: 30015
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

题意:

给定Q (1 ≤ Q ≤ 200,000) 个数A 1 ,A 2 … A Q , ,多次求任一区间A i – A j 中最大数和最小数的差。

解题思路:

POJ(3264):Balanced Lineup

用一颗线段树把每个小区间内的最大最小值都记录下来,然后根据输入数据查询即可。

 

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
const int inf = 0xffffff0;
using namespace std;

struct node{
    int L,R;
    int minV,maxV;
    int mid(){
        return (L+R)/2;
    }
};
struct node tree[800010];
int minV = inf;
int maxV = -inf;

void build_tree(int root,int L,int R)//建树。每个节点为一个区间段,用来记录第i个输入的数,与输入数的大小无关!
{
    tree[root].L = L;
    tree[root].R = R;
    tree[root].minV = inf;   //标记区间段上的最大最小值。
    tree[root].maxV = -inf;
    if(L!=R){
        build_tree(2*root+1,L,(L+R)/2);
        build_tree(2*root+2,(L+R)/2+1,R);
    }
}

void Insert(int root,int i,int v){ //插入
    if(tree[root].L == tree[root].R){
        tree[root].maxV = tree[root].minV = v;
        return;
    }
    tree[root].maxV = max(tree[root].maxV,v);
    tree[root].minV = min(tree[root].minV,v);
    if(i <= tree[root].mid())
        Insert(2*root+1,i,v);
    else
        Insert(2*root+2,i,v);
}

void query(int root,int a,int b){
    if( tree[root].minV >= minV && tree[root].maxV <= maxV )
        return;
    if(tree[root].L==a && tree[root].R==b){
        maxV = max(tree[root].maxV,maxV);
        minV = min(tree[root].minV,minV);
        return;
    }
    if(b <= tree[root].mid()){  //如果要查找的区间全在该结点左边
        query(2*root+1,a,b);
    }
    else if(a > tree[root].mid()){//全在右边
        query(2*root+2,a,b);
    }
    else{ //包含两个区间
        query(2*root+1,a,tree[root].mid());
        query(2*root+2,tree[root].mid()+1,b);
    }
}

int main()
{
    int n,q;
    scanf("%d%d",&n,&q);
    build_tree(0,1,n);
    for(int i = 1;i <= n;i++){
        int v;
        scanf("%d",&v);
        Insert(0,i,v);
    }
    for(int i = 1;i <= q;i++){
        int a,b;
        scanf("%d%d",&a,&b);
        maxV = -inf; //每次都要更新
        minV = inf;
        query(0,a,b);
        printf("%d\n",maxV - minV);
    }
    return 0;
}

 

相关标签: POJ