1. Overview
RMQ (range minimum/maximum query), or interval-most-valued queries, refers to the question of answering a number of queries RMQ (A,I,J) (i,j<=n) for a sequence of length n, and returning the smallest/largest value between the i,j in sequence a. These two problems are frequently encountered in practical applications, the following is a more efficient algorithm to solve these two problems. Of course, the problem can also be solved with the line segment tree (also called interval tree), the algorithm complexity is: O (N) ~o (LOGN), here we do not introduce.
2.RMQ Algorithm
The easiest solution for this problem is traversal, and the complexity is O (n). But when the amount of data is very large and the query is very frequent, the algorithm cannot query the positive solution in a valid time.
This section describes a more efficient on-line algorithm (ST algorithm) to solve this problem. The so-called online algorithm, refers to the user each input a query immediately processing a query. The algorithm is usually used for a long time to preprocess, waiting for sufficient information can be used in less time to answer each query. The ST (Sparse Table) algorithm is a well-known algorithm for online processing of RMQ problems, which can be preprocessed in O (nlogn) time and then answer each query in O (1) time.
(a) first, pretreatment, with dynamic programming (DP) solution.
Set A[i] is the number of intervals that require the most value, F[i, j] represents the maximum value from the number of consecutive 2^j from the first. (DP status)
For example:
A number of columns: 3 2 4 5 6 8 1 2 9 7
f[1,0] Represents the 1th number, the maximum length of 2^0=1, is actually 3 this number. similarly f[1,1] = max (3,2) = 3, F[1,2]=max (3,2,4,5) = 5,f[1,3] = max (3,2,4,5,6,8,1,2) = 8;
And we can easily see that f[i,0] is equal to a[i]. (DP's initial value)
In this way, the DP state, the initial value has already been, the rest is the state transfer equation.
We divide f[i,j] evenly into two segments (because F[I,J] must be an even number of numbers), from I to i + 2 ^ (j-1)-1 for a paragraph, i + 2 ^ (j-1) to i + 2 ^ j-1 for a segment (length is 2 ^ (j-1)). Using the example above, when I=1,j=3 is the 3,2,4,5 and 6,8,1,2 the two paragraphs. F[I,J] is the maximum of the respective maximum values for these two paragraphs. So we got the state transfer equation F[i, J]=max (f[i,j-1], f[i + 2^ (j-1), j-1]).
The code is as follows:
void RMQ (int num)//preprocessing->o (Nlogn)
{for
(int j = 1; j <, ++j) for
(int i = 1; I <= num; ++i)
if ( i + (1 << J)-1 <= num)
{
Maxsum[i][j] = max (maxsum[i][j-1], maxsum[i + (1 << (j-1))][j-1]);
Minsum[i][j] = min (minsum[i][j-1], minsum[i + (1 << (j-1))][j-1]);
}
}
What we need to note here is the order of the loops, and we find that the outer layer is J, inner I, which is why. Can it be I outside, J?
The answer is no. Because we need to understand the meaning of this state transfer equation.
The state transition equation means: First update all lengths of f[i,0] that is 1 elements, and then through the 2 1 elements of the maximum value, get all the length of f[i,1] that is 2 elements of the maximum, and then through 2 2 elements of the maximum, to get all the length of f[i,2] is 4 elements of the maximum value, And so on to update the maximum value for all lengths.
And if I was outside, J, the Order of our update is f[1,0],f[1,1],f[1,2],f[1,3], which means the update starts from 1 1 elements, 2 elements, 4 elements, 8 elements (A[0],a[1],.... A[7]), where f[1,3] = max (max (a[0],a[1],a[2],a[3), Max (A[4],a[5],a[6],a[7])), but we didn't calculate Max (A[0],a[1],a[2],a[3]) and Max (A[4],a[5],a[6],a[7]), so this method must be wrong.
To avoid such errors, it is important to understand the meaning of this state transfer equation.
(b) then the query.
If we need to query the interval is (I,J), then we need to find a cover this closed interval (the left boundary takes I, the right boundary fetch J) the smallest power (can be repeated, such as query 5,6,7,8,9, we can query 5678 and 6789).
Since the length of this interval is J-i + 1, we can take k=log2 (J-i + 1), then there are: RMQ (A, I, j) =max{f[i, K], f[j-2 ^ k + 1, K]}.
For example, the maximum value of the interval [2,8] is required, k = log2 (8-2 + 1) = 2, which means Max (f[2, 2],f[8-2 ^ 2 + 1, 2]) = Max (F[2, 2],f[5, 2]);
Here we also need to pay attention to one place, which is the precedence of the << and +-operators.
For example, this expression: 5-1 << 2 is how much.
The answer is: 4 * 2 * 2 = 16. So we're going to write 5-(1 << 2) is 5-1 * 2 * 2 = 1.