zoukankan      html  css  js  c++  java
  • 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; 

    }

  • 相关阅读:
    Eclipse安装flex插件
    Free download SKP900 update tool & activation tool
    How to installation V145 Renault CAN Clip diagnostic software
    How to setup ELM327 Bluetooth WiFi for Android software Torque
    How to make remote key fob for 2002 BMW 3 series
    2015 FVDI V6.3 Software Free Download
    How to solve the SVDI SN Number Display Problem
    Tested work with China Digiprog 3 4.94 mileage programmer
    Autel MaxiDAS DS708 Fatal Application Error illegal operation
    Remap BMW F11 2010 all ECUs with E-Sys and ENET cable
  • 原文地址:https://www.cnblogs.com/liuxin13/p/4678570.html
Copyright © 2011-2022 走看看