Skip to content
AI360Xpert
Beta

Static Range Minimum Query

Official exercise: CSES

Problem in Plain English

Preprocess a fixed array to answer inclusive range minimum queries with a Sparse Table. Combine two overlapping power-of-two blocks in constant time.

Problem Statement

This page solves CSES Static Range Minimum Queries. Read n q, then n integers, then q pairs a b. For each pair print the minimum element from position a through b inclusive. Input endpoints are one-based; the array never changes. The public task has no difficulty rating. The complete programs below convert each endpoint to zero-based and print one result per line.

Constraints

  • 1 <= n,q <= 2 × 10^5.
  • 1 <= x[i] <= 10^9.
  • 1 <= a <= b <= n; endpoints are inclusive and one-based.
  • The input array is static; updates are outside this exercise.

Examples

Example 1

Input
8 4 3 2 4 5 1 1 5 3 2 4 5 6 1 8 3 3
Output
2 1 1 4

The four inclusive intervals have minima 2,1,1,4. The last query contains only the third value.

Example 2

Input
1 1 1000000000 1 1
Output
1000000000

A singleton uses two identical length-one blocks, whose minimum remains that value.

Example 3

Input
5 3 9 8 7 6 5 1 5 2 4 4 5
Output
5 6 5

Lengths five and three use overlapping blocks; the final length-two query uses one stored interval at both ends.

Intuition

Store minima for intervals of lengths 1,2,4,8 and so on. A query need not decompose into many disjoint blocks: choose the largest power of two no longer than the query and cover it with one block at each end. The two blocks overlap, but min(x,x)=x, so repeated elements do not change the result. In Example 1 the query [2,4] has length 3; blocks [2,3] and [3,4] both contain position 3 and their minima are 2 and 4. The answer is 2.

Approaches

Sparse Table

Optimal

Solution Details

Reveal Sparse Table: intuition, complexity, and code

Hints

Hint 1
Precompute blocks whose lengths double at each level.
Hint 2
Cover a query from both ends using its largest fitting power of two.
Hint 3
Overlapping minima are safe because min is idempotent; use right-width+1 as the second start.

Edge Cases

  • A one-element interval uses k=0 and the same cell twice.
  • A power-of-two length uses identical blocks, which must not cause double-counting concerns for min.
  • A full-array query can have a non-power-of-two length and therefore overlap.
  • Repeated minima are harmless, including inside the overlap.

Common Mistakes and Interview Tips

  • Using length right-left instead of right-left+1 fails singleton queries.
  • Forgetting to subtract one from each CSES endpoint shifts every query.
  • Using this overlapping formula for sums counts the overlap twice.
  • Assuming a point update changes only logarithmically many cells confuses Sparse Tables with Segment Trees.

Key Takeaway

Preprocessing can remove query work when the data is static. Overlap is safe for idempotent operations such as min, max and gcd; it is unsafe for addition without a different query decomposition.