RMQ algorithm, the algorithm is a fast off request value interval most pretreatment time complexity O (n * log (n)), the query O (1), it is a very fast algorithm, of course, the same problem with the segment tree It can be solved.
In the given N data fast, by preprocessing. Quickly find the most value.
Mainly dp +-bit computing.
Precomputed:
void rmq_isit(){
for(int i=1;i<=m;i++) {
dp[i][0]=a[i];
}
for(int i=1;(1<<i)<=m;i++){
for(int j=1;j+(1<<i)-1<=m;j++){
dp[j][i]=min(dp[j][i-1],dp[j+(1<<(i-1))][i-1]);
}
}
}
1 << x equal to x-th power of 2;
Transfer each core:
dp [ i ] [ j ] = max ( dp [ i ] [ j - 1 ] , dp [ i + ( 1 << ( j - 1 ) ) ] [ j - 1 ] );
dp [ i ] [ j ] = min ( dp [ i ] [ j - 1 ] , dp [ i + ( 1 << ( j - 1 ) ) ] [ j - 1 ] );
Check out the core:
**int rmq_1 ( int l , int r){
int k = 0;
while( ( 1 << ( k ) ) <= ( r - l + 1 ) ) k++;
k --;
int ans1 = min ( dp [ l ] [ k ] , dp [ r - ( 1 << k ) + 1 ] [ k ] ) ;
return ans1;
}**
With pictures on a good understanding. . .