• 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;
    }
    

      

  • 相关阅读:
    Codeforces Round 546 (Div. 2)
    Codeforces Round 545 (Div. 2)
    Codeforces Round 544(Div. 3)
    牛客小白月赛12
    Codeforces Round 261(Div. 2)
    Codeforces Round 260(Div. 2)
    Codeforces Round 259(Div. 2)
    Codeforces Round 258(Div. 2)
    Codeforces Round 257 (Div. 2)
    《A First Course in Probability》-chaper5-连续型随机变量-随机变量函数的分布
  • 原文地址:https://www.cnblogs.com/PersistFaith/p/4808128.html
Copyright © 2020-2023  润新知