ACM学习历程—HDU 5443 The Water Problem(RMQ)(2015长春网赛1007题)

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,...,an representing 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(T≤10) indicating the number of test cases. For each test case, there is a number n(0≤n≤1000) on a line representing the number of water sources. n integers follow, respectively a1,a2,a3,...,an , and each integer is in {1,...,10^6} . On the next line, there is a number q(0≤q≤1000) representing the number of queries. After that, there will be q lines with two integers l and r(1≤l≤r≤n) 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

题目大意就是给定区间,求区间最值。

这里采用了RMQ的ST算法,直接套的模板。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

const int maxN = 1005;
int n, q;
int a[maxN];
int ma[maxN][20];

void RMQ()
{
    memset(ma, 0, sizeof(ma));
    for (int i = 0; i < n; ++i)
        ma[i][0] = a[i];
    for (int j = 1; (1<<j) <= n; ++j)
        for (int i = 0; i+(1<<j)-1 < n; ++i)
            ma[i][j] = max(ma[i][j-1], ma[i+(1<<(j-1))][j-1]);
}

int query(int lt, int rt)
{
    int k = 0;
    while ((1<<(k+1)) <= rt-lt+1)
        k++;
    return max(ma[lt][k], ma[rt-(1<<k)+1][k]);
}

void input()
{
    scanf("%d", &n);
    for (int i = 0; i < n; ++i)
        scanf("%d", &a[i]);
    RMQ();
}

void work()
{
    scanf("%d", &q);
    int u, v, ans;
    for (int i = 0; i < q; ++i)
    {
        scanf("%d%d", &u, &v);
        ans = query(u-1, v-1);
        printf("%d\n", ans);
    }
}

int main()
{
    //freopen("test.in", "r", stdin);
    int T;
    scanf("%d", &T);
    for (int times = 0; times < T; ++times)
    {
        input();
        work();
    }
    return 0;
}
时间: 2024-12-18 03:10:13

ACM学习历程—HDU 5443 The Water Problem(RMQ)(2015长春网赛1007题)的相关文章

ACM学习历程—HDU 5446 Unknown Treasure(数论)(2015长春网赛1010题)

Problem Description On the way to the next secret treasure hiding place, the mathematician discovered a cave unknown to the map. The mathematician entered the cave because it is there. Somewhere deep in the cave, she found a treasure chest with a com

ACM学习历程—HDU 5025 Saving Tang Monk(广州赛区网赛)(bfs)

Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classical Novels of Chinese literature. It was written by Wu Cheng'en during the Ming Dynasty. In this novel, Monkey King Sun Wukong, pig Zhu Bajie and Sha Wujin

ACM学习历程—HDU 5451 Best Solver(Fibonacci数列 &amp;&amp; 快速幂)(2015长春网赛1007题)

Problem Description The so-called best problem solver can easily solve this problem, with his/her childhood sweetheart. It is known that y=(5+2√6)^(1+2^x).For a given integer x (0≤x<2^32) and a given prime number M (M≤46337) , print [y]%M . ([y] mean

ACM学习历程——HDU5017 Ellipsoid(模拟退火)(2014西安网赛K题)

---恢复内容开始--- Description Given a 3-dimension ellipsoid(椭球面) your task is to find the minimal distance between the original point (0,0,0) and points on the ellipsoid. The distance between two points (x 1,y 1,z 1) and (x 2,y 2,z 2) is defined as  Input

hdu 5443 (2015长春网赛G题 求区间最值)

求区间最值,数据范围也很小,因为只会线段树,所以套了线段树模板=.= Sample Input3110011 151 2 3 4 551 21 32 43 43 531 999999 141 11 22 33 3 Sample Output1002344519999999999991 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm>

hdu 5475 模拟计算器乘除 (2015上海网赛F题 线段树)

给出有多少次操作 和MOD 初始值为1 操作1 y 表示乘上y操作2 y 表示除以第 y次操作乘的那个数 线段树的叶子结点i 表示 第i次操作乘的数 将1替换成y遇到操作2 就把第i个结点的值 替换成1利用线段树的性质,对整个1~n的区间进行维护,每次输出sum[1]的值即可 Sample Input110 10000000001 22 11 21 102 32 41 61 71 122 7 Sample OutputCase #1:2122010164250484 1 # include <i

ACM学习历程—HDU 4726 Kia&#39;s Calculation( 贪心&amp;&amp;计数排序)

DescriptionDoctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so careless and alway forget to carry a number when the sum of two digits exceeds 9. For example, when she calculates 4567+5789, she will get 9246, and for 12

ACM学习历程—HDU 5023 A Corrupt Mayor&#39;s Performance Art(广州赛区网赛)(线段树)

Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a high price to someone who wants to bribe him/her on an auction, this seemed a safe way for mayor X to make money. Becaus

hdu 5443 The Water Problem(长春网络赛——暴力)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5443 The Water Problem Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1084    Accepted Submission(s): 863 Problem Description In Land waterless