#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int maxsum[1000000][30];
int minsum[1000000][30];
int a[1000000];
int main(void)
{
int n,i,j;
while(scanf("%d",&n)==1)
{
memset(maxsum,0,sizeof(maxsum));
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
maxsum[i][0] = minsum[i][0] = a[i];//预处理第一层
}
for(j=1;(1<<j)<=n;j++)
for(i=1;i + (1<<j) - 1 <= n;i++)
{
maxsum[i][j] = max(maxsum[i][j - 1], maxsum[i + (1 << (j - 1))][j - 1]);
minsum[i][j] = min(minsum[i][j - 1], minsum[i + (1 << (j - 1))][j - 1]);
}
while(1)
{
int L,R;
scanf("%d%d",&L,&R);
int k = (int)(log(R - L + 1.0)/log(2.0));
int maxn = max(maxsum[L][k],maxsum[R-(1<<k)+1][k]);
int minn = min(minsum[L][k],minsum[R-(1<<k)+1][k]);
printf("%d %d\n",maxn,minn);
}
}
return 0;
}
RMQ模板
最新推荐文章于 2022-03-26 21:08:54 发布