博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu5443 The Water Problem(水)
阅读量:5115 次
发布时间:2019-06-13

本文共 2553 字,大约阅读时间需要 8 分钟。

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 
#include
#include
#include
#include
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\n", ans); } } return 0;}

  

 

转载于:https://www.cnblogs.com/PersistFaith/p/4808128.html

你可能感兴趣的文章
python网络编程初级
查看>>
#Python语言程序设计Demo - 七段数码管绘制
查看>>
Delphi 取整函数round、trunc、ceil和floor
查看>>
记百人计划--测试思路
查看>>
Python接口测试之moco
查看>>
android NDK的单步调试
查看>>
C/C++二维数组的用法
查看>>
排序 冒泡排序法
查看>>
同步代码时忽略maven项目 target目录
查看>>
MVC.NET:提供对字体文件.woff的访问
查看>>
Informatica_(2)第一个例子
查看>>
string 常用函数
查看>>
RGB色彩的计算机表示
查看>>
iOS地图之MapKit框架
查看>>
2010年过年左右时的艾米果
查看>>
朱元璋
查看>>
[常微分方程]Lecture 2: 欧拉数值方法及推广
查看>>
第九届全国大学生数学竞赛(江西赛区)数学类获奖学生名单
查看>>
《大型网站技术架构》读书笔记之七:随需应变之网站的可扩展架构
查看>>
剑指Offer面试题:28.连续子数组的最大和
查看>>