分享

Balanced Lineup

 印度阿三17 2019-08-02


上面链接是有关线段树的感觉描述的特别简单易懂,我怕博主不让直接转载,所以就把他的博客链接 转了过来*-**-*

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

 题意:每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 
             决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛. 
             但是为了避免水平悬殊,牛的身高不应该相差太大. 
            John 准备了Q (1 <= Q <= 180,000) 个可能的牛的选择和所有牛的身高 (1 <= 
             身高 <= 1,000,000). 他想知道每一组里面最高和最低的牛的身高差别. 

 简单的线段树模板。。。。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;
int sum[50010*4],sum1[50010*4],a;
void build(int l,int r,int o)
{
    if(l==r)
    {
        scanf("%d",&a);
        sum[o]=a;//****
        sum1[o]=a;//*****
        return ;
    }
    int mid=(l r)>>1;
    build(l,mid,o<<1);
    build(mid 1,r,o<<1|1);
    sum[o]=max(sum[o<<1],sum[o<<1|1]);
    sum1[o]=min(sum1[o<<1],sum1[o<<1|1]);//>>>>>>>>>>>>
}
int maxquery(int x,int y,int l,int r,int o)
{
    if(l>=x&&r<=y)//----
        return sum[o];
    int t1=0,t2=0;
    int mid=(l r)>>1;
    if(x<=mid)
        t1=maxquery(x,y,l,mid,o<<1);
    if(y>mid)//>>>>>>>>>>>
        t2=maxquery(x,y,mid 1,r,o<<1|1);
    return max(t1,t2);

}
int minquery(int x,int y,int l,int r,int o)
{
    if(l>=x&&r<=y)
        return sum1[o];
    int t1=999999999,t2=999999999;
    int mid=(l r)>>1;
    if(x<=mid)
        t1=minquery(x,y,l,mid,o<<1);
    if(y>mid)//>>>>>>>>>>>
        t2=minquery(x,y,mid 1,r,o<<1|1);
    return min(t1,t2);
}
int main()
{
    int n,q;
    while(~scanf("%d%d",&n,&q))
    {
        memset(sum,0,sizeof(sum));
        memset(sum1,0,sizeof(sum1));
        build(1,n,1);
        int l,r;
        while(q--)
        {
            scanf("%d%d",&l,&r);
            printf("%d\n",maxquery(l,r,1,n,1)-minquery(l,r,1,n,1));
        }
    }
    return 0;
}
来源:https://www./content-4-375251.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约