843. Range XOR Queries With Subarray Reversals

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:

  1. Update:Β queries[i] = [1, index, value]
  2. SetΒ nums[index] = value.
  3. Range XOR Query:Β queries[i] = [2, left, right]
  4. Compute the bitwise XOR of all elements in theΒ subarrayΒ nums[left...right], and record this result.
  5. Reverse Subarray:Β queries[i] = [3, left, right]
  6. 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.

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].

Now Your Turn!

Pick the correct output for the given input

Input: nums = [1,2,3], queries = [[2,1,2]]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= nums.length <= 105
  • 0 <= nums[i] <= 109
  • 1 <= queries.length <= 105
  • queries[i].length == 3​
  • queries[i][0] ∈ {1, 2, 3}​
  • IfΒ queries[i][0] == 1:​
  • 0 <= index < nums.length​
  • 0 <= value <= 109
  • IfΒ queries[i][0] == 2Β orΒ queries[i][0] == 3:​
  • 0 <= left <= right < nums.length​

Hints

Frequently Occurring Doubts

Fun Facts

0
class Solution {
public:
vector<int> getResults(vector<int> nums, vector<vector<int>> queries) {
}
};
Test Case

Input:

Nums
Queries