ڈسکریٹ میتھ کی مشق

Algorithms / Sparse Table Rmq

Least You Need to Know: Sparse Tables, RMQ, and Static Idempotent Queries

Sparse tables preprocess answers for intervals of length power of two. They shine on **static arrays** and especially on idempotent operations like minimum, where overlapping power-of-two blocks can answer a query in `O(1)`.

جو کم از کم جاننا ضروری ہے

اہم علامتیں

st[i][j] answer on interval starting at i of length `2^j`
RMQ(l, r) minimum on interval `[l, r]`
idempotent combining a value with itself does not change it

مختصر حل شدہ مثال

  • Precompute minimums for all intervals of lengths `1, 2, 4, 8, ...`.
  • To answer RMQ on `[l, r]`, choose the largest `2^k` fitting in the interval.
  • Compare the two precomputed blocks of length `2^k` covering the left and right ends.
  • Overlap is harmless for `min`, which is why the query is constant time.

عام غلطیاں

اس قسم کے سوال کو کیسے پہچانیں

مشق شروع کریں