题目链接:HDU 5443

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 \(a_1,a_2,a_3,...,a_n\) 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 \(a_l\) and \(a_r\).

Input

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

Source

2015 ACM/ICPC Asia Regional Changchun Online

Solution

题意

给定 \(n\) 个数,\(q\) 个询问,每个询问包含 \(l\) 和 \(r\),求区间 \([l, r]\) 内的最大值。

思路

ST算法

\(RMQ\) 问题。ST 算法模板题。预处理时间 \(O(nlogn)\),查询时间 \(O(1)\)。

Code

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010; int a[maxn];
int f[maxn][11];
int n; void st_prework() {
for(int i = 1; i <= n; ++i) f[i][0] = a[i];
int t = log(n) / log(2) + 1;
for(int j = 1; j < t; ++j) {
for(int i = 1; i <= n - (1 << j) + 1; ++i) {
f[i][j] = max(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
}
}
} int st_query(int l, int r) {
int k = log(r - l + 1) / log(2);
return max(f[l][k], f[r - (1 << k) + 1][k]);
} int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while(T--) {
cin >> n;
for(int i = 1; i <= n; ++i) {
cin >> a[i];
}
st_prework();
int q;
cin >> q;
for(int i = 0; i < q; ++i) {
int l, r;
cin >> l >> r;
cout << st_query(l, r) << endl;
}
}
return 0;
}

HDU 5443 The Water Problem (ST算法)的更多相关文章

  1. hdu 5443 The Water Problem

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5443 The Water Problem Description In Land waterless, ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5443 The Water Problem Time Limit: 1500/1000 MS (Java ...

  3. hdu 5443 The Water Problem 线段树

    The Water Problem Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...

  4. 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 ...

  5. 【线段树】HDU 5443 The Water Problem

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5443 题目大意: T组数据.n个值,m个询问,求区间l到r里的最大值.(n,m<=1000) ...

  6. HDU 5443 The Water Problem (水题,暴力)

    题意:给定 n 个数,然后有 q 个询问,问你每个区间的最大值. 析:数据很小,直接暴力即可,不会超时,也可以用RMQ算法. 代码如下: #include <cstdio> #includ ...

  7. HDU 5832 A water problem(某水题)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  8. HDU 5832 A water problem (带坑水题)

    A water problem 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5832 Description Two planets named H ...

  9. HDU 5832 A water problem 水题

    A water problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5832 Description Two planets named H ...

随机推荐

  1. FFMpeg视频解码初探

    在视频解码前,先了解以下几个基本的概念: 编解码器(CODEC):能够进行视频和音频压缩(CO)与解压缩(DEC),是视频编解码的核心部分. 容器/多媒体文件(Container/File):没有了解 ...

  2. Java面试题一览

    Java面试题一览

  3. 用 Flask 来写个轻博客 (15) — M(V)C_实现博文页面评论表单

    目录 目录 前文列表 实现 post 视图函数 在 posthtml 中添加表单 效果 前文列表 用 Flask 来写个轻博客 (1) - 创建项目 用 Flask 来写个轻博客 (2) - Hell ...

  4. C# winform 动态构建fastreport报表

    private void DoPrint() { DataView dv = (DataView)dgv_apply_details.DataSource; Report report = new R ...

  5. 用processing生成屏保程序

    想法 利用随机数控制圆圈的大小.位置以及颜色,可以产生随机的美感. 让小球动起来,并且在屏幕边界处产生反弹效果. 代码 1: float circle_x = (float) 0.0; 2: floa ...

  6. activi7流程部署

    package com.zcc.acvitivi; import org.activiti.engine.ProcessEngine;import org.activiti.engine.Proces ...

  7. C#关键字扫盲——Tuple(元组类) 、ValueTuple(值元组)

    原文:C#关键字扫盲--Tuple(元组类) .ValueTuple(值元组) 版权声明:本文为博主原创文章,随意转载. https://blog.csdn.net/Michel4Liu/articl ...

  8. MVC中的自定义标签分页控件,仅供大家学习!!

    public static HtmlString ShowPageNavigate(this HtmlHelper htmlHelper, int currentPage, int pageSize, ...

  9. 使用sass

    sass安装 compass安装 1.sass 需要安装ruby,然后通过gem安装sass. 2. less有想=两种安装方: 客户端安装: 引入less.js,然后就可以直接用.less文件 &l ...

  10. Android开发——回调(Callback)

    1. 回调函数的定义: 在A类中定义了一个方法,这个方法中用到了一个接口和该接口中的抽象方法,但是抽象方法没有具体的实现,需要B类去实现,B类实现该方法后,它本身不会去调用该方法,而是传递给A类,供A ...