St algorithm:
ID Array Subscript: 1 2 3 4 5 6 7 8 9
ID array elements: 5 7 3 1 4 8 2 9 8
1,St algorithm function :
It is mainly used to calculate the interval maximum, which can compress the interval of the required requirement greatly, and the complexity of the query is O (1). For example, we require a range of the maximum, even with the idea of DP, using DP[I][J] to represent the maximum from the I to J interval, if you need to save the data element n more time, such as n=10000, you open a two-dimensional array definitely hyper-memory, if you use a line tree, maybe it can work , but if n is n=100000 at a larger time, it is not possible to estimate the number of sub-ranges that the line tree can create. This is where the St algorithm comes in handy ~
2. The main Preservation form of St algorithm is f[i][k]:
Indicates the interval size from n points as the starting point, the interval with the length of 2^k equivalent to id[i][i+2^k-1] , and the greater the range when k is larger. A point to note is that the interval length is 2^k to express!!!
3, F[i][k] pretreatment :
At the same interval, the maximum value of each interval f[i][k] is solved by the idea of dynamic programming.
When k=0 , the interval is represented as a point, which is the value at the I position. So the initial value of DP is also f[i][0]=id[i].
Then, dynamic programming is the most important even if the state transfer equation, with the maximum value as an example, that is,
f[i][J]=max (f[i][j-1],f[i+2^ (j-1)[j-1]);
The origin of this state transfer equation, we ask for F[i][j], which is the range represented by the array of id[] [i,i+2^j-1]:
F[I][J]=>ID[I~I+2^J-1]:
[i ... ...] ... ... ..... ..... ..... ............................. ..... ..... ..... ..... ..... ..... ..... ..... ..... ...... ....... ....... ........... i+2^j-1 ]
It is easy to understand that this interval is made up of two equal parts, namely [i,i+2^j-1]=[i,i+2^ (J-1) -1]+[i+2^ (j-1), i+2^j-1], f[i][j] and f[i][j-1 (f[i+2^) j-1]. That is, each time you assign the maximum value from the sub-interval of the two points to the current interval. So that the maximum value of all the intervals is preserved,
Bisection =>[i,i+2^j-1]=[i,i+2^ (j-1) -1]+[i+2^ (j-1), i+2^j-1]=>f[i][j-1] and f[i+2^ (j-1)][j-1]
[i... ...] ..... ............................. ..... ..... ..... ..... ..... .............. .......... ........................ i+2^j-1 ]
||
[i ... ...] ..... .......................... i+2^ (j-1)-1 [i+2^ (j-1) ..... ...................... i+2^j-1 ]
4 . The maximum value of the solution interval [a, b]
However, we need to ask for the interval [a, b], not so [i,2^k], such an interval, to find this f[i][k] what is the use of the interval? Of course it's useful. Let's convert the interval [A, a, b] a bit, will you?
We can know that interval [a, b] interval length is b-a+1, if you want to convert the interval length to two 2^k, and then solve the maximum value of two intervals, you think wrong, it is impossible, unless you can prove that an arbitrary number n=2^k+2^k,k the existence of an integer solution, and then it is obviously wrong, such as n= 11 is not an integer solution.The ST algorithm's query only O (1), he is by solving the interval length of the largest 2^k value, that is, to find a K, make 2^k<=b-a+1, and then by comparing the interval [a,a+2^k-1] and [B-2^k+1,b] to achieve the maximum value, the a+2^k-1 here will not necessarily be equal to b-2^k+1, and the general situation is greater than the case,
[A...................................................................b]
[A....................... (a+2^k-1)]
[(b-2^k+1) ... b. A.... .... A.]
The maximum value of these two intervals is the maximum value of the solution interval [a, b].
First we have to find the K, the calculation method is:
2^k=b-a+1
=>k=log2 (b-a+1)
=>K=LG (b-a+1)/LG (2)
=>k= (int) (log (b-a+1.0)/log (2.0));
(LG in PS:C is calculated using log to represent Orz,orz,orz...)
5, some proofs:
The solution of K represents the length of b-a+1 represented as 2 of the N-Square, the largest can be represented by N, will certainly make: 2^k<=b-a+1.
Proof: 2^k>= (b-a+1)/2.
Assuming that the K is the largest secondary, if the 2^k is less than half the interval length (b-a+1),
Indicates that the interval (b-a+1) is longer than 2 2^k,2^k+2^k=2^ (k+1),
In other words, this interval (b-a+1) also exists n=k+1 makes 2^n > 2^k, inconsistent with assumptions,
Therefore, 2^k>= (b-a+1)/2 Heng set up,
Code: (2015.8.14)
1#include <iostream>2#include <stdio.h>3#include <math.h>4 #defineMax (a) (a) > (b)? (a):(B)5 #defineMin (a) (a) < (b)? (a):(B)6 #defineMAX 1000107 using namespacestd;8 intmaxsum[max][ -];9 intminsum[max][ -];Ten intNum[max]; One voidCread_st (intN//pretreatment->o (NLOGN) A { - for(intI=1; i<=n;i++) maxsum[i][0]=minsum[i][0]=Num[i]; - intLen= (int) (log (N)/log (2.0)); the for(intj =1; J <=Len; J + +){ - for(inti =1; i+ (1<<J)-1<= N; i++){ - inttmd=i+ (1<< (J-1)); -Maxsum[i][j]=max (maxsum[i][j-1],maxsum[tmd][j-1]); +Minsum[i][j]=min (minsum[i][j-1],minsum[tmd][j-1]); - } + } A } at intRMQ (intLintR) - { - intk,tmd,max,min; -K= (int) (Log (r-l+1.0)/log (2.0)); -tmd=r-(1<<K) +1; -max=Max (maxsum[l][k],maxsum[tmd][k]); inmin=min (minsum[l][k],minsum[tmd][k]); - returnmax-Min; to } + intMain () - { the intn,i,q,a,b,k,max,min; * while(SCANF ("%d%d", &n,&q)! =EOF) $ {Panax Notoginseng for(i=1; i<=n;i++) -scanf"%d",&num[i]); the Cread_st (N); + while(q--) A { thescanf"%d%d",&a,&b); +printf"%d\n", RMQ (A, b)); - } $ } $ return 0; -}
View Code
"Original" St algorithm detailed explanation