Problem solution · Python

Range Xor Queries with Subarray Reversals

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

Technique
Sliding window or two pointers
Source
Kamyu LeetCode Solutions
Length
131 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

  • 131 lines of Python from the credited upstream file range-xor-queries-with-subarray-reversals.py.
  • The implementation visibly relies on sequence storage.
  • No explicit 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 Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeRange Xor Queries with Subarray Reversals · PythonPython
Use this to learn the idea, then write your own version.
# Time:  O(n + qlogn)# Space: O(n)# pass in Python3 but Python2 import random  # template from: https://cp-algorithms.com/data_structures/treap.htmlclass TreapNode(object):    __slots__ = ('value', 'prior', 'cnt', 'xor_sum', 'rev', 'l', 'r')     def __init__(self, value):        self.value = value        self.prior = random.randint(1, 1 << 30)        self.cnt = 1        self.xor_sum = value        self.rev = False        self.l = None        self.r = None def cnt(t):    return t.cnt if t else 0 def xor_sum(t):    return t.xor_sum if t else 0 def upd_cnt(t):    if t:        t.cnt = 1 + cnt(t.l) + cnt(t.r)        t.xor_sum = t.value ^ xor_sum(t.l) ^ xor_sum(t.r) def push(t):    if t and t.rev:        t.rev = False        t.l, t.r = t.r, t.l        if t.l:            t.l.rev ^= True        if t.r:            t.r.rev ^= True def merge(l, r):    push(l)    push(r)    if not l or not r:        return l or r    if l.prior > r.prior:        l.r = merge(l.r, r)        upd_cnt(l)        return l    else:        r.l = merge(l, r.l)        upd_cnt(r)        return r def split(t, key, add=0):    if not t:        return (None, None)    push(t)    cur_key = add + cnt(t.l)    if key <= cur_key:        l, t.l = split(t.l, key, add)        upd_cnt(t)        return (l, t)    else:        t.r, r = split(t.r, key, add + 1 + cnt(t.l))        upd_cnt(t)        return (t, r) def reverse(t, l, r):    t1, t2 = split(t, l)    t2, t3 = split(t2, r - l + 1)    if t2:        t2.rev ^= True    return merge(merge(t1, t2), t3) def heapify(t):    if not t:        return    mx = t    if t.l and t.l.prior > mx.prior:        mx = t.l    if t.r and t.r.prior > mx.prior:        mx = t.r    if mx != t:        t.prior, mx.prior = mx.prior, t.prior        heapify(mx) def build(a, i, n):    if not n:        return None    mid = n // 2    t = TreapNode(a[i + mid])    t.l = build(a, i, mid)    t.r = build(a, i + mid + 1, n - mid - 1)    heapify(t)    upd_cnt(t)    return t # treapclass Solution(object):    def getResults(self, nums, queries):        """        :type nums: List[int]        :type queries: List[List[int]]        :rtype: List[int]        """        def update(root, index, value):            left, mid = split(root, index)            mid, right = split(mid, 1)            mid.value = value            upd_cnt(mid)            return merge(merge(left, mid), right)         def query(root, left, right):            t1, t2 = split(root, left)            t2, t3 = split(t2, right - left + 1)            result = xor_sum(t2)            return merge(merge(t1, t2), t3), result         result = []        root = build(nums, 0, len(nums))        for q in queries:            if q[0] == 1:                root = update(root, q[1], q[2])            elif q[0] == 2:                root, x = query(root, q[1], q[2])                result.append(x)            elif q[0] == 3:                root = reverse(root, q[1], q[2])        return result 

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 ↗