ST表,O(nlogn)预处理,O(1)求区间最值
线段树也能解决,而且支持动态修改,但是对于不修改的序列,线段树查询logn,要慢4
用f[i][j]表示一个区间[i,2^j]
利用倍增
f[i][j]=____(f[i][j-1],f[1+2^(j-1)][j-1])
也就是拆分成两段,预处理
查询也是拆分,拆分成两段可以重叠的部分,求最值不影响
拆分的长度是2^l,l=log2(y-x+1)
查询两段 f[x][l],f[y-2^l+1][l]
//Writer:GhostCai && His Yellow Duck
#include<iostream>
#include<cmath>
using namespace std;
const int MAXN=20000;
int a[MAXN];
int f[MAXN][32];
int n,m;
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
f[i][0]=a[i];
}
for(int j=1;(1<<j<=n);j++){
for(int i=1;i<=n;i++){
f[i][j]=max(f[i][j-1],f[i+(1<<(j-1))][j-1]);
}
}
int x,y,l;
for(int i=1;i<=m;i++){
cin>>x>>y;
l=log(y-x+1)/log(2);
cout<<max(f[x][l],f[y-(1<<l)+1][l])<<endl;
}
}