zoukankan      html  css  js  c++  java
  • POJ 3264 Balanced Lineup

    题意:给出n个数,q次询问,每次询问一段区间输出区间内最大值和最小值的差。

    解法:线段树。拿两个线段树分别维护最大值和最小值。

    代码:

    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    #include<string>
    #include<string.h>
    #include<math.h>
    #include<limits.h>
    #include<time.h>
    #include<stdlib.h>
    #include<map>
    #include<queue>
    #include<set>
    #include<stack>
    #include<vector>
    #define LL long long
    using namespace std;
    #define lson l, m, rt << 1
    #define rson m + 1, r, rt << 1 | 1
    const int maxn = 50005;
    int Max[maxn << 2], Min[maxn << 2];
    int a[maxn];
    void pushUpMax(int rt)
    {
        Max[rt] = max(Max[rt << 1], Max[rt << 1 | 1]);
    }
    void buildMax(int l, int r, int rt)
    {
        if(l == r)
        {
            Max[rt] = a[l];
            return ;
        }
        int m = (l + r) >> 1;
        buildMax(lson);
        buildMax(rson);
        pushUpMax(rt);
    }
    void pushUpMin(int rt)
    {
        Min[rt] = min(Min[rt << 1], Min[rt << 1 | 1]);
    }
    void buildMin(int l, int r, int rt)
    {
        if(l == r)
        {
            Min[rt] = a[l];
            return ;
        }
        int m = (l + r) >> 1;
        buildMin(lson);
        buildMin(rson);
        pushUpMin(rt);
    }
    int queryMax(int ll, int rr, int l, int r, int rt)
    {
        if(ll <= l && rr >= r)
            return Max[rt];
        int m = (l + r) >> 1;
        int res = INT_MIN;
        if(ll <= m)
            res = max(res, queryMax(ll, rr, lson));
        if(rr > m)
            res = max(res, queryMax(ll, rr, rson));
        return res;
    }
    int queryMin(int ll, int rr, int l, int r, int rt)
    {
        if(ll <= l && rr >= r)
            return Min[rt];
        int m = (l + r) >> 1;
        int res = INT_MAX;
        if(ll <= m)
            res = min(res, queryMin(ll, rr, lson));
        if(rr > m)
            res = min(res, queryMin(ll, rr, rson));
        return res;
    }
    int main()
    {
        int n, q;
        while(~scanf("%d%d", &n, &q))
        {
            for(int i = 1; i <= n; i++)
                scanf("%d", &a[i]);
            buildMax(1, n, 1);
            buildMin(1, n, 1);
            for(int i = 0; i < q; i++)
            {
                int maxx, minn;
                int l, r;
                scanf("%d%d", &l, &r);
                maxx = queryMax(l, r, 1, n, 1);
                minn = queryMin(l, r, 1, n, 1);
                printf("%d
    ", maxx - minn);
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    当面对会反制遭破解装置的App该如何顺利提取数据
    管理信息系统的开发与管理
    加载静态文件,父模板的继承和扩展
    开始Flask项目
    夜间模式的开启与关闭,父模板的制作
    完成登录与注册页面的前端
    JavaScript 基础,登录验证
    CSS实例:图片导航块
    导航,头部,CSS基础
    web基础,用html元素制作web页面
  • 原文地址:https://www.cnblogs.com/Apro/p/4477509.html
Copyright © 2011-2022 走看看