Data Structures19 sections · 729 units
Open in Course

Problem - Range Xor Queries

XOR is not idempotent

Given an array and queries, find XOR of elements in range [l,r][l, r]. XOR is NOT idempotent: aa=0aa \oplus a = 0 \neq a. For XOR, you can't use the overlap trick. Options:

1.1. Prefix XOR: xor(l,r)=prefix(r)prefix(l1)\text{xor}(l, r) = \text{prefix}(r) \oplus \text{prefix}(l-1). Build in O(n)O(n), query in O(1)O(1).

2.2. Disjoint Sparse Table: O(nlogn)O(n \log n) build, O(1)O(1) query.

3.3. Regular Sparse Table: O(nlogn)O(n \log n) build, O(logn)O(\log n) query (non-overlapping ranges). For XOR particularly, prefix XOR is simplest and fastest.