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: aa=aa \land a = a
  • Bitwise OR: aa=aa \lor a = a

NOT idempotent:

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