• G


    题意:给你一组值,然后询问某个区间的最大值和最小值得差
    分析:因为没有更新,所以只需要查找即可,节点保存一个最大值最小值就行了
    ******************************************************************
    #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(11, N);

            while(M--)
            {
                Max = -oo, Min = oo;
                scanf("%d%d", &l, &r);
                Query(1, l, r);

                printf("%d ", Max-Min);
            }
        }

        return 0; 

    }

  • 相关阅读:
    【光学相机】关于网络通信的方法及函数
    InvokeRequired与Invoke
    线程基本概念
    线程同步与线程异步
    AutoResetEvent和ManualResetEvent
    C#图像亮度调式与伪彩色图的处理
    Github文件高速下载方法
    JDK11以上版本没有JRE的解决方法
    mysql 随机函数生成某个范围内的整数
    mysql主键id重置
  • 原文地址:https://www.cnblogs.com/liuxin13/p/4678570.html
Copyright © 2020-2023  润新知