Graph Theory37 sections · 1633 units
Open in Course

Sparse Table for RMQ

(Precompute range minimums)

A sparse table precomputes the minimum over every range of length 2k2^k for all starting positions. For each index ii and power kk, store st[i][k] = min(tourDepth[i], ..., tourDepth[i + 2^k - 1]). To query min(l,r)min(l, r), find the largest kk such that 2krl+12^k \leq r - l + 1.

The answer is min(st[l][k], st[r - 2^k + 1][k]). These two ranges overlap but cover [l,r][l, r]. Preprocessing takes O(nlogn)O(n \log n), queries take O(1)O(1).

Space complexity is O(nlogn)O(n \log n) for the data structures used.