RMQ (range Minimum/maximum Query), which is the interval maximum value problem.
For a sequence A of length n, answer a number of query RMQ (A,I,J) (i,j<=n) to return the maximum (small) value in the i,j of the sequence A.
Relative algorithm simplicity (search), time complexity: O (n) −o (QXN) o (n)-O (qxn), online segment tree, time complexity: O (n) −o (QXLOGN) o (n)-O (qx\log N), online ST (dynamic planning), Time complexity: O (NXLOGN) −o (q) O (nx\log N)-O (q), online RMQ standard algorithm, first statute for LCA, then statute into constraint RMQ, time complexity: O (n) −o (q) O (n)-O (q), online
ST Algorithm
Assuming that the current topic requires the interval minimum, we make dp[i][j] represent the minimum value of the interval from I starting with the length 2j 2^j.
so there are: dp[i][j]=min (dp[i][j−1],dp[i+2j−1][j−1]) dp[i][j]=\min (dp[i][j-1],dp[i+2^{j-1}][j-1))
The analysis shows that dp[i][j−1] dp[i][j-1] represents the minimum value from the beginning of the length of the 2j 2^j interval, and dp[i+2j−1][j−1] Dp[i+2^{j-1}][j-1 is the other half of the interval.
at last (from the bottom up):
|
dp[0][*] |
dp[1][*] |
dp[2][*] |
dp[3][*] |
dp[4][*] |
dp[5][*] |
dp[6][*] |
dp[7][*] |
| DP[*][3] |
1 |
|
|
|
|
|
|
|
| DP[*][2] |
1 |
1 |
1 |
5 |
2 |
|
|
|
| DP[*][1] |
3 |
1 |
1 |
5 |
7 |
6 |
2 |
|
| DP[*][0] |
4 |
3 |
1 |
5 |
7 |
8 |
6 |
2 |
Write a set of data, your own hands-on simulation can be understood slightly ~
pretreatment
According to the state transition equation, first specify the initial value when the interval length is 20 2^0, then the result of the following is introduced.
void St_init (const vector<int> &a)
{
int n=a.size ();
for (int i=0; i<n; i++)
dp[i][0]=a[i];
for (int j=1; (1<<J) <=n; J + + for
(int i=0; i+ (1<<j) <=n; i++)
dp[i][j]=min (dp[i][j-1],dp[i+ (1<<)) j-1]);
Query
After preprocessing the whole DP array, the query operation is very simple, so k is to satisfy the 2k<=r−l+1 2^k of the largest integer, then the beginning of L, with R end of two lengths of 2k 2^k of the interval together that covers the query interval [l,r].
int rmq (int l,int R)
{
int k=0;
while ((1<< (k+1)) <=r-l+1) k++;
return min (dp[l][k],dp[r-(1<<k) +1][k]);
}
Line Segment Tree
Well. How to say it. The sensory segment tree seems to be the most versatile method in this type of topic.
Whether it is [point modification + query] or [interval modification + query], it can do O (logn) O (\log N) of the complexity, and in the line tree we can maintain a lot of things (interval and, the most value, etc.).
Of course, in addition to the one-dimensional case of the ordinary line tree, there are in the two-dimensional plane line of the tree, similar can also achieve three-dimensional, four-dimension ... (AH. Qian Chin What do you want to do O ((>ω<)) O)
All right ~
For the one-dimensional line of the tree, we want to query the maximum value of a certain interval, first of all should be done ~ (the specific method omitted )
In the query, we can recursively search from the root node, as shown in the following figure, assuming that the Chahi between the two is [2,6].
This large interval of [2,6] is decomposed into disjoint three cells [2,3], [4,5], [6], and the final result is determined by the information maintained in these three nodes.
We assume that the query is still the interval minimum, so the final result is