题目链接: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. python学习笔记:网络请求——requests模块

    上面讲过的urllib模块太麻烦了,还有一个比较方便的模块,就是requests模块,好用到你怀疑人生·^_^,一定要会哦 需要安装,pip install requests即可,下面是request ...

  2. 第四周总结和实验二Java简单类与对象

    实验目的 掌握类的定义,熟悉属性.构造函数.方法的使用,掌握用类作为类型声明变量和方法返回值: 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实列的方法和属性: 理解static修饰对类. ...

  3. Qt5.2中使用ping命令实现Ip扫描功能

    在实现类似于Free IP Scanner 2.1的Ip扫描器软件中,会用到ping命令.如果使用Qt编程实现,主要会用QThread.QProcess这两个类.关于这两个类的具体用法可以查阅Qt助手 ...

  4. python-并发编程之进程

    进程 python中创建进程模块为:multiprocessing 开销非常大 是计算机中资源分配的最小单位(内存隔离) 能利用多个CPU 由操作系统控制 同时操作内存之外的数据会产生数据的不安全 进 ...

  5. @Validated和@Valid区别:Spring validation验证框架对入参实体进行嵌套验证必须在相应属性(字段)加上@Valid而不是@Validated

    Spring Validation验证框架对参数的验证机制提供了@Validated(Spring's JSR-303规范,是标准JSR-303的一个变种),javax提供了@Valid(标准JSR- ...

  6. 5、springcloud整合mybatis注解方式

    1.上一篇学习了服务提供者provider,但是并不是单单就学习了服务提供者.中间还穿插使用了Hikari数据源和spring cloud整合mybatis.但是上篇使用mybatis时还是沿用了老的 ...

  7. Vue项目引入sass

    最近两天手头的事情暂时搞完了,可以抽出空来学习一下东西,之前项目都是鹏哥搭建好了,我们在直接在里面写代码,sass语法用来写样式还是比较方便常用的,今天就来试试怎么引入和配置sass 参考文章:Vue ...

  8. Recall,Precision,ROC曲线的介绍

    https://www.jianshu.com/p/f154237924c4 (ROC讲解) https://blog.csdn.net/saltriver/article/details/74012 ...

  9. BZOJ 1576: [Usaco2009 Jan]安全路经Travel

    日常自闭半小时后看题解,太弱了qwq. 感觉这道题还是比较难的,解法十分巧妙,不容易想到. 首先题目说了起点到每个点的最短路都是唯一的,那么对这个图求最短路图必定是一棵树,而且这棵树是唯一的. 那么我 ...

  10. Android 6.0 - 动态权限管理的解决方案(转)

    转自:http://www.cnblogs.com/dubo-/p/6018262.html Android 6.0 - 动态权限管理的解决方案   转载请标注 Android 6.0版本(Api 2 ...