Data Structures & AlgorithmsModerateCodeforces Div2 B · prefix techniques

Problem: What is the complexity of computing all prefix XOR values of an array of n = 10^6 integers and then answering q = 10^6 range-XOR queries online?

  1. A. O(n * q) by recomputing each range
  2. B. O(n + q) using prefix XOR, since x XOR x = 0 lets each query use two prefix values
  3. C. O(n log q) with a segment tree needed
  4. D. O(q * sqrt(n)) for every query

Correct answer: B. O(n + q) using prefix XOR, since x XOR x = 0 lets each query use two prefix values

Range XOR of [l, r] equals pre[r+1] XOR pre[l], because equal values cancel. The trap is using a segment tree when the operation is invertible.

More Coding Interview Problems questions

Exams that ask this topic