Balanced Lineup

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头牛,输入每头牛的高度,再给定m次查询,查询每个区间中的最大高度差。

下面给出两种方法:
1、线段树版

#include<iostream>
#include<cstdio>
using namespace std;
const int maxx=50010;
struct node
{
int l,r;
int lch,rch;
int maxn,minn;
}tree[maxx*4];
int n,m,num,a[maxx];
int init()
{
int p=0;char c=getchar();
while(c<'0'||c>'9')
c=getchar();
while(c>='0'&&c<='9')
{
p=p*10+c-'0';
c=getchar();
}
return p;
}
void build_tree(int ll,int rr)
{
int cur=++num;
tree[cur].l=ll;
tree[cur].r=rr;
if(ll!=rr-1)
{
tree[cur].lch=num+1;
build_tree(ll,(ll+rr)/2);
tree[cur].rch=num+1;
build_tree((ll+rr)/2,rr);
tree[cur].maxn=max(tree[tree[cur].lch].maxn,tree[tree[cur].rch].maxn);
tree[cur].minn=min(tree[tree[cur].lch].minn,tree[tree[cur].rch].minn);
}
else tree[cur].maxn=tree[cur].minn=a[ll];
}
int find_max(int k,int ll,int rr)
{
if(tree[k].l>=ll&&tree[k].r<=rr)
return tree[k].maxn;
int ans=-1;
if(ll<(tree[k].l+tree[k].r)/2) ans=max(ans,find_max(tree[k].lch,ll,rr));
if(rr>(tree[k].l+tree[k].r)/2) ans=max(ans,find_max(tree[k].rch,ll,rr));
return ans;
}
int find_min(int k,int ll,int rr)
{
if(tree[k].l>=ll&&tree[k].r<=rr)
return tree[k].minn;
int ans=10000000;
if(ll<(tree[k].l+tree[k].r)/2) ans=min(ans,find_min(tree[k].lch,ll,rr));
if(rr>(tree[k].l+tree[k].r)/2) ans=min(ans,find_min(tree[k].rch,ll,rr));
return ans;
}
int main()
{
int l,r;
cin>>n>>m;
for(int i=1;i<=n;i++)
a[i]=init();
build_tree(1,n+1);
for(int i=1;i<=m;i++)
{
l=init();r=init();
cout<<find_max(1,l,r+1)-find_min(1,l,r+1)<<endl;
}
return 0;
}

2、RMQ的st表算法:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int maxx=50010;
int n,m,f[maxx][20],maxn[maxx][20],minn[maxx][20];
int l,r,x,y;
int init()
{
int p=0;char c=getchar();
while(c<'0'||c>'9')
c=getchar();
while(c>='0'&&c<='9')
{
p=p*10+c-'0';
c=getchar();
}
return p;
}
void prepare()
{
for(int j=1;j<=19;j++)
for(int i=1;i+(1<<j)-1<=n;i++)
{
maxn[i][j]=max(maxn[i][j-1],maxn[i+(1<<j-1)][j-1]);
minn[i][j]=min(minn[i][j-1],minn[i+(1<<j-1)][j-1]);
}
}
int main()
{
memset(maxn,-1,sizeof(maxn));
memset(minn,127/3,sizeof(minn));
n=init();m=init();
for(int i=1;i<=n;i++)
{
f[i][0]=init();
maxn[i][0]=minn[i][0]=f[i][0];
}
prepare();
for(int i=1;i<=m;i++)
{
l=init();r=init();
int k=double(log(r-l+1.0))/double(log(2.0));
x=max(maxn[l][k],maxn[r-(1<<k)+1][k]);
y=min(minn[l][k],minn[r-(1<<k)+1][k]);
cout<<x-y<<endl;
}
return 0;
}

分析:
st表算法适合询问次数较多的情况,而且代码长度比线段树要短,使用较方便。线段树代码长度较长,但是功能上完爆st表。