The Water Problem:
题目描述:
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,...,ana1,a2,a3,...,an representing the size of the water source. Given a set of queries each containing 22 integers ll and rr, please find out the biggest water source between alal and arar.
输入描述:
First you are given an integer T(T≤10)T(T≤10) indicating the number of test cases. For each test case, there is a number n(0≤n≤1000)n(0≤n≤1000) on a line representing the number of water sources. nn integers follow, respectively a1,a2,a3,...,ana1,a2,a3,...,an, and each integer is in 1,...,1061,...,106. On the next line, there is a number q(0≤q≤1000)q(0≤q≤1000) representing the number of queries. After that, there will be qq lines with two integers ll and r(1≤l≤r≤n)r(1≤l≤r≤n) indicating the range of which you should find out the biggest water source.
输出描述:
For each query, output an integer representing the size of the biggest water source.
题目大致为:找寻给定区间的最大值:
输入解释:首先是T组样例 然后会给你一个数组:输入的话是这个数组的长度n;后面跟随的 a1,a2'''''''''an 然后给你m组查询 后跟随输入L,R;查询max[ L,R ];
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+100;
int node[maxn<<2];
int num[maxn];
void build(int rt,int l,int r){
if(l==r){
node[rt]=num[l];
return;
}
int mid=(l+r)>>1;
build(rt<<1,l,mid);
build(rt<<1|1,mid+1,r);
node[rt]=max(node[rt<<1],node[rt<<1|1]);
}
int query(int rt,int l,int r,int L,int R){
if(L<=l&&r<=R){
return node[rt];
}
int ans1=-1000000000,ans2=-1000000000;
int mid=(l+r)>>1;
if(L<=mid)ans1=max(ans1,query(rt<<1,l,mid,L,R));
if(R>mid)ans2=max(ans2,query(rt<<1|1,mid+1,r,L,R));
return max(ans1,ans2);
}
int main(){
int t,n,m;
cin>>t;
while(t--){
cin>>n;
for(int i=1;i<=n;i++){
cin>>num[i];
}
build(1,1,n);
cin>>m;
for(int i=0;i<m;i++){
int a,b;
cin>>a>>b;
cout<<query(1,1,n,a,b)<<endl;;
}
}
return 0;
}
希望对博友们有帮助!