模板 倍增维护RMQ
倍增维护RMQ,nlogn预处理,O(1)查询
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
struct RMQ{
const static int RMQ_size = maxn;
int n;
int ArrayMax[RMQ_size][21];
int ArrayMin[RMQ_size][21];
int mm[RMQ_size];
void build_rmq(){
for(int j = 1 ; (1<<j) <= n ; ++ j)
for(int i = 0 ; i + (1<<j) - 1 < n ; ++ i){
ArrayMax[i][j]=max(ArrayMax[i][j-1],ArrayMax[i+(1<<(j-1))][j-1]);
ArrayMin[i][j]=min(ArrayMin[i][j-1],ArrayMin[i+(1<<(j-1))][j-1]);
}
mm[0]=-1;
for(int i=1;i<=n;i++){
mm[i]=((i&(i-1))==0)?mm[i-1]+1:mm[i-1];
}
}
int QueryMax(int l,int r){
int k = mm[r-l+1];
return max(ArrayMax[l][k],ArrayMax[r-(1<<k)+1][k]);
}
int QueryMin(int l,int r){
int k = mm[r-l+1];
return min(ArrayMin[l][k],ArrayMin[r-(1<<k)+1][k]);
}
void init(int * a,int sz){
n = sz ;
for(int i = 0 ; i < n ; ++ i) ArrayMax[i][0] = ArrayMin[i][0] = a[i];
build_rmq();
}
}solver;