The sparse_table algorithm of RMQ problem

Source: Internet
Author: User

RMQ problem, full name (Range minimum/maximum Query), is the maximum value problem in a given interval.

The main methods and complexity are as follows:

1, simple (that is, search), O (n)-O (qn) online.

2, line tree, O (n)-O (Qlogn) online.

3, sparse_table (essentially dynamic planning), O (Nlogn)-O (1) online.

4, RMQ standard algorithm: first statute into LCA (Lowest Common Ancestor), then the Statute into a constraint Rmq,o (n)-O (1) online.

Yesterday just learned the third kind, St algorithm.

The ST algorithm can realize the query efficiency of O (1) After the preprocessing of O (NLOGN), thus solving the RMQ problem of many queries (such as greater than 1 million).

First, it is preprocessing. Preprocessing is the idea of DP, and we use F[I][J] to represent the maximum value in the interval [i,i+2^j-1] (that is, the closed interval from I, which is 2^j in length).

At first, f[i][0] must be equal to num[i]. OK, the initial value is found, the following is the state transition equation:

F[i][j]=max (f[i][j-1],f[i+2^ (j-1)][j-1]). That is, the [i,i+2^j-1] interval is divided into two parts [i,i+2^ (J-1)-1] and [i+2^ (J-1), i+2^ (j-1) +2^ (j-1)-1], exactly the same as the original interval.

With the initial value and the transfer equation, we can pass the value of all F[i][j] from the bottom up.

There is one more thing to note about boundaries. Due to the maximum interval length of n, the two-dimensional boundary is the largest of log (n)/log (2.0);

One-dimensional boundary as long as the satisfaction of each starting point, can have a length to find n on the line, that is, let i+2^j-1<=n good.

Then there is the query. Suppose you want to query the maximum of the interval [a, b], since the length of the interval is probably not an integer power of 2, we want to divide the interval into two parts of an integer power of length 2, and the set of these two intervals must be [a, b]. In order to achieve this, we need to first find a maximum K, so that 2^k<= (b-a+1), so that the interval can be divided into two parts [a,a+2^k-1] and [b-2^k+1,b], so that they can not exceed the range of A/b, but also to cover all the interval. Thus, the maximum value of the [a, b] interval is equal to the largest of the two intervals above.

Other explanations

Algorithm (sparsetable):

It is a method of dynamic programming. Take the minimum value as an example. A is the array to be searched, and a two- dimensional array F (i,j) is used to record the minimum value in the interval [i,i+2^j-1] interval. where f[i,0] = a[i]; therefore, for any set of (I,j), F (i,j) =min{f (i,j-1), F (i+2^ (j-1), j-1)} to be computed using dynamic programming. The genius of this algorithm is not the establishment of this dynamic programming, but its query: its query efficiency is O (1)! If you do not think about it, how to do it is not expected to have O (1) algorithm. Suppose we ask for the minimum value in the interval [m,n] and find a number k to make 2^k<n-m+1, i.e. K=[ln (b-a+1)/LN (2) ] so that the interval can be divided into two parts: [M,m+2^k-1] and [n-2^k+1,n]! We found that the two intervals are already initialized well! The front interval is f (m,k), and the trailing interval is f (n-2^k+1,k)! So, just look at the minimum value of the two intervals, you can know the minimum value of the entire interval!

Summary:
The sparse table (sparsetable) algorithm is O (NLOGN)-O (1), which is better for queries with large numbers of cases.
St algorithm preprocessing: using DP[I,J] to represent the rmq of the interval starting from I, with a length of 2^j, there is a recursiveDP[I,J]=MIN{DP[I,J-1],DP[I+2J-1,J-1]}, which updates blocks of length 2j with two adjacent blocks of length 2j-1.

therefore , the preprocessing time complexity is O (nlogn). This algorithm records the results of all queries of all length shapes, such as 2k. It can be seen from this that the spatial complexity of the sparse table algorithm is O (NLOGN).

Code:

#include <math.h> #include <stdio.h> #define MAX (A, b) A>b?a:b#define min (A, b) a<b?a:bconst int n=  100005;int N,q,c[n],a,b;int dp_max[n][20]; 20 is not necessarily the only one.  Need to calculate log (N)/log (2) int dp_min[n][20];    void Init () {for (int i=1;i<=n;i++) dp_max[i][0] = dp_min[i][0] = C[i];    Double limit = log (n)/log (2.0); for (int j=1;j<= (int.) limit;j++) for (int i=1;i+ (1&LT;&LT;J) -1<=n;i++) {Dp_max[i][j] = max (DP            _max[i][j-1],dp_max[i+ (1<< (j-1))][j-1]);        Dp_min[i][j] = min (dp_min[i][j-1],dp_min[i+ (1<< (j-1))][j-1]);    }}int Get_max (int a,int b) {int k = (int) (log (b-a+1)/log (2.0)); Return Max (dp_max[a][k],dp_max[b-(1<<k) +1][k]);}    int get_min (int a,int b) {int k = (int) (log (b-a+1)/log (2.0)); return min (dp_min[a][k],dp_min[b-(1<<k) +1][k]);}    int main () {scanf ("%d%d", &n,&q);    for (int i=1;i<=n;i++) scanf ("%d", &c[i]);    Init ();    while (q--) {scanf ("%d%d", &a,&b);    printf ("%d\n", Get_max (A, B)-get_min (A, b)); } return 0;}

Learn to self-Reliance blog:
The sparse_table algorithm of RMQ problem

The sparse_table algorithm of RMQ problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.