Data Structures19 sections · 729 units
Open in Course

Static RMQ Solution

Implement your solution

Use the Sparse Table template from earlier. Optimizations for competitive programming:

1.1. Precompute LOG array to avoid log calls

2.2. Use arrays instead of dynamic lists for speed

3.3. Read input efficiently

function main():
    read n, q
    read array arr of size n

    // Build sparse table
    st = SparseTable(arr)

    // Answer queries
    results = empty list
    for i = 0 to q - 1:
        read l, r
        results.append(st.query(l - 1, r - 1))  // 0-indexed

    print results, one per line