Problem solution · C++

Range XOR Queries with Subarray Reversals

Range XOR Queries with Subarray Reversals: a C++ solution using sliding window or two pointers. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Sliding window or two pointers
Source
walkccc LeetCode Solutions
Length
136 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Sliding window or two pointers

For Range XOR Queries with Subarray Reversals, the implementation maintains a moving interval and updates only the information that enters or leaves the window.

  1. Choose the invariant that makes a window valid or useful.
  2. Advance the right boundary and add the new element.
  3. Move the left boundary only as needed while maintaining the invariant and updating the answer.

Code notes

  • 136 lines of C++ from the credited upstream file 3526.cpp.
  • The implementation visibly relies on sequence storage.
  • 2 loop blocks detected.

Complexity

Confirm that neither pointer moves backwards; if so, the scan is usually linear apart from the window’s data-structure operations.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeRange XOR Queries with Subarray Reversals · C++C++
Use this to learn the idea, then write your own version.
struct Node {  Node(int v) : val(v), subXor(v) {}  int val;  int subXor;  int sz = 1;  int rev = false;  int prior = rand();  Node* l = nullptr;  Node* r = nullptr;}; class AVLTree { public:  AVLTree(const vector<int>& nums) : root(nullptr) {    build(nums);  }   void updateValue(int index, int val) {    Node* l = nullptr;    Node* r = nullptr;    Node* m = nullptr;    split(root, index, l, r);    split(r, 1, m, r);    if (m != nullptr)      m->val = val;    update(m);    root = merge(merge(l, m), r);  }   int rangeXor(int left, int right) {    Node* l = nullptr;    Node* m = nullptr;    Node* r = nullptr;    split(root, left, l, r);    split(r, right - left + 1, m, r);    const int res = getXor(m);    root = merge(merge(l, m), r);    return res;  }   void reverseRange(int left, int right) {    Node* l = nullptr;    Node* m = nullptr;    Node* r = nullptr;    split(root, left, l, r);    split(r, right - left + 1, m, r);    if (m != nullptr)      m->rev = !m->rev;    root = merge(merge(l, m), r);  }  private:  Node* root;   void build(const vector<int>& nums) {    for (const int num : nums)      root = merge(root, new Node(num));  }   int getSize(Node* t) {    return t ? t->sz : 0;  }   int getXor(Node* t) {    return t ? t->subXor : 0;  }   void push(Node* t) {    if (t == nullptr || !t->rev)      return;    swap(t->l, t->r);    if (t->l != nullptr)      t->l->rev ^= 1;    if (t->r != nullptr)      t->r->rev ^= 1;    t->rev = false;  }   void update(Node* t) {    if (t == nullptr)      return;    t->sz = 1 + getSize(t->l) + getSize(t->r);    t->subXor = t->val ^ getXor(t->l) ^ getXor(t->r);  }   void split(Node* t, int k, Node*& l, Node*& r) {    if (t == nullptr)      return void(l = r = nullptr);    push(t);    if (getSize(t->l) >= k) {      split(t->l, k, l, t->l);      r = t;    } else {      split(t->r, k - getSize(t->l) - 1, t->r, r);      l = t;    }    update(t);  }   Node* merge(Node* l, Node* r) {    push(l);    push(r);    if (l == nullptr || r == nullptr)      return l == nullptr ? r : l;    if (l->prior > r->prior) {      l->r = merge(l->r, r);      update(l);      return l;    } else {      r->l = merge(l, r->l);      update(r);      return r;    }  }}; class Solution { public:  vector<int> getResults(vector<int>& nums, vector<vector<int>>& queries) {    AVLTree tree(nums);    vector<int> ans;     for (const vector<int>& query : queries) {      const int type = query[0];      if (type == 1)        tree.updateValue(/*index=*/query[1], /*val=*/query[2]);      else if (type == 2)        ans.push_back(tree.rangeXor(/*left=*/query[1], /*right=*/query[2]));      else if (type == 3)        tree.reverseRange(/*left=*/query[1], /*right=*/query[2]);    }     return ans;  }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗