题意:给你一组值,然后询问某个区间的最大值和最小值得差
分析:因为没有更新,所以只需要查找即可,节点保存一个最大值最小值就行了
******************************************************************
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define Lson root<<1,L,tree[root].Mid()
#define Rson root<<1|1,tree[root].Mid()+1,R
const int maxn = 50005;
const int oo = 0xfffffff;
struct Tree
{
int L, R, MaxH, MinH;
int Mid(){return (L+R)/2;}
}tree[maxn*4];
int High[maxn], Max, Min;
void Build(int root, int L, int R)
{
tree[root].L = L, tree[root].R = R;
if(L == R)
{
tree[root].MaxH = tree[root].MinH = High[L];
return ;
}
Build(Lson);
Build(Rson);
tree[root].MaxH = max(tree[root<<1].MaxH, tree[root<<1|1].MaxH);
tree[root].MinH = min(tree[root<<1].MinH, tree[root<<1|1].MinH);
}
void Query(int root, int L, int R)
{
if(tree[root].L == L && tree[root].R == R)
{
Max = max(tree[root].MaxH, Max);
Min = min(tree[root].MinH, Min);
return ;
}
if(R <= tree[root].Mid())
Query(root<<1, L, R);
else if(L > tree[root].Mid())
Query(root<<1|1, L, R);
else
{
Query(Lson);
Query(Rson);
}
}
int main()
{
int N, M;
while(scanf("%d%d", &N, &M) != EOF)
{
int i, l, r;
for(i=1; i<=N; i++)
scanf("%d", &High[i]);
Build(1, 1, N);
while(M--)
{
Max = -oo, Min = oo;
scanf("%d%d", &l, &r);
Query(1, l, r);
printf("%d\n", Max-Min);
}
}
return 0;
}