Handling Sum Queries After Update Problem
Description
LeetCode Problem 2569.
You are given two 0-indexed arrays nums1 and nums2 and a 2D array queries of queries. There are three types of queries:
- For a query of type 1,
queries[i] = [1, l, r]. Flip the values from0to1and from1to0innums1from indexlto indexr. Bothlandrare 0-indexed. - For a query of type 2,
queries[i] = [2, p, 0]. For every index0 <= i < n, setnums2[i] = nums2[i] + nums1[i] * p. - For a query of type 3,
queries[i] = [3, 0, 0]. Find the sum of the elements innums2.
Return an array containing all the answers to the third type queries.
Example 1:
Input: nums1 = [1,0,1], nums2 = [0,0,0], queries = [[1,1,1],[2,1,0],[3,0,0]]
Output: [3]
Explanation: 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.
Example 2:
Input: nums1 = [1], nums2 = [5], queries = [[2,0,0],[3,0,0]]
Output: [5]
Explanation: After the first query, nums2 remains [5], so the answer to the second query is 5. Thus, [5] is returned.
Constraints:
1 <= nums1.length,nums2.length <= 10^5nums1.length = nums2.length1 <= queries.length <= 10^5queries[i].length = 30 <= l <= r <= nums1.length - 10 <= p <= 10^60 <= nums1[i] <= 10 <= nums2[i] <= 10^9
Solution
Approach: Segment Tree with Lazy Flip
Only the number of ones in nums1 matters. Operation 2 adds p * (number of ones in nums1) to sum(nums2), operation 3 reports sum(nums2), and operation 1 flips the bits of a range of nums1.
A segment tree stores the count of ones per segment. Flipping a segment turns its count c into length - c, and a lazy flag defers the flip to the children. Keep s = sum(nums2) as a plain number, update it on operation 2 with the root's count, and append it on operation 3.
Complexity: time O(n + m log n), space O(n), where n is the length of nums1 and m the number of queries.
class Node:
def __init__(self):
self.l = self.r = 0
self.s = self.lazy = 0
class SegmentTree:
def __init__(self, nums):
self.nums = nums
n = len(nums)
self.tr = [Node() for _ in range(n << 2)]
self.build(1, 1, n)
def build(self, u, l, r):
self.tr[u].l, self.tr[u].r = l, r
if l == r:
self.tr[u].s = self.nums[l - 1]
return
mid = (l + r) >> 1
self.build(u << 1, l, mid)
self.build(u << 1 | 1, mid + 1, r)
self.pushup(u)
def modify(self, u, l, r):
if self.tr[u].l >= l and self.tr[u].r <= r:
self.tr[u].lazy ^= 1
self.tr[u].s = self.tr[u].r - self.tr[u].l + 1 - self.tr[u].s
return
self.pushdown(u)
mid = (self.tr[u].l + self.tr[u].r) >> 1
if l <= mid:
self.modify(u << 1, l, r)
if r > mid:
self.modify(u << 1 | 1, l, r)
self.pushup(u)
def query(self, u, l, r):
if self.tr[u].l >= l and self.tr[u].r <= r:
return self.tr[u].s
self.pushdown(u)
mid = (self.tr[u].l + self.tr[u].r) >> 1
res = 0
if l <= mid:
res += self.query(u << 1, l, r)
if r > mid:
res += self.query(u << 1 | 1, l, r)
return res
def pushup(self, u):
self.tr[u].s = self.tr[u << 1].s + self.tr[u << 1 | 1].s
def pushdown(self, u):
if self.tr[u].lazy:
mid = (self.tr[u].l + self.tr[u].r) >> 1
self.tr[u << 1].s = mid - self.tr[u].l + 1 - self.tr[u << 1].s
self.tr[u << 1].lazy ^= 1
self.tr[u << 1 | 1].s = self.tr[u].r - mid - self.tr[u << 1 | 1].s
self.tr[u << 1 | 1].lazy ^= 1
self.tr[u].lazy ^= 1
class Solution:
def handleQuery(
self, nums1: List[int], nums2: List[int], queries: List[List[int]]
) -> List[int]:
tree = SegmentTree(nums1)
s = sum(nums2)
ans = []
for op, a, b in queries:
if op == 1:
tree.modify(1, a + 1, b + 1)
elif op == 2:
s += a * tree.query(1, 1, len(nums1))
else:
ans.append(s)
return ans