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 = ;
int n, q;
int a[maxN];
int ma[maxN][]; void RMQ()
{
memset(ma, , sizeof(ma));
for (int i = ; i < n; ++i)
ma[i][] = a[i];
for (int j = ; (<<j) <= n; ++j)
for (int i = ; i+(<<j)- < n; ++i)
ma[i][j] = max(ma[i][j-], ma[i+(<<(j-))][j-]);
} int query(int lt, int rt)
{
int k = ;
while ((<<(k+)) <= rt-lt+)
k++;
return max(ma[lt][k], ma[rt-(<<k)+][k]);
} void input()
{
scanf("%d", &n);
for (int i = ; i < n; ++i)
scanf("%d", &a[i]);
RMQ();
} void work()
{
scanf("%d", &q);
int u, v, ans;
for (int i = ; i < q; ++i)
{
scanf("%d%d", &u, &v);
ans = query(u-, v-);
printf("%d\n", ans);
}
} int main()
{
//freopen("test.in", "r", stdin);
int T;
scanf("%d", &T);
for (int times = ; times < T; ++times)
{
input();
work();
}
return ;
}

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

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

    Problem Description On the way to the next secret treasure hiding place, the mathematician discovere ...

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

    Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classi ...

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

    ---恢复内容开始--- Description Given a 3-dimension ellipsoid(椭球面) your task is to find the minimal distanc ...

  4. ACM学习历程—HDU 5289 Assignment(线段树 || RMQ || 单调队列)

    Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered fro ...

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

    求区间最值,数据范围也很小,因为只会线段树,所以套了线段树模板=.= Sample Input3110011 151 2 3 4 551 21 32 43 43 531 999999 141 11 2 ...

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

    给出有多少次操作 和MOD 初始值为1 操作1 y 表示乘上y操作2 y 表示除以第 y次操作乘的那个数 线段树的叶子结点i 表示 第i次操作乘的数 将1替换成y遇到操作2 就把第i个结点的值 替换成 ...

  7. hdu 5443 The Water Problem

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

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

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

  9. hdu 5443 The Water Problem 线段树

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

随机推荐

  1. 安卓编译出错: Process 'command 'C:\Java\jdk1.8.0_51\bin\java.exe'' finished with non-zero exit value 1 解决!

    安卓编译出错: Process 'command 'C:\Java\jdk1.8.0_51\bin\java.exe'' finished with non-zero exit value 1 解决! ...

  2. 我自己曾经经历的CMMI3认证通过关于软件测试的访谈【转载】

    转自<http://blog.csdn.net/jcy58/article/details/51908884> 因为当初我在公司里是负责软件测试工作的,所以CMMI3和测试相关的访谈,就是 ...

  3. 【BZOJ3791】作业 DP

    [BZOJ3791]作业 Description 众所周知,白神是具有神奇的能力的.比如说,他对数学作业说一声“数”,数学作业就会出于畏惧而自己完成:对语文作业说一声“语”,语文作业就会出于畏惧而自己 ...

  4. vue router-link子级返回父级页面

    vue-router嵌套路由,从二级路由跳转到一级路由时,间歇性导致一级路由重叠 解决方法: 1.使用this.$router.push跳转

  5. sharding-jdbc从入门到出门(03)

    经过端午节这2天对 sharding-jdbc一直怀揣成梦想的去学习,还是有一些没有解决的问题: 上一张图:

  6. 让lu哥头痛了许久的代码(洛谷:树的统计)

    错在单点修改时传的是a,应该是id[a](Line 89).谨记!!! //fushao zuishuai #include <cstdio> #include <cstring&g ...

  7. 我的Android进阶之旅------>android:drawableLeft的用法

    有时候想在EditText左边放一个图片,如图所示: 就可以在xml布局文件中的EditText定义代码中,添加入下面的代码,即可实现: android:drawableLeft="@dra ...

  8. Computer Vision: Algorithms and ApplicationsのImage processing

    实在是太喜欢Richard Szeliski的这本书了.每一章节(after chapter3)都详述了该研究方向比較新的成果.还有很多很多的reference,假设你感兴趣.全然能够看那些參考论文 ...

  9. 用swift创建各种UI控件【iSwifting社区】

    为了方便大家学习,www.iSwifting.com社区为大家准备了创建各种UI控件的代码.開始看着语法可能有些别扭,当用习惯了,就认为还是非常不错的. 社区还添加了问答专区.有问题的朋友.虽然问.大 ...

  10. activiti 基础搭建

    首先在eclipse内添加activiti的插件,https://blog.csdn.net/qq_22701869/article/details/79537971 1.创建一个maven的java ...