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学习笔记7.自动装配(补充)
    [转]JSON 入门指南
    [转]spring学习笔记补: 生命周期
    rownum的用法
    简易计算器实现
    Flush the AOS cache from code
    通过Batch发送Report
    Export excel file format
    The report's image auto resizes (Always use in report)
    Save the users last values in a dialog
  • 原文地址:https://www.cnblogs.com/PersistFaith/p/4808128.html
Copyright © 2011-2022 走看看