HDU - 5443The Water Problem 线段树(区间查询最大值)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU - 5443The Water Problem 线段树(区间查询最大值)相关的知识,希望对你有一定的参考价值。
The Water Problem
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 3386 Accepted Submission(s): 2416
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(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,...,106. 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
Source
2015 ACM/ICPC Asia Regional Changchun Online
Recommend
hujie
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#define N 200010
#define ll long long
using namespace std;
ll a[N];
int n,m;
struct node //线段树
int l,r;
ll dat;
tree[N<<2];
void PushUp(int x) //线段树维护结点信息
tree[x].dat=max(tree[x<<1].dat,tree[x<<1|1].dat);
void build(int x,int l,int r) //线段树建树
tree[x].l=l;
tree[x].r=r;
if (l==r) //叶子节点
tree[x].dat=a[l];
return;
int mid=(l+r)>>1;
build(x<<1,l,mid);
build(x<<1|1,mid+1,r);
PushUp(x);
ll query(int x,int l,int r) //线段树区间查询
if (l<=tree[x].l&&r>=tree[x].r)
return abs(tree[x].dat); //找到
int mid=(tree[x].l+tree[x].r)>>1;
ll ans=0;
if (l<=mid) ans=max(ans,query(x<<1,l,r));
if (r>mid) ans=max(ans,query(x<<1|1,l,r));
return ans;
void update(int x,int y,ll k) //线段树单点修改
if (tree[x].l==tree[x].r)
tree[x].dat = k;
return ;
int mid=(tree[x].l+tree[x].r)>>1;
if (y<=mid) update(x<<1,y,k);
else update(x<<1|1,y,k);
PushUp(x);
int main()
int t;
cin>>t;
while(t--)
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]);
scanf("%d",&m);
build(1,1,n);
char s[10];
while(m--)
int i,j;
scanf("%d%d",&i,&j);
printf("%lld\\n",query(1,i,j));
return 0;
以上是关于HDU - 5443The Water Problem 线段树(区间查询最大值)的主要内容,如果未能解决你的问题,请参考以下文章
hdu-1029-Ignatius and the Princess IV
hdu 1028 Ignatius and the Princess III