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
- Normally, array A is
- Create a two-dimensional array
- In this array
- The process of building the array can be found in
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
- Then we calculate the maximum that is not greater than it
- Obviously
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