Algorithmic sorting + template ③:rmq

Source: Internet
Author: User

The first is intended to use LCA as the third algorithm collation, but because of the study of LCA found their RMQ learning is not solid, so first review rmq. This article thanks the teammate to a song's study notes.

When I first saw the RMQ template, I felt good high-end, feeling the various arrays inside, the displacement operation is very complicated. So for any algorithm of learning I think to be divided into the following steps:

1. Understand what kind of problems this algorithm can solve. 2, know the worst time complexity of the algorithm and the expected time complexity, the future can see the problem through the data range to quickly determine the solution of some problems. 3, understand the idea of the algorithm and the most core code. 4, the last is to use the problem of the more than the training, until fully mastered.

The book to the True, RMQ first translated into Chinese is the interval of the most value problem, as the name implies, we want to use this algorithm to solve the problem of the maximum or minimum value of a certain interval, and the implementation of RMQ there are many ways, the simplest is the violence law, for the F array F[a,b] The most value, from beginning to end traversal, It is possible to find the corresponding maximum value. The time complexity of a single traversal is O (n), but if Q is asked, the total complexity is O (NQ), which in general is timed out. The second is the line tree method, using excellent data structure segment tree to solve, using the nature of the segment tree to maintain the maximum value of the interval, the specific algorithm will be detailed in the segment tree, and use the time complexity of the segment tree, a single query for O (LGN), q operation is O (QLGN), but the line tree can be done with O The complexity of the single-point or interval modification, when the topic is related to the maintenance of the maximum value of the problem, the general use of line tree. The third algorithm is the ST algorithm detailed in this paper, its nature is dynamic programming, can be in O (NLGN) time complexity of the original array preprocessing, and then the O (1) time complexity of the query, very fast and convenient. The last one is the transformation between the LCA and the RMQ, and the RMQ is first regulated into an LCA, and then the constraint RMQ. I feel that this solution is not very meaningful, as to understand, after all, there is an excellent algorithm such as St or line tree RMQ solution, there is no need to superfluous.

For St algorithm:

Its essence is the idea of DP, for array F, using an auxiliary array dp,dp[i,j] to represent the maximum number of 1<<j starting from f[i], i.e.: from F[i] to f[i+ (1<<J)-1]. For example, for dp[5,2] is the maximum value of f[5] to f[5+4-1] that is f[8]. Now let's take a look at how the DP array is state-shifted, assuming we require f[5] to f[12], and f[5] to f[12] we can represent dp[5,3], then we can split the interval into: f[5] to f[8] and f[9] to f[12], namely: dp[5,2 ] and dp[9,2]. So the state transfer equation is easy to write: Dp[i,j]=max (or Min) (dp[i,j-1],dp[i+ (1<< (j-1)), J-1]).

And in the query, we can think of this, if for a certain interval f[a,b], suppose there is an intermediate point C, there is a constant d< (b-a)/2, we can split f[a,b] into f[a,c+d] and f[c-d,b]. In other words, we do not necessarily have to decompose the interval completely, because we solve the interval maximum, even if there are repeated elements after decomposition, it does not affect the maximum value. So for the interval f[a,b] we just have to select a range with a as the head and a range with a B as the tail and repeat the number of elements greater than or equal to 0. For example, we would like to query f[5,11] for the maximum value, we can decompose into f[5,8] and f[8,11], as we have already said, for the existence of duplicate elements is not affected by the final maximum value. So for the decomposition of the interval, can be used in the auxiliary array of dp[5,2] and dp[8,2] to represent, so our query just find dp[5,2] and dp[8,2] in the maximum value.

Const intmaxn=50010;intdp[maxn][ -];intMM[MAXN];//for log optimizations, there is an array ofvoidINIT_RMQ (intNint*G) {mm[0]=-1;  for(intI=1; i<=n;i++) {Mm[i]= ((i& (i-1))==0)? mm[i-1]+1: mm[i-1]; dp[i][0]=F[i]; }     for(intj=1; j<=mm[n];j++){         for(intI=1; i+ (1&LT;&LT;J)-1<=n;i++) {Dp[i][j]=max (dp[i][j-1],dp[i+ (1<< (J-1))][j-1]); }    }}intRMQ (intXinty) {    intk=mm[y-x+1]; returnMax (dp[x][k],dp[y-(1&LT;&LT;K) +1][k]);}
rmq Templates

Two-dimensional rmq and one-dimensional RMQ principle is the same, understand the template can be copied:

intval[ -][ -];intdp[ -][ -][Ten][Ten];intmm[ -];voidINIT_RMQ (intNintm) {mm[0]=-1;  for(intI=1; i<= -; i++) {Mm[i]= ((i& (i-1))==0)? mm[i-1]+1: mm[i-1]; }     for(intI=1; i<=n;i++)         for(intj=1; j<=m;j++) dp[i][j][0][0]=Val[i][j];  for(intIi=0; ii<=mm[n];ii++){         for(intjj=0; jj<=mm[m];jj++){            if(ii+JJ) {                 for(intI=1; i+ (1&LT;&LT;II)-1<=n;i++){                     for(intj=1; j+ (1&LT;&LT;JJ)-1<=m;j++){                        if(ii) DP[I][J][II][JJ]=max (dp[i][j][ii-1][jj],dp[i+ (1<< (ii-1))][j][ii-1][JJ]); ElseDP[I][J][II][JJ]=max (dp[i][j][ii][jj-1],dp[i][j+ (1<< (jj-1))][ii][jj-1]); }                }            }        }    }}intRMQ (intX1,intY1,intX2,inty2) {    intk1=mm[x2-x1+1]; intk2=mm[y2-y1+1]; X2=x2-(1&LT;&LT;K1) +1; Y1=y2-(1&LT;&LT;K2) +1; returnMax ( Max (Dp[x1][y1][k1][k2],dp[x1][y2][k1][k2]), Max (DP[X2][Y1][K1][K2],DP[X2][Y2][K1][K2]));}
two-dimensional rmq template

Algorithmic sorting + template ③:rmq

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.