Code:
#include
#include
#include
#include
using namespace std;
const int maxn = 100007;
int dp_max[maxn][25];//dp_max[i][j]表示arr[i], arr[i+1], ..., arr[i+2^j-1]中的最大值
int arr[maxn];
int n, m;
void init() {
for (int i=1;i<=n;i++) {
dp_max[i][0] = arr[i];
}
for (int j=1;j<=20;j++) {//2^20 大于maxn就行了
for (int i=1;i + (1 << j) - 1 <=n;i++) {
dp_max[i][j] = max(dp_max[i][j-1], dp_max[i + (1 << (j - 1))][j-1]);
}
}
}
void query(int from, int to) {
int s;
while ((1 << (s + 1) ) <= (to - from + 1))//或者直接s = log2(to - from + 1);
s++;
cout<<"max=";
cout<<max(dp_max[from][s], dp_max[to - (1 << s) + 1][s])<<endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for (int i=1;i<=n;i++) {
cin>>arr[i];
}
init();
int from, to;
while (m--) {
cin>>from>>to;
query(from, to);
}
return 0;
}