The water problem
Time Limit:1 Sec
Memory limit:256 MB
Topic Connection http://acm.hdu.edu.cn/showproblem.php?pid=5443
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 is given an integer T (t≤10) indicating the number of test cases. For each test case, there was 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 are in {1,..., 106}. On the next line, there is a number Q (0≤q≤1000) representing the number of queries. After that, there'll be Q lines with the 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
HINT
Test instructions
Query interval maximum, without modification
Exercises
Anyway, the data range is only 1000.
Code:
#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>#include<map>#include<stack>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineTest Freopen ("Test.txt", "R", stdin)#defineMAXN 10505#defineMoD 10007#defineEPS 1e-9Const intinf=0x3f3f3f3f;Constll infll =0x3f3f3f3f3f3f3f3fll;inline ll Read () {ll x=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}//**************************************************************************************structnode{intL,r; intMa;}; Node A[MAXN*4];intNUM[MAXN];voidBuildintXintLintR) {A[X].L=l,a[x].r=R; if(l==r) {a[x].ma=Num[l]; return; } intMid= (l+r) >>1; Build (x<<1, L,mid); Build (x<<1|1, mid+1, R); A[x].ma=max (a[x<<1].ma,a[x<<1|1].ma);}intQueryintXintLintR) { intL=a[x].l,r=A[X].R; if(l<=l&&r<=R)returna[x].ma; intMid= (A[X].L+A[X].R) >>1; if(r<=mid)returnQuery (x<<1, L,r); if(l>mid)returnQuery (x<<1|1, L,r); returnMax (Query (x<<1, L,mid), query (x<<1|1, mid+1, R));}intMain () {intt=read (); while(t--) {memset (A,0,sizeof(a)); intn=read (); for(intI=1; i<=n;i++) Num[i]=read (); Build (1,1, N); intq=read (); for(intI=0; i<q;i++) { intL=read (), r=read (); printf ("%d\n", Query (1, L,r)); } }}
HDU 5443 The water Problem segment tree