*** Welcome to piglix ***

Range Minimum Query


In computer science, a range minimum query (RMQ) solves the problem of finding the minimal value in a sub-array of an array of comparable objects. Range minimum queries have several use cases in computer science such as the lowest common ancestor problem or the longest common prefix problem (LCP).

Given an array A[1 … n] of n objects taken from a well-ordered set (such as numbers), the range minimum query RMQA(l,r) =arg min A[k] (with 1 ≤ lkrn) returns the position of the minimal element in the specified sub-array A[lr].

For example, when A = [0,5,2,5,4,3,1,6,3], then the answer to the range minimum query for the sub-array A[3 … 8] = [2,5,4,3,1,6] is 7, as A[7] = 1.

In a typical setting, the array A is static, i.e., elements are not inserted or deleted during a series of queries, and the queries to be answered on-line (i.e., the whole set of queries are not known in advance to the algorithm). In this case a suitable preprocessing of the array into a data structure ensures faster query answering. A naïve solution is to precompute all possible queries, i.e. the minimum of all sub-arrays of A, and store these in an array B such that B[i, j] = min(A[ij]); then a range min query can be solved in constant time by array lookup in B. There are Θ(n²) possible queries for a length-n array, and the answers to these can be computed in Θ(n²) time by dynamic programming.

As in the solution above, answering queries in constant time will be achieved by pre-computing results. However, the array will store precomputed min queries for all and only the ranges whose size is a power of two. There are Θ(log n) such queries for each start position i, so the size of the dynamic programming table B is Θ(n log n). Each element B[i, j] holds the index of the minimum of the range A[ii+2j-1]. The table is filled with the indices of minima using the recurrence


...
Wikipedia

...