Skip to content

Latest commit

 

History

History
20 lines (18 loc) · 672 Bytes

2569.md

File metadata and controls

20 lines (18 loc) · 672 Bytes

2569. Handling Sum Queries After Update

Solution 1 (time O(n), space O(1))

class Solution:
    def handleQuery(self, nums1: List[int], nums2: List[int], queries: List[List[int]]) -> List[int]:
        ans = []
        n1 = int(''.join([str(d) for d in nums1]), 2)
        sum1, sum2 = n1.bit_count(), sum(nums2)
        for t, left, right in queries:
            if t == 1:
                n1 ^= int('1' * (right - left + 1) + '0' * (len(nums1) - right - 1), 2)
            elif t == 2:
                sum1 = n1.bit_count()
                sum2 += left * sum1
            else:
                ans.append(sum2)
        return ans