RMQ interval to find the most value

Source: Internet
Author: User

The RMQ is used for the interval quick Find maximum, which is applicable to cases where the period value is unchanged. The complexity of the preprocessing is O (Nlogn), the time complexity of the query is O (1), compared to the preprocessing O (Nlogn) of the segment tree, the Query O (logn), in some cases has its unique advantages.


RMQ principle is to run a DP on the original array, we take the query maximum value as an example, its state definition is this:

dp[I [j]: Subscript Starting from I, the maximum value of the interval of length 2^j. Obviously dp[I [0] is the subscript is the number of I itself.

The transfer equation is given below:

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


Max for the query interval [i ~ j]:

Set k = log (j-i + 1)/log (2)

max = max (dp[I [K], dp[j-2 ^ k + 1] [K])



The specific code for the above procedure is as follows:

#include <cstdio> #include <algorithm> #include <iostream> #include <cstring> #include < queue> #include <ctime> #include <cmath> #include <set> #define EPS 1e-10#define MAXN 500010#define INF 2*0x3f3f3f3fusing namespace Std;int NUM[MAXN], dp[maxn][30], N, L, r;int pow (int a, int p) {//= A^p here uses a fast power, the actual use should use an array pre- Handle if (P = = 0) return 1;int ans = pow (A, P/2); Ans *= ans;if (p% 2) ans *= a;return ans; int main () {//freopen ("in.in", "R", stdin),//freopen ("Out.out", "w", stdout), scanf ("%d", &n); for (int i = 1; I <= n ; i++) scanf ("%d", &num[i]); for (int i = 1; I <= n; i++)//Initialize dp[i][0] = dp[i][0 (int j = 1; Pow (2, j) num[i];for t;= N; J + +)//above the transfer equation for (int i = 1; i + POW (2, j)-1 <= N; i++) dp[i][j] = max (dp[i][j-1], dp[i + POW (2, j-1)][j-1]); SC ANF ("%d%d", &l, &r); Find the maximum value between the interval [l~r] int k = log (r-l + 1)/log (2), int ans = max (dp[l][k], Dp[r-pow (2, K) + 1][k]);p rintf ("ans is:%d\n", ANS); return 0;}


RMQ problem is of great use in dealing with LCA, an online method is to use DFS+RMQ to find the nearest common ancestor problem of two sub-nodes, the general practice is to put the timestamp of each point into an array in the order of access, So the common ancestor of U and V is the point where the time stamp between U and V is the smallest in the array, where the answer can be obtained between RMQ and O (1) in time.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

RMQ interval to find the most value

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.