参考博客:http://blog.csdn.net/bingshangjiguang/article/details/5673692
附模板:
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int Max = 1000 + 5;
int gt[Max], n, st[Max][30];
void getSt()
{
for (int i = 1;i <= n;++i)
st[i][0] = gt[i];//最大是本身
for (int j = 1;1 << j <= n;++j)
for (int i = 1;i + (1 << j) - 1 <= n;++i)
st[i][j] = max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
int toRmq(int l, int r)
{
int k;
for (k = 0;(1 << k) <= r - l + 1; k++);
k--;
return max(st[l][k], st[r - (1 << k) + 1][k]);//求最大最小值,就算两个区间有重合也没关系
}
int main()
{
int t;scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
for (int i = 1;i <= n;++i)
scanf("%d", gt + i);
int q;scanf("%d", &q);
getSt();
for (int i = 0;i < q;++i)
{
int l, r;scanf("%d%d", &l, &r);
printf("%d\n", toRmq(l, r));
}
}
return 0;
}