RMQ是一种在线求区间最值的算法。
求法:
1 void rmq_init(){ 2 for(int i=1;i<=N;i++) dp[i][0]=a[i]; 3 for(int j=1;(1<<j)<=N;j++) 4 for(int i=1;i+(1<<j)-1<=N;i++) 5 dp[i][j]=min(dp[i][j-1],dp[i+(1<<j-1)][j-1]); 6 }
查询:
1 int rmq(int l,int r){ 2 int k=log2(r-l+1); 3 return min(dp[l][k],dp[r-(1<<k)+1][k]); 4 }