Maximum#4473

Maximum (Editorial)

The problem requires answering range maximum queries. The array elements do not change, so a static data structure is suitable. The number of queries reaches 5 \cdot 10^5, and the length of the array is 10^5. Computing the maximum of each segment directly would require up to 5 \cdot 10^{10} operations, which is too slow.

We use a sparse table for the RMQ problem, but instead of the minimum we look for the maximum (Range Maximum Query). The maximum, like the minimum, does not change if some element is counted twice. Therefore, the query segment can be covered by two overlapping segments of length 2^k, and the larger of their maximums is chosen.

A table cell can store either the index of the maximum element or the maximum itself. The problem requires printing only the value, so it is enough to store the maximum. The index variant is needed when the position of the maximum must be known. Both implementations are given below.

By the problem statement, 1 \le l, r \le n, and it is not guaranteed that l \le r. Therefore, before executing a query, we swap the bounds if necessary.

Complexity. Building the sparse table takes O(n \log n), and each query takes . The overall complexity is O(n \log n + q).

Example

Consider the array from the example: (5, 1, 2, 8, 7, 6, 10, 7, 5, 6). The table , in which mas[i][j] is the maximum on the segment [i; i + 2^j - 1], is shown below. A dash means that the segment goes beyond the array.

\boldsymbol{j \setminus i}

—

—

—

—

—

—

—

—

—

—

—

For example, mas[4][2] = \max(mas[4][1], mas[6][1]) = \max(8, 10) = 10 — this is the maximum on the segment .

Consider the query . The length of the segment is , and the largest power of two not exceeding is , so k = 2. The segment is covered by the segments and , which overlap on the segment :

The answer is \max(mas[2][2], mas[3][2]) = \max(8, 8) = 8. The element belongs to both segments, but this does not affect the value of the maximum.

All queries from the example are listed in the table:

query

segments

maximums

answer

,

,

,

,

,

,

,

,

For segments of length and (queries , and ), both covering segments coincide with the query segment itself.

Algorithm implementation — index of the maximum

The input sequence a_i (n \le 10^5) is stored in the array starting from index . In this variant, the cell mas[i][j] stores the index of the maximum element on the segment [i; i + 2^j - 1], and the maximum itself equals a[mas[i][j]]. Since 2^{16} \le 10^5 < 2^{17}, LOGMAX = 17 is enough for the second dimension.

#define MAX 100010
#define LOGMAX 17
int mas[MAX][LOGMAX];
int a[MAX];

The function Build_RMQ_Array builds the sparse table of indices.

void Build_RMQ_Array()
{

For j = 0, the segment consists of one element, so mas[i][0] = i.

  for (int i = 1; i <= n; i++) mas[i][0] = i;

For j > 0, compare the elements of the array with indices mas[i][j - 1] and mas[i + 2^{j-1}][j - 1] — the maximums on the two halves of the segment — and store the index of the larger one.

  for (int j = 1; (1 << j) <= n; j++)
    for (int i = 1; i + (1 << j) - 1 <= n; i++)
      if (a[mas[i][j - 1]] > a[mas[i + (1 << (j - 1))][j - 1]])
        mas[i][j] = mas[i][j - 1];
      else mas[i][j] = mas[i + (1 << (j - 1))][j - 1];
}

The function RangeMaxQuery returns the index of the maximum element on the segment .

int RangeMaxQuery(int i, int j)
{
  int temp, k = 0;

If i > j, the bounds are swapped.

  if (i > j) temp = i, i = j, j = temp;

Then, in a loop, find the largest such that 2^k \le j - i + 1.

  while ((1 << (k + 1)) <= j - i + 1) k++;

Compare the elements with indices mas[i][k] and mas[j - 2^k + 1][k] and return the index of the larger one.

  return (a[mas[i][k]] >= a[mas[j - (1 << k) + 1][k]]) ?
         mas[i][k] : mas[j - (1 << k) + 1][k];
}

The main part of the program. Read the size of the array and its elements, starting from index .

scanf("%d", &n);
for (i = 1; i <= n; i++)
  scanf("%d", &a[i]);

Build the sparse table from the array .

Build_RMQ_Array();

Read the number of queries .

scanf("%d", &q);

For each query, the function RangeMaxQuery returns the index of the maximum element on the segment , and we print the element itself.

for (i = 0; i < q; i++)
{
  scanf("%d %d", &u, &v);
  int index = RangeMaxQuery(u, v);
  printf("%d\n", a[index]);
}

Algorithm implementation — maximum

The input sequence a_i (n \le 10^5) is stored in the array starting from index . In this variant, the cell mas[i][j] stores the maximum itself on the segment [i; i + 2^j - 1]. Since 2^{16} \le 10^5 < 2^{17}, LOGMAX = 17 is enough for the second dimension.

#define MAX 100010
#define LOGMAX 17
int mas[MAX][LOGMAX];
int a[MAX];

The function Build_RMQ_Array builds the sparse table from the array :

mas[i][j] = \max(b[i], b[i + 1], \ldots, b[i + 2^j - 1])
void Build_RMQ_Array(int *b)
{

For j = 0, the sparse table contains the original array elements: mas[i][0] = b[i].

  for (int i = 1; i <= n; i++) mas[i][0] = b[i];

For j > 0, the value mas[i][j] equals the larger of the maximums on the two halves of the segment.

  for (int j = 1; 1 << j <= n; j++)
    for (int i = 1; i + (1 << j) - 1 <= n; i++)
      mas[i][j] = max(mas[i][j - 1], mas[i + (1 << (j - 1))][j - 1]);
}

The function RangeMaxQuery returns the maximum on the segment .

int RangeMaxQuery(int i, int j)
{
  int temp, k = 0;

If i > j, the bounds are swapped.

  if (i > j) temp = i, i = j, j = temp;

In a loop, find the largest such that 2^k \le j - i + 1. This way of computing does not require the logarithm function and does not depend on floating-point errors.

  while ((1 << (k + 1)) <= j - i + 1) k++;

The answer is the larger of the maximums on the segments [i; i + 2^k - 1] and [j - 2^k + 1; j].

  return max(mas[i][k],mas[j - (1<<k) + 1][k]);
}

The main part of the program. Read the size of the array and its elements, starting from index .

scanf("%d",&n);
for(i = 1; i <= n; i++) scanf("%d",&a[i]);

Build the sparse table from the array .

Build_RMQ_Array(a);

Read the number of queries . For each query, print the maximum on the segment .

scanf("%d",&q);
for(i = 0; i < q; i++)
{
  scanf("%d %d",&u,&v);
  printf("%d\n",RangeMaxQuery(u,v));
}