zoukankan      html  css  js  c++  java
  • hdu5443 The Water Problem(水)

    Problem Description
    In Land waterless, water is a very limited resource. People always fight for the biggest source of water. Given a sequence of water sources with a1,a2,a3,...,anrepresenting the size of the water source. Given a set of queries each containing 2 integers l and r, please find out the biggest water source between al and ar.
     
    Input
    First you are given an integer T(T10) indicating the number of test cases. For each test case, there is a number n(0n1000) on a line representing the number of water sources. n integers follow, respectively a1,a2,a3,...,an, and each integer is in {1,...,106}. On the next line, there is a number q(0q1000) representing the number of queries. After that, there will be q lines with two integers l and r(1lrn) indicating the range of which you should find out the biggest water source.
     
    Output
    For each query, output an integer representing the size of the biggest water source.
     
    Sample Input

    3
    1
    100
    1
    1 1
    5
    1 2 3 4 5
    5
    1 2
    1 3
    2 4
    3 4
    3 5
    3
    1 999999 1
    4
    1 1
    1 2
    2 3
    3 3

    Sample Output
    100
    2
    3
    4
    4
    5
    1
    999999
    999999
    1

    区间最大值   线段树

    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    const int oo = 0x3f3f3f3f;
    const int N = 1005;
    typedef long long LL;
    struct da
    {
        int left, right, val;
    }as[N*4];
    int n, ac[N];
    void build(int left, int right, int i)
    {
        as[i].left = left;
        as[i].right = right;
        int mid = (left + right)/2;
        if(left == right)
        {
            as[i].val = ac[left];
            return ;
        }
        build(left, mid, 2*i);
        build(mid+1, right, 2*i+1);
        as[i].val = max(as[i*2].val, as[2*i+1].val);
    }
    void query(int left, int right, int i, int &ans)
    {
        if(as[i].left == left && as[i].right == right)
        {
            ans = max(as[i].val, ans);
            return ;
        }
        int mid = (as[i].left + as[i].right)/2;
        if(right <= mid)  query(left, right, 2*i, ans);
        else if(left > mid) query(left, right, 2*i+1, ans);
        else
        {
            query(left, mid, 2*i, ans);
            query(mid+1, right, 2*i+1, ans);
        }
    }
    int main()
    {
        int T, Q;
        scanf("%d", &T);
        while(T--)
        {
            scanf("%d", &n);
            for(int i = 1; i <= n; i++)
                scanf("%d", &ac[i]);
            build(1, n, 1);
            scanf("%d", &Q);
            while(Q--)
            {
                int a, b;
                scanf("%d %d", &a, &b);
                int ans = 0;
                query(a, b, 1, ans);
                printf("%d
    ", ans);
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    Spring中的destroy-method方法
    github Pull Request合入全流程介绍
    vue-router动态路由设置参数可选
    github上fork原项目,如何将本地仓库代码更新到最新版本?
    github的pull Request使用
    vue2.X版本vue-cli生成项目后运行失败,报错信息为getaddrinfo ENOTFOUND localhost
    npm在linux即mac下更新时报错
    数组Array和字符串String的indexOf方法,以及ES7(ES2016)中新增的Array.prototype.includes方法
    less的使用总结
    ssh连接github连不上
  • 原文地址:https://www.cnblogs.com/PersistFaith/p/4808128.html
Copyright © 2011-2022 走看看