Balanced Lineup
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 77030 Accepted: 35276
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

题目大意:
有n头牛,现在给你每头牛i的身高(1<=i<=n),有m条询问,问你在[x,y]区间内身高的最大值-最小值等于多少?

分析:O(n)的算法肯定T了,我们要O(logn)的算法,这里采用线段树维护max,min时间复杂度O(logn)

思路:
1:针对此题,题目中只有查询,没有更新。
2:查询是以区间为单位,也就是说是区间查询。
3:有效信息是最大值和最小值,也就是说我们要维护的数据就是最大值和最小值。
我们只需要在结构体中新增一个最大值和最小值,建树的时候顺便求一下。
这题关键难在区间查询中,假如n=6,也就是说区间范围[1,6],根据我画的图:

我们可以很轻松的求出[1,6],[1,3],[1,2],[1,1],[2,2],[3,3],[4,6],[4,5],[,4,4],[5,5],[6,6]这些区间的最大值和最小值然后做减法就是答案了,但是我们要求[1,5],[3,6],[1,4]等这些分叉的区间就不太好求了。针对这些问题,我另外写了两个函数求max和min的然后对分叉区间求max和min,然后做减法就是答案了,具体看代码吧:

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;

typedef long long ll;
const int maxn=1<<19;
struct segment{
	ll l,r;
	ll _max,_min;
};
struct segment Node[maxn];
void build(ll i,ll l,ll r){
	Node[i].l=l;
	Node[i].r=r;
	if(l==r){
		scanf("%lld",&Node[i]._max);
		Node[i]._min=Node[i]._max;
		return ;
	}
	ll mid=(l+r)>>1;
	build(i<<1,l,mid);
	build(i<<1|1,mid+1,r);
	Node[i]._max=max(Node[i<<1]._max,Node[i<<1|1]._max);
	Node[i]._min=min(Node[i<<1]._min,Node[i<<1|1]._min);
}
ll _query1(ll i,ll l,ll r){
	if(Node[i].l==l&&Node[i].r==r){
		return Node[i]._max; 
	}
	ll mid=(Node[i].l+Node[i].r)>>1;
	if(r<=mid){
		return _query1(i<<1,l,r);
	}else if(l>mid){
		return _query1(i<<1|1,l,r);
	}else{
		return max(_query1(i<<1,l,mid),_query1(i<<1|1,mid+1,r));
	}
}
ll _query2(ll i,ll l,ll r){
	if(Node[i].l==l&&Node[i].r==r){
		return Node[i]._min; 
	}
	ll mid=(Node[i].l+Node[i].r)>>1;
	if(r<=mid){
		return _query2(i<<1,l,r);
	}else if(l>mid){
		return _query2(i<<1|1,l,r);
	}else{
		return min(_query2(i<<1,l,mid),_query2(i<<1|1,mid+1,r));
	}
}
ll query(ll i,ll l,ll r){
	if(Node[i].l==l&&Node[i].r==r){
		return Node[i]._max-Node[i]._min;
	}
	ll mid=(Node[i].l+Node[i].r)>>1;
	if(r<=mid){
		return query(i<<1,l,r);
	}else if(l>mid){
		return query(i<<1|1,l,r);
	}else{
		ll __max=max(_query1(i<<1,l,mid),_query1(i<<1|1,mid+1,r));
		ll __min=min(_query2(i<<1,l,mid),_query2(i<<1|1,mid+1,r));
		return __max-__min;
	}
}
int main(){
	ll n,q,x,y;
	scanf("%lld%lld",&n,&q);
	build(1,1,n);
	while(q--){
		scanf("%lld%lld",&x,&y);
		printf("%lld\n",query(1,x,y));
	}
}