G - Balanced Lineup - poj3264(区间查询)

时间:2023-03-09 00:57:26
G - Balanced Lineup - poj3264(区间查询)
题意:给你一组值,然后询问某个区间的最大值和最小值得差
分析:因为没有更新,所以只需要查找即可,节点保存一个最大值最小值就行了
******************************************************************
#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 = ;
const int oo = 0xfffffff; struct Tree
{
    int L, R, MaxH, MinH;
    int Mid(){return (L+R)/;}
}tree[maxn*];
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<<].MaxH, tree[root<<|].MaxH);
    tree[root].MinH = min(tree[root<<].MinH, tree[root<<|].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<<, L, R);
    else if(L > tree[root].Mid())
        Query(root<<|, 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=; i<=N; i++)
            scanf("%d", &High[i]);
        Build(, , N);         while(M--)
        {
            Max = -oo, Min = oo;
            scanf("%d%d", &l, &r);
            Query(, l, r);             printf("%d\n", Max-Min);
        }
    }     return ; }