求区间最值,数据范围也很小,因为只会线段树,所以套了线段树模板=.=
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
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6 # include <queue> 7 # define LL long long 8 using namespace std ; 9 10 const int maxn = 1010; 11 12 int MAX[maxn<<2] ; //结点开4倍 13 14 void PushUP(int rt) //更新到父节点 15 { 16 MAX[rt] = max(MAX[rt * 2] , MAX[rt * 2 + 1] ); //rt 为当前结点 17 } 18 19 void build(int l , int r , int rt) //构建线段树 20 { 21 if (l == r) 22 { 23 scanf("%d" , &MAX[rt]) ; 24 return ; 25 } 26 int m = (l + r) / 2 ; 27 build(l , m , rt * 2) ; 28 build(m + 1 , r , rt * 2 +1) ; 29 PushUP(rt) ; 30 } 31 32 33 int query(int L , int R , int l , int r , int rt) //区间求最大值 34 { 35 if (L <= l && r <= R) 36 return MAX[rt] ; 37 int m = (l + r) / 2 ; 38 int ret = 0 ; 39 if (L <= m) 40 ret = max(ret , query(L , R , l , m , rt * 2) ) ; 41 if (R > m) 42 ret = max(ret , query(L , R , m + 1 , r , rt * 2 + 1) ); 43 return ret ; 44 } 45 46 int main () 47 { 48 //freopen("in.txt","r",stdin) ; 49 int n , m ; 50 int T ; 51 scanf("%d" , &T) ; 52 while(T--) 53 { 54 scanf("%d" , &n) ; 55 build(1 , n , 1) ; 56 scanf("%d" , &m) ; 57 while(m--) 58 { 59 int a , b ; 60 scanf("%d %d" , &a , &b) ; 61 printf("%d ", query(a , b , 1 , n , 1)) ; 62 } 63 } 64 65 return 0 ; 66 }