What Is a Range Query?

Range minimum query (Range Minimum Query) is a conditional query for the data set. If an array is given, the range value query specifies a range condition, and if this condition is met, the largest or smallest element in the given array is required to be fetched.

If
On condition
Range max query returns 1 which is a subarray
The smallest element.
Normally, array A is
Create a two-dimensional array
Integer value.
In this array
Representation range
Maximum in (e.g.
The maximum value in is counted as
).
The process of building the array can be found in
Finish in time. The specific algorithm is similar to dynamic programming. The following is the code described in C ++. The array convention starts from 0:
  void Initialise (vector <int> & Array)
 {
 int Length = (int) Array.size ();

 // When the length is 0, it means the data itself.
 for (int i = 0; i <Length; ++ i) F [i] [0] = Array [i];

 for (int j = 1; (1 << j) <= Length; ++ j)
 for (int i = 0; i + (1 << j)-1 <Length; ++ i)
 F [i] [j] = min (
 F [i] [j-1], F [i + (1 << (j-1))] [j-1]
 ); // split into two equal lengths}
When we need to calculate the maximum value in an interval (for example
Time), we first find the length of this interval (ie 8-3 + 1 = 6).
Then we calculate the maximum that is not greater than it
The exponent of the value (ie 2). We want to query two lengths that start with 3 and end with 8 as
The maximum value of the interval.
Obviously
with
That is (
with
The maximum value is
The maximum value. The query time is constant and the code is as follows:
  int Query (int Left, int Right)
 {
 int i = 0;

 while (1 << (i + 1) <= Right-Left + 1) ++ i;

 return min (
 F [Left] [i], F [Right-(1 << i) + 1] [i]
 );
 }
The time complexity of the entire program is
, Q is the number of queries.

IN OTHER LANGUAGES

Was this article helpful? Thanks for the feedback Thanks for the feedback

How can we help? How can we help?