Range XOR Queries with Subarray Reversals - Problem
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] - Set nums[index] = value.
Range XOR Query: queries[i] = [2, left, right] - Compute the bitwise XOR of all elements in the subarray nums[left...right], and record this result.
Reverse Subarray: queries[i] = [3, left, right] - Reverse the subarray nums[left...right] in place.
Return an array of the results of all range XOR queries in the order they were encountered.
Input & Output
Example 1 — Basic Mixed Operations
$
Input:
nums = [1,3,4,8], queries = [[2,0,1],[1,1,5],[2,0,1],[3,0,3],[2,0,3]]
›
Output:
[2,4,6]
💡 Note:
Query [2,0,1]: XOR of nums[0..1] = 1⊕3 = 2. Query [1,1,5]: Update nums[1] = 5. Query [2,0,1]: XOR of nums[0..1] = 1⊕5 = 4. Query [3,0,3]: Reverse nums[0..3] → [8,4,5,1]. Query [2,0,3]: XOR of nums[0..3] = 8⊕4⊕5⊕1 = 6.
Example 2 — Only XOR Queries
$
Input:
nums = [2,3,4], queries = [[2,0,2],[2,1,2],[2,0,1]]
›
Output:
[5,7,1]
💡 Note:
Query [2,0,2]: XOR of nums[0..2] = 2⊕3⊕4 = 5. Query [2,1,2]: XOR of nums[1..2] = 3⊕4 = 7. Query [2,0,1]: XOR of nums[0..1] = 2⊕3 = 1.
Example 3 — Update and Reverse
$
Input:
nums = [5,6], queries = [[1,0,2],[3,0,1],[2,0,1]]
›
Output:
[4]
💡 Note:
Query [1,0,2]: Update nums[0] = 2, so nums = [2,6]. Query [3,0,1]: Reverse nums[0..1] → [6,2]. Query [2,0,1]: XOR of nums[0..1] = 6⊕2 = 4.
Constraints
- 1 ≤ nums.length ≤ 105
- 1 ≤ queries.length ≤ 105
- 0 ≤ nums[i] ≤ 106
- For update queries: 0 ≤ index < nums.length, 0 ≤ value ≤ 106
- For range queries: 0 ≤ left ≤ right < nums.length
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array nums and queries of three types
2
Process
Handle updates, XOR queries, and reversals
3
Output
Return results of all XOR queries
Key Takeaway
🎯 Key Insight: Process queries sequentially since XOR properties are preserved through array modifications
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code