Algorithmstree path xor query
Tree Path XOR Query on a Path
TT
Testlaa Team
May 15, 2026•1 min read
Path XOR on a tree often uses prefix XOR from root: xor(u,v) = pref[u] ^ pref[v] ^ lca_edge or HLD with XOR segtree.
Why this shows up in the real world
Time-series dashboards, game leaderboards, and competitive programming interval problems all need fast answers on changing arrays.
Core idea (explained for students)
Prefix trick is O(1) per query if only XOR and no updates; with updates use HLD + XOR lazy.
Try this in Python
def path_xor_pref(pref: list[int], u: int, v: int) -> int:
return pref[u] ^ pref[v]
print(path_xor_pref([0, 1, 2, 3], 1, 3))
Common mistakes
- XOR path without clearing LCA double-count.
- Wrong root for prefix array.
Key takeaways
- Draw small tree, compute pref manually.
- Updates break prefix-only—switch to HLD.
Tags:
Segment tree & range queriesPythonStudents
