Lv.3segment-treesparse-table
Range Minimum Queries
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
The altitude at each point of a trail is given by the integer array values, which never changes. For each
queries[j] = [l, r], return the minimum of values[l], values[l+1], …, values[r], in query order.
Examples
| values | queries | result |
|---|---|---|
[5, 2, 8, 6, 3, 7] | [[0, 5], [2, 3], [3, 5], [4, 4]] | [2, 6, 3, 3] |
[-1, -1, 0] | [[1, 2], [0, 0]] | [-1, -1] |
Efficiency
This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.
Function signature
solution(values: int[], queries: int[][]) → int[]
Constraints
-
values.length,values[i] -
queries.length,lr
Expected complexity
- Time
- O(N log N + Q)
- Space
- O(N log N)
Examples
| values | queries | Expected |
|---|---|---|
[5,2,8,6,3,7] | [[0,5],[2,3],[3,5],[4,4]] | [2,6,3,3] |
[-1,-1,0] | [[1,2],[0,0]] | [-1,-1] |
Hint
Hint
Build a sparse table t[k][i] = min(values[i .. i + 2^k - 1]) in ; answer [l, r] with the largest k such
that as min(t[k][l], t[k][r - 2^k + 1]) — overlapping halves do not matter for a minimum. A segment
tree ( per query) also works. Scanning every range is and exceeds the time limit.
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 222 ms | estimated |
| C++ (GCC, gnu++23) | 222 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 2248 ms | derived |
| Kotlin 2.4 (JVM) | 1444 ms | estimated |
| C# 14 (.NET 10) | 1444 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 2 | inputs and outputs |
| correctness | 40 | 10 | verdict only |
| performance | 60 | 4 | verdict only |