RMQ算法就是求给定区间的最值问题,利用Sparse_Table算法经过预处理时间复杂度变成O(nlog(n))查询的时候就可以以O(1)实现快速查询。
f[i][j]表示从第i个开始连续2^j个数之间的最值,其中f[i][j]可以由f[i,j-1]和f[i+2^(j-1)][j-1]组合而成,f[i,j-1]的区间为[i,i+2^(j-1)-1],f[i+2^(j-1)][j-1]区间则是[i+2^(j-1),i+2^j-1]合并起来就是f[i][j]的区间[i,i+2^j-1].
查询:
假设要查询从m到n这一段的最小值, 那么我们先求出一个最大的k, 使得k满足2^k <(n - m + 1).
于是我们就可以把[m, n]分成两个(部分重叠的)长度为2^k的区间: [m, m+2^k-1], [n-2^k+1, n];
而我们之前已经求出了f(m, k)为[m, m+2^k-1]的最小值, f(n-2^k+1, k)为[n-2^k+1, n]的最小值
我们只要返回其中更小的那个, 就是我们想要的答案, 这个算法的时间复杂度是O(1)的.
而且的取值范围只需要算到n-2^k+1即可,j的取值范围也只需取到log(n+1)/log(2)即可。
poj 3264
Language:
Balanced Lineup
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 45601 Accepted: 21404
Case Time Limit: 2000MS
Description
For the daily milking, Farmer John’s N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.
Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.
Input
Line 1: Two space-separated integers, N and Q.
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i
Lines N+2..N+Q+1: Two integers A and B (1 ≤ A ≤ B ≤ N), representing the range of cows from A to B inclusive.
Output
Lines 1..Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.
Sample Input
6 3
1
7
3
4
2
5
1 5
4 6
2 2
Sample Output
6
3
0
典型RMQ
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
#define maxs( a , b ) a>b?a:b
#define mins( a , b ) a>b?b:a
const int MAX_N = 50005;
int d[MAX_N];
int dpmin[MAX_N][20];
int dpmax[MAX_N][20];
int n;
void create_Dpmin()//对dpmin[i][j]进行赋值
{
int i , j;
for( i = 1 ; i <= n ; i++ )
dpmin[i][0] = d[i];
for( j = 1 ; j <= log((double)(n+1))/log(2.0) ; j++ )
{
for( i = 1 ; i+(1<<j)-1 <= n ; i++ )
{
dpmin[i][j] = mins( dpmin[i][j-1] , dpmin[i+(1<<(j-1))][j-1] );
}
}
}
void create_Dpmax()
{
int i , j;
for( i = 1 ; i <= n ; i++ )
dpmax[i][0] = d[i];
for( j = 1 ; j <= log((double)(n+1))/log(2.0) ; j++ )
{
for( i = 1 ; i+(1<<j)-1 <= n ; i++ )
{
dpmax[i][j] = maxs( dpmax[i][j-1] , dpmax[i+(1<<(j-1))][j-1] );
}
}
}
int getmax( int a , int b )//a,b区间求出最值
{
int k = (int)(log((double)(b-a+1))/log(2.0));
return maxs( dpmax[a][k] , dpmax[b-(1<<k)+1][k] );
}
int getmin( int a , int b )
{
int k = (int)(log((double)(b-a+1))/log(2.0));
return mins( dpmin[a][k] , dpmin[b-(1<<k)+1][k] );
}
void Init()
{
create_Dpmin();
create_Dpmax();
}
int main()
{
int i , m , a , b;
scanf("%d%d",&n,&m);
for( i = 1 ; i <= n ; i++ )
{
scanf("%d",&d[i]);
}
Init();
while( m-- )
{
scanf("%d%d",&a,&b);
printf("%d\n",getmax(a,b)-getmin(a,b));
}
return 0;
}