Range XOR Queries with Subarray Reversals Problem
Description
LeetCode Problem 3526.
You are given an integer array nums of length n and a 2D integer array queries of length q, where each query is one of the following three types:
- Update:
queries[i] = [1, index, value]Setnums[index] = value. - Range XOR Query:
queries[i] = [2, left, right]Compute the bitwise XOR of all elements in the subarraynums[left...right], and record this result. - Reverse Subarray:
queries[i] = [3, left, right]Reverse the subarraynums[left...right]in place.
Return an array of the results of all range XOR queries in the order they were encountered.
Example 1:
Input: nums = [1,2,3,4,5], queries = [[2,1,3],[1,2,10],[3,0,4],[2,0,4]]
Output: [5,8]
Explanation:
- Query 1: [2, 1, 3] – Compute XOR of subarray [2, 3, 4] resulting in 5.
- Query 2: [1, 2, 10] – Update nums[2] to 10, updating the array to [1, 2, 10, 4, 5].
- Query 3: [3, 0, 4] – Reverse the entire array to get [5, 4, 10, 2, 1].
- Query 4: [2, 0, 4] – Compute XOR of subarray [5, 4, 10, 2, 1] resulting in 8.
Example 2:
Input: nums = [7,8,9], queries = [[1,0,3],[2,0,2],[3,1,2]]
Output: [2]
Explanation:
- Query 1: [1, 0, 3] – Update nums[0] to 3, updating the array to [3, 8, 9].
- Query 2: [2, 0, 2] – Compute XOR of subarray [3, 8, 9] resulting in 2.
- Query 3: [3, 1, 2] – Reverse the subarray [8, 9] to get [9, 8].
Constraints:
1 <= nums.length <= 10^50 <= nums[i] <= 10^91 <= queries.length <= 10^5queries[i].length == 3queries[i][0] ∈ {1, 2, 3}- If
queries[i][0] == 1:0 <= index < nums.length0 <= value <= 10^9
- If
queries[i][0] == 2orqueries[i][0] == 3:0 <= left <= right < nums.length
Solution
Approach: Implicit Treap with Lazy Reversal
We need point assignment, range XOR and range reversal, each in logarithmic time, with n and q up to 105. A plain array makes reversal linear, so we use an implicit treap, a balanced binary tree keyed by position in the sequence rather than by value.
Every node stores its value, the XOR of its subtree, the subtree size and a lazy "reversed" flag. Two operations are enough:
split(t, k)cuts the tree into its firstkelements and the rest, andmerge(a, b)joins two trees back together.- Reversing a subtree only toggles its flag; when a flag is pushed down, the children are swapped and the flag moves to them.
For each query, split out the nodes of the affected range. An update changes the single node's value and subtree XOR, a XOR query reads the subtree XOR, and a reverse toggles the flag. Then merge everything back.
Complexity: O((n + q) log n) expected time and O(n) space.
import random
class Solution:
def getResults(self, nums: List[int], queries: List[List[int]]) -> List[int]:
# Implicit treap: nodes are 1..n, node 0 is the empty tree.
n = len(nums)
lc = [0] * (n + 1)
rc = [0] * (n + 1)
pri = [random.random() for _ in range(n + 1)]
val = [0] + nums
xr = [0] + nums
sz = [0] + [1] * n
rev = [False] * (n + 1)
def push_down(t: int) -> None:
if rev[t]:
lc[t], rc[t] = rc[t], lc[t]
for c in (lc[t], rc[t]):
if c:
rev[c] = not rev[c]
rev[t] = False
def push_up(t: int) -> None:
sz[t] = sz[lc[t]] + sz[rc[t]] + 1
xr[t] = xr[lc[t]] ^ xr[rc[t]] ^ val[t]
# xr and sz of the children are not affected by a pending reverse flag
def split(t: int, k: int):
# first k nodes go left
if not t:
return 0, 0
push_down(t)
if sz[lc[t]] >= k:
a, b = split(lc[t], k)
lc[t] = b
push_up(t)
return a, t
a, b = split(rc[t], k - sz[lc[t]] - 1)
rc[t] = a
push_up(t)
return t, b
def merge(a: int, b: int) -> int:
if not a or not b:
return a or b
if pri[a] > pri[b]:
push_down(a)
rc[a] = merge(rc[a], b)
push_up(a)
return a
push_down(b)
lc[b] = merge(a, lc[b])
push_up(b)
return b
root = 0
for i in range(1, n + 1):
root = merge(root, i)
ans = []
for q in queries:
if q[0] == 1:
a, bc = split(root, q[1])
b, c = split(bc, 1)
val[b] = q[2]
xr[b] = q[2]
root = merge(merge(a, b), c)
else:
a, bc = split(root, q[1])
b, c = split(bc, q[2] - q[1] + 1)
if q[0] == 2:
ans.append(xr[b])
else:
rev[b] = not rev[b]
root = merge(merge(a, b), c)
return ans