输入一串数字,给你 M 个询问,每次询问就给你两个数字 X,Y,要求你说出 X 到 Y 这段区间内的最大数。
输入格式
第一行两个整数 N,M 表示数字的个数和要询问的次数;
接下来一行为 N 个数;
接下来 M 行,每行都有两个整数 X,Y。
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,m,a[N],st[N][20],l,r;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i=1;i<=n;i++)
cin >> a[i];
for(int i=1;i<=n;i++)
st[i][0]=a[i];
for(int j=1;(1<<j)<=n;j++)//n为个数
{
for(int i=1;(1<<j)<=n-i+1;i++)//n为下标
{
//(1<<j)都是总个数
st[i][j]=max(st[i][j-1],st[i+(1<<(j-1))][j-1]);
}
}
while(m--)
{
cin >> l >> r;
int k=log2(r-l+1);
cout << max(st[l][k],st[r-(1<<k)+1][k]) << endl;
}
return 0;
}