Original: http://blog.163.com/zhaohai_1988/blog/static/209510085201263011135062/
Thanks to the authors and the recommendations of kb ~~~~~~
Problem description
The rmq problem is the most important problem in the given range. For the number of series A whose length is N, answer several queries rmq (A, I, j ). Returns the subscript of the minimum value in [I, j] of array. For example, for a sequence of 5, 8, 1, 3, 6, 4, 9, 5, and 7, rmq (2, 4) = 3, rmq (6, 9)
= 6.
The simplest solution to the problem is O (n), which is to traverse the array for each query. However, when n is very large and the number of queries is very large, this solution is not so efficient. You can use the line segment tree (to be discussed later) to optimize the time complexity to O (logn), and store the maximum value of the line segment in the line segment tree. However, this article will introduce a sparse-table algorithm, the most powerful Algorithm for Solving rmq. The Sparse-table algorithm is an online algorithm. An online algorithm is used to process a query immediately after each input. This algorithm is usually used for preprocessing over a long period of time. After the information is sufficient, you can use less time to answer each query. ST (sparse
Table) is a well-known Algorithm for online rmq processing. It can be preprocessed in O (nlogn) time, and then each query is answered in O (1) time.
The first is preprocessing, Which is solved by Dynamic Planning (DP. If a [I] is the sequence that requires the greatest value in the interval, F [I, j] indicates the maximum value in the number of 2 ^ J consecutive since the number of I. For example, the number of Columns 3 2 4 5 6 8 1 2 9 7, F [1st] indicates the number starting from. The length is the maximum value of 2 ^ 0 = 1, which is actually 3. F [1, 2] = 5, F [1, 3] = 8, F [2, 0] = 2, F [2, 1] = 4 ...... From this we can see that f [I, 0] is actually equal to a [I]. In this way, the status and initial values of DP are available, and the rest is the state transition equation. We divide f [I, j] evenly into two segments (because f [I, j] must be an even number), from I to I + 2 ^ (J-1) -1 for the segment, I + 2 ^ (J-1) to I + 2 ^ J-1 for the segment (length are 2 ^ (J-1 )). The preceding example shows that when I = 1, j = 3, it is 3, 2, 4, 5.
And 6, 8, 1, 2. F [I, j] is the maximum value of the two segments. Then we obtain the Dynamic Programming equation f [I, j] = max (F [I, J-1], F [I + 2 ^ (J-1), J-1]).
Then query. If K = [log2 (J-I + 1)] is used, rmq (A, I, j) = min {f [I, K], f [J-2 ^ k + 1, K]}. For example, the maximum value of the range [] is required. A total of 2 to 8 are 7 elements, so K = 2, so we need to divide it into two intervals: [] and, because the maximum values of these two intervals can be obtained directly from F [2, 2] and f [5, 2.
As shown in:
Algorithm pseudocode
// Initialize init_rmq // MAX [I] [J] stores the maximum values of 2 ^ j Data starting with heavy I. The minimum value is similar to that of num containing the array value for I: 1 to n MAX [I] [0] = num [I] For J: 1 to log (N)/log (2) for I: 1 to (n + 1-2 ^ I) Max [I] [J] = max (MAX [I] [J-1], Max [I + 2 ^ (I-1)] [J-1]) // query rmq (I, j) k = Log (J-I + 1)/log (2) return max (MAX [I] [K], max [J-2 ^ k + 1] [k])