Data Structures19 sections · 729 units
Open in Course

Idempotent Operations

When overlap trick works

The overlap trick works for idempotent operations: f(a,a)=af(a, a) = a.

Idempotent:

  • min⁡\min: min⁡(a,a)=a\min(a, a) = a ✓
  • max⁡\max: max⁡(a,a)=a\max(a, a) = a ✓
  • gcd⁡\gcd: gcd⁡(a,a)=a\gcd(a, a) = a ✓
  • Bitwise AND: a∧a=aa \land a = a ✓
  • Bitwise OR: a∨a=aa \lor a = a ✓

NOT idempotent:

  • Sum: a+a=2a≠aa + a = 2a \neq a ✗
  • Product: a⋅a=a2≠aa \cdot a = a^2 \neq a ✗
  • XOR: a⊕a=0≠aa \oplus a = 0 \neq a ✗
  • Count: overlapping counts twice ✗ For non-idempotent operations, you can still use Sparse Table, but queries become O(log⁡n)O(\log n) because you must use non-overlapping ranges. Idempotent query: O(1)O(1). Non-idempotent query: O(log⁡n)O(\log n). Space: O(nlog⁡n)O(n \log n).