代码:
void RMQ(int n)
{
for(int j = 1; j != 20; j++)
{
for(int i = 1; i <= n; i++)
{
if(i + (1<<j) - 1 <= n)
dp[i][j] = max(dp[i][j - 1], dp[i + (1<<j>>1)][j - 1]);
}
}
}
int main()
{
int t, i, m, n;
cin >> t;
while(t--)
{
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
scanf("%d", &dp[i][0]);
}
RMQ(n);
scanf("%d", &m);
while(m--)
{
int l, r, k;
scanf("%d%d", &l, &r);
k = (int)(log(1.0 * r - l + 1) / log(2.0));
int ans = max(dp[l][k], dp[r - (1<<k) + 1][k]);
cout << ans << endl;
}
}
return 0;
}