[tem]RMQ(st)

时间:2023-03-08 15:42:43
[tem]RMQ(st)

倍增思想

代码中有两个测试

#include <iostream>
#include <cmath>
using namespace std;
const int N=1e5;
int a[N],f[N][]; void init(int n){
for(int i=;i<=n;i++) f[i][]=a[i]; for(int j=;j<=;j++)
for(int i=;i+(<<j)-<=n;i++)
f[i][j]=min(f[i][j-],f[i+(<<(j-))][j-]);
} int RMQ(int l,int r){
int k=log(r-l+)/log(); //2^k<=l~r
return min(f[l][k],f[r-(<<k)+][k]);
}
int main(int argc, const char * argv[]) { //test beizeng
//find 2^k<=l~r
int l=,r=;
int ans1,ans2;
ans1=log(r-l+)/log();          //advise     
while ((<<(ans2+))<=r-l+) ans2++;
printf("test1: %d %d\n\n",ans1,ans2); //test beizeng
//change into binary
int n=;
for(int i=;i<=;i++)          //advise
if(n&(<<i)) printf("t1: %d ",i);
printf("\n");
for(int i=;i>=;i--)
if(n>=(<<i)) printf("t2: %d ",i),n^=(<<i);
return ;
}