<p>You are given two <strong>0-indexed</strong> arrays <code>nums1</code> and <code>nums2</code> and a 2D array <code>queries</code> of queries. There are three types of queries:</p>

<ol>
	<li>For a query of type 1, <code>queries[i]&nbsp;= [1, l, r]</code>. Flip the values from <code>0</code> to <code>1</code> and from <code>1</code> to <code>0</code> in <code>nums1</code>&nbsp;from index <code>l</code> to index <code>r</code>. Both <code>l</code> and <code>r</code> are <strong>0-indexed</strong>.</li>
	<li>For a query of type 2, <code>queries[i]&nbsp;= [2, p, 0]</code>. For every index <code>0 &lt;= i &lt; n</code>, set&nbsp;<code>nums2[i] =&nbsp;nums2[i]&nbsp;+ nums1[i]&nbsp;* p</code>.</li>
	<li>For a query of type 3, <code>queries[i]&nbsp;= [3, 0, 0]</code>. Find the sum of the elements in <code>nums2</code>.</li>
</ol>

<p>Return <em>an array containing all the answers to the third type&nbsp;queries.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums1 = [1,0,1], nums2 = [0,0,0], queries = [[1,1,1],[2,1,0],[3,0,0]]
<strong>Output:</strong> [3]
<strong>Explanation:</strong> After the first query nums1 becomes [1,1,1]. After the second query, nums2 becomes [1,1,1], so the answer to the third query is 3. Thus, [3] is returned.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums1 = [1], nums2 = [5], queries = [[2,0,0],[3,0,0]]
<strong>Output:</strong> [5]
<strong>Explanation:</strong> After the first query, nums2 remains [5], so the answer to the second query is 5. Thus, [5] is returned.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums1.length,nums2.length &lt;= 10<sup>5</sup></code></li>
	<li><code>nums1.length = nums2.length</code></li>
	<li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li>
	<li><code><font face="monospace">queries[i].length = 3</font></code></li>
	<li><code><font face="monospace">0 &lt;= l &lt;= r &lt;= nums1.length - 1</font></code></li>
	<li><code><font face="monospace">0 &lt;= p &lt;= 10<sup>6</sup></font></code></li>
	<li><code>0 &lt;= nums1[i] &lt;= 1</code></li>
	<li><code>0 &lt;= nums2[i] &lt;= 10<sup>9</sup></code></li>
</ul>

import math

class TransitionSegmentTree:

    def __init__(self, nums):
        # Round up to the nearest power of 2
        self.n = 1<<len(nums).bit_length()
        # We will store the tree in one array, starting from index 1
        self.tree = [0] * (self.n*2)
        self.buildTree(nums)

    def buildTree(self, nums):
        prev = 0
        
        # Instead of storing 1s and 0s, we will store indeces of transitions from 1 to 0 and 0 to 1
        # assuming that array starts at 0 and ends at 0
        for i,num in enumerate(nums+[0],self.n):
            # XOR returns 1 if only one is true
            if num^prev:
                self.tree[i] = -i # negative to distinguish index from sum
                prev = 1 - prev # flip bit

        for i in range(self.n-1, 0, -1):
            l, r = i*2, i*2+1
            self.calculateParent(i, l, r)
                
    def calculateParent(self, parentInd, l, r):
        # We store one of 2 possible things - a transition index or 
        # a number that will be joined with index later
        if self.tree[l] < 0 and self.tree[r] < 0: # two indeces
            self.tree[parentInd] = self.tree[l] - self.tree[r] # take diff
        elif self.tree[l] < 0 and self.tree[r] >= 0: # left is index and right is sum
            self.tree[parentInd] = self.tree[l] - self.tree[r] # move index right by sum
        elif self.tree[l] >=0 and self.tree[r] < 0: # left is sum and right is index
            self.tree[parentInd] = self.tree[l] + self.tree[r] # move index left by sum
        else:
            # both sums
            self.tree[parentInd] = self.tree[l] + self.tree[r] # put sums together
            
    def update(self, j: int):
        j += self.n
        if self.tree[j] < 0:
            self.tree[j] = 0
        else:
            self.tree[j] = -j

        while j > 1:
            if j%2 == 1:    l, r = j-1, j
            else:           l, r = j, j+1
            j //= 2
            self.calculateParent(j, l, r)
            
    def flipRange(self, l, r):
        self.update(l)
        self.update(r)
    
    def sum(self):
        return self.tree[1]



class Solution:
    def handleQuery(self, nums1: List[int], nums2: List[int], queries: List[List[int]]) -> List[int]:
        self.nums1 = nums1
        self.nums2 = nums2
        self.n = len(nums1)
        self.nums2_sum = sum(nums2)
        self.segmentTree = TransitionSegmentTree(nums1)

        result = []
        for query in queries:
            x, y, z = query
            if x == 1:
                self.segmentTree.flipRange(y, z + 1)
            elif x == 2:
                self.nums2_sum += self.segmentTree.sum() * y
            else:
                result.append(self.nums2_sum)
        return result
        

47900