P2880 [USACO07JAN]Balanced Lineup G(树状数组维护序列最值)

题目描述

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 ≤ 180,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.
每天,农夫 John 的n(1≤n≤5×104 ) 头牛总是按同一序列排队。
有一天, John 决定让一些牛们玩一场飞盘比赛。他准备找一群在对列中为置连续的牛来进行比赛。但是为了避免水平悬殊,牛的身高不应该相差太大。John 准备了q(1≤q≤1.8×105) 个可能的牛的选择和所有牛的身高hi(1≤hi​≤106,1≤i≤n)。他想知道每一组里面最高和最低的牛的身高差。

输入格式

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.
第一行两个数 n,q。
接下来 n 行,每行一个数 hi.
再接下来 q 行,每行两个整数 a 和 b,表示询问第 a 头牛到第 b 头牛里的最高和最低的牛的身高差。

输出格式

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.
输出共 q 行,对于每一组询问,输出每一组中最高和最低的牛的身高差。

样例

输入
6 3
1
7
3
4
2
5
1 5
4 6
2 2
输出
6
3
0

题目分析

这个题需要维护一段序列的最值,这个操作其实也可以用树状数组来维护。
我们可以用 trx[] 和 trn[] 分别维护序列的最大值和最小值。其原理和正常的树状数组是一样的,但是求原来的存和变为了存对应几个位置上的最值
add操作只要从原来的加和改为求最值即可(如:trx[i]=max(trx[i],c))。

主要的问题还是如何通过这个树状数组找出一段序列的最值:add操作可以直接修改原来的模板,但是求最大值与最小值不行。因为他没有像求区间和那样的性质。
这里分为两种情况:

  1. r-lowbit(r)>l; 这种情况下,我们可以把求[l,r]区间的最值拆分成两部分,先求[ l,r−lowbit( r ) ]中最值与[ r−lowbit( r )+1,r ]中的最值,再求这两者的最值。
    这时我们可以发现 [ r-lowbit( r )+1,r ] 的最值就是tr[r]
    于是,问题就转化为:求 [l,r-lowbit( r )]中最值与tr[r]的最值。
  2. r-lowbit(r)<=l ; 在这种情况下,我们可以直接把 a[r]分出来,于是原问就变成了:求 [l,r-1]中最值与 a[r] 的最值。
  3. 递归到某一层时,l=r,这个时候返回a[l]就行了。(毕竟l=r时,区间[l,r]就只有a[l]这一个数据了)

这样序列的最值也就求出来了。

代码如下
#include 
#include 
#include 
#define LL long long
using namespace std;
const int N=5e4+5;
int n,m;
int a[N];
int trx[N],trn[N];
int lowbit(int x)
{
    return x & -x;
}
void add(int x,int c)		//加入元素
{
    for(int i=x;i<=n;i+=lowbit(i))
    {
        trx[i]=max(trx[i],c);
        trn[i]=min(trn[i],c);
    }
}
int findmax(int l,int r)	//寻找区间中的最大值
{
    if(l==r) return a[l];
    if(r-lowbit(r)>l) return max(trx[r],findmax(l,r-lowbit(r)));	//上面说的两种情况
    else return max(a[r],findmax(l,r-1));
}
int findmin(int l,int r)	//寻找区间中的最小值
{
    if(l==r) return a[l];
    if(r-lowbit(r)>l) return min(trn[r],findmin(l,r-lowbit(r)));
    else return min(a[r],findmin(l,r-1));
}
int main()
{
    memset(trn,0x3f,sizeof trn);	//一开始trn[]要初始化为正无穷
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        add(i,a[i]);
    }
    while(m--)
    {
        int l,r;
        scanf("%d %d",&l,&r);
        printf("%d\n",findmax(l,r)-findmin(l,r));
    }
    return 0;
}

你可能感兴趣的:(洛谷,树状数组)