"RMQ" "Sparse_table algorithm"

Source: Internet
Author: User

Defined:

RMQ (range minimum/maximum query), or interval-most-valued queries , refers to an issue such as:

For sequence A of length n, answer a number of questions RMQ (A,I,J) (I,j<=n), returning the minimum/large value between the i,j of the column A.

There are a lot of solutions to this problem, and violence (and, of course, basically no questions will get you out of the way), line-segment Trees (O (Nlogn)), and a very well-known algorithm for on-line processing of RMQ problems-- sparse_table algorithm , called St algorithm, the algorithm can achieve the efficiency of Query O (1) after preprocessing O (Nlogn). The main idea is DP.

(i) pretreatment, DP

Set A[i] is the number of intervals requiring the most value of the series,Dp[i][j] represents the number of consecutive 2j from the first of the maximum value .

For example, a[10] = 3 2 4 5 6 8 1 2 9 7;

Initial:

Dp[1][0] Represents the 1th number, length is the maximum value of 20=1, that is, element 3. 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 dp[i][0] = A[i].

State transition equation:

We divide dp[i][j] into two segments of the same length and 2j-1, a section of dp[i][2j-1] and a dp[i+2j-1][2j-1]. Using the example above, when I=1,j=3 is the 3,2,4,5 and 6,8,1,2 the two paragraphs.

DP[I,J] is the maximum of the respective maximum values for these two paragraphs. So we got the state transition equation:

  DP[I][J] = max (dp[i][j-1], Dp[i + 2 (j-1)][j-1];

1 /*pretreatment->o (NLOGN)*/2 voidINITRMQ (intN)3 {4      for(inti =1; I <= N; i++) dp[i][0] =A[i];5      for(intj =1; J < -; i++)6          for(inti =1; I <= N; J + +)7DP[I][J] = max (dp[i][j-1], Dp[i + (1<< (J-1))][j-1]);8}

Note the order of loops in the code, the outer layer is J, and the inner is i!!

(b) Enquiry

If the query interval is a[i, j], then we need to find the smallest power that covers this closed interval (the left boundary takes I, the right bounds fetch j) (can overlap, for example, query a[5,9], we can query a[5678] and a[6789]).

Because the length of this interval len = j-i + 1, so we can take K=log2 (len), then there are: RMQ (A, I, j) = max{Dp[i][k], Dp[j-2k+1][k]}.

For example, the maximum value of the query interval [2,8], k = log2 (8-2 + 1) = 2, which is Max (dp[2][2],dp[8-2 ^ 2 +)) = Max (F[2, 2],f[5, 2]);

intLOG2[MAXN];voidinit () {log2[0] = -1; log2[1] =0; intI, J;  for(i =2, j = 0; i < MAXN; i++)    {        if(I > (1<< (j+1)) J + +; Log2[i]=J; }}intQueryintLintR) {    if(L >r) Swap (L, R); if(L = = R)returndp[l][0]; intLen = r-l+1; intk = Log[len];//here is the log2[] array to save the log value of each number, of course, can also be evaluated with the following formula;//int k = (int) (log (DES-SRC + 1.0)/log (2.0));    returnMax (Dp[l][k], dp[r-(1&LT;&LT;K) +1][k]);}

"RMQ" "Sparse_table algorithm"

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.