Learning Notes St Algorithm

Source: Internet
Author: User

"Intro" RMQ (Range minimum/maximum Query) Question:

For series A with length n, answer a number of questions RMQ (A,I,J) (I,j<=n), returning the smallest (large) value in the i,j of sequence A, that is, the RMQ problem is the problem of finding the maximum interval.

Method

1, simple (that is, search), O (n)-O (qn) online.    2, line tree, O (n)-O (Qlogn) online.        3, ST (essentially dynamic planning), O (Nlogn)-O (q) online. St Algorithm (Sparse Table), in order to maximize the value for example, set d[i,j] for [i,i+2^j-1] The maximum value in this interval, then asked to the [a, b] interval maximum value when the answer is Max (D[a,k], d[b-2^k+1,k]), where K is satisfied 2        ^k<=b-a+1 (i.e. the length) of the largest k, i.e. K=[ln (b-a+1)/LN (2)].    The method of D can be used in dynamic programming, D[i, J]=max (D[i, j-1],d[i+2^ (j-1), j-1]).        4, RMQ standard algorithm: first statute into LCA (Lowest Common Ancestor), then the Statute into a constraint Rmq,o (n)-O (q) online. First, the Cartesian tree is established according to the original sequence, thus the problem is regulated as LCA in linear time. The LCA problem can be constrained rmq in linear time, that is, the difference of any two adjacent numbers in the sequence is the RMQ problem of +1 or 1. The constraint rmq has an O (n)-O (1) on-line solution, so the time complexity of the whole algorithm is O (n)-O (1).

"Example" for the given array, ask for the interval minimum value. (No modification)
(Data range without segment tree)

The "solution" can write a line segment tree, but the preprocessing and query complexity are O (LOGN), the intentional words can get you off.

So using the ST algorithm, it can do O (nlogn) preprocessing, O (1) to answer each inquiry

F[I][J] Indicates the minimum value of the array p from position I to position i+2^j-1
F[i][j]=min (f[i+ (1<< (j-1))][j-1],f[i][j-1]); F[i][0]=p[i].
Finding the minimum value of a~b is to find the largest power K of two, which is smaller than the b-a+1.
With Ans=min (f[a][k],f[b-(1<<k) +1][k])

Principle

Nlogn preprocessing min[][] and max[][], query the time O (1) query.

Max[j][i] or Min[j][i] represents, starting from the position of J, the maximum or minimum value in a sub-segment of length 2^i.

Then the preprocessing is recursive.

when asked, calculate the logarithm of the length of [L,r] 2, and then take out the answer .

is an excellent method of access.

"Implementation" (in the case of maximum values):
The first is preprocessing, with a DP solution. Set A[i] is the number of intervals that require the most value, F[i,j] represents the maximum value from the number of consecutive 2^j from the first. For example, Series 3 2 4 5 6 8 1 2 9 7, f[1,0] represents the 1th number, the maximum length of 2^0=1, is actually 3 this number. F[1,2]=5,f[1,3]=8,f[2,0]=2,f[2,1]=4 ... From here you can see that f[i,0] is actually equal to a[i]. In this way, the DP state, the initial value has already been, the rest is the state transfer equation. We divide the f[i,j] evenly into two segments (because F[I,J] must be an even number of numbers), from I to i+2^ (j-1)-1 for a paragraph, i+2^ (j-1) to I+2^j-1 for a segment (2^ in length). Using the example above, when I=1,j=3 is the 3,2,4,5 and 6,8,1,2 the two paragraphs. F[I,J] is the maximum value in the maximum of these two paragraphs. So we got the equation of motion F[i,j]=max (f[i,j-1],f[i+2^ (j-i), j-1]).

The next is to get the most value, perhaps you can not imagine to calculate the use of f[i,j], want to calculate Max or O (logn), even O (n). But there is a good way to do it O (1). or separate. As in the above example we require the maximum value of the interval [2,8], it is divided into [2,5] and [5,8] two intervals, because the maximum value of these two intervals we can directly from f[2,2] and f[5,2] get. Extended to the general situation, is to divide the interval [l,r] into two lengths of 2^n (guaranteed to have f[i,j] corresponding)

"Template Code"

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6#include <queue>7#include <cstdlib>8#include <iomanip>9#include <cassert>Ten#include <climits> One #defineMAXN 100001 A #defineF (i,j,k) for (int i=j;i<=k;i++) - #defineM (A, B) memset (A,b,sizeof (a)) - #defineFF (i,j,k) for (int i=j;i>=k;i--) the #defineINF 0x7fffffff - #defineMAXM 21 - using namespacestd; - intRead () { +     intx=0, f=1;CharCh=GetChar (); -      while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} +      while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} A     returnx*F; at } - intFM[MAXN][MAXM],FI[MAXN][MAXM],P[MAXN]; - intn,q; -InlineintInit () - { -Cin>>n>>Q; inF (I,1, N) { -Cin>>P[i]; to     } +F (I,1, N) { -fm[i][0]=fi[i][0]=P[i]; the     } *     intM=floor ((int) (LOG10 (Double) n)/log10 ((Double)2)));  $F (J,1, m) F (i,1, N) {Panax NotoginsengFm[i][j]=max (fm[i+ (1<< (J-1))][j-1],fm[i][j-1]); -Fi[i][j]=min (fi[i+ (1<< (J-1))][j-1],fi[i][j-1]); the     } + } AInlineintStmax (intAintb) the { +     intM=floor ((int) (LOG10 (Double) (b-a+1))/log10 (Double)2))); -     returnMax (fm[a][m],fm[b-(1&LT;&LT;M) +1][m]);  $ } $InlineintStmin (intAintb) - { -     intM=floor ((int) (LOG10 (Double) (b-a+1))/log10 (Double)2))); the     returnMin (fi[a][m],fi[b-(1&LT;&LT;M) +1][m]);  - }Wuyi intMain () the { -Std::ios::sync_with_stdio (false);//cout<<setiosflags (ios::fixed) <<setprecision (1) <<y; Wu //freopen ("data.in", "R", stdin); - //freopen ("Data.out", "w", stdout); AboutInit ();intc,d; $      while(q--) -     { -         intb; -Cin>>a>>b; A         if(a>b) Swap ( A, a); +C=Stmax (A, b); theD=Stmin (A, b); -cout<<c<<endl<<d<<Endl; $      } the      return 0; the}
ST

Learning Notes St 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.