题目
http://acm.hdu.edu.cn/showproblem.php?pid=5443
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 <bits/stdc++.h>
using namespace std;
#define RMQ max // min
#define MXN 1010
int n, ans, a[MXN], st[MXN][20];
void st_init(){
for(int i = 1; i <= n; i++) st[i][0] = a[i]; // 第1列
for(int j = 1; (1<<j) <= n; j++){ // j的最大值
for(int i = 1; i+(1<<j)-1 <= n; i++){ // i最大值
//递推关系, 起点分别是:i 和 i+(1<<(j-1))
st[i][j] = RMQ(st[i][j-1], st[i+(1<<(j-1))][j-1]);
}
}
}
int st_query(int l, int r){
int j = 0;
// 满足 2^j < r-l+1 的最大j值
while((1<<(j+1)) < r-l+1) j++;
// 区间的起点分别是:l 和 r-(1<<j)+1
return RMQ(st[l][j], st[r-(1<<j)+1][j]);
}
int main(){
int T, q, l, r;
scanf("%d", &T);
while(T--){
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%d", a+i);
st_init();
scanf("%d", &q);
while(q--){
scanf("%d %d", &l, &r);
printf("%d\n", st_query(l, r));
}
}
return 0;
}