Handling Sum Queries After Update - Problem

You are given two 0-indexed arrays nums1 and nums2 and a 2D array queries of queries. There are three types of queries:

Type 1: queries[i] = [1, l, r]. Flip the values from 0 to 1 and from 1 to 0 in nums1 from index l to index r (both inclusive).

Type 2: queries[i] = [2, p, 0]. For every index 0 <= i < n, set nums2[i] = nums2[i] + nums1[i] * p.

Type 3: queries[i] = [3, 0, 0]. Find the sum of all elements in nums2.

Return an array containing all the answers to the third type queries.

Input & Output

Example 1 — Basic Operations
$ Input: nums1 = [1,0,1], nums2 = [0,0,0], queries = [[1,1,1],[2,1,0],[3,0,0]]
Output: [3]
💡 Note: Query [1,1,1]: flip nums1[1] from 0→1, so nums1=[1,1,1]. Query [2,1,0]: add nums1[i]*1 to nums2[i], so nums2=[1,1,1]. Query [3,0,0]: sum of nums2 = 1+1+1 = 3.
Example 2 — Multiple Sum Queries
$ Input: nums1 = [1,0,1,0], nums2 = [1,2,3,4], queries = [[3,0,0],[2,3,0],[3,0,0],[1,0,3],[3,0,0]]
Output: [10,16,16]
💡 Note: Initial sum=10. After [2,3,0]: add nums1[i]*3 to each nums2[i], giving [4,2,6,4], sum=16. After [1,0,3]: flip nums1[0:3] from [1,0,1,0] to [0,1,0,0], but nums2 remains [4,2,6,4], sum=16.
Example 3 — Range Flip Edge Case
$ Input: nums1 = [1], nums2 = [5], queries = [[2,2,0],[3,0,0],[1,0,0],[2,3,0],[3,0,0]]
Output: [7,7]
💡 Note: [2,2,0]: nums2[0] += 1*2 = 7. [3,0,0]: sum=7. [1,0,0]: flip nums1[0] to 0. [2,3,0]: add 0*3=0. [3,0,0]: sum=7.

Constraints

  • 1 ≤ nums1.length, nums2.length ≤ 105
  • 0 ≤ nums1[i] ≤ 1
  • 1 ≤ nums2[i] ≤ 109
  • 1 ≤ queries.length ≤ 105
  • queries[i].length == 3
  • 1 ≤ queries[i][0] ≤ 3
  • For queries of type 1: 0 ≤ l ≤ r < nums1.length
  • For queries of type 2: 1 ≤ p ≤ 106

Visualization

Tap to expand
Handling Sum Queries After Update INPUT nums1: 1 0 1 i=0 i=1 i=2 nums2: 0 0 0 queries: [1, 1, 1] - Flip l=1 to r=1 [2, 1, 0] - Add nums1*1 [3, 0, 0] - Get sum 3 query types to process ALGORITHM STEPS 1 Process Query [1,1,1] Flip nums1[1]: 0 --> 1 1 1 1 flipped! 2 Process Query [2,1,0] nums2[i] += nums1[i] * 1 nums2 = [0+1, 0+1, 0+1] 1 1 1 3 Process Query [3,0,0] Calculate sum(nums2) sum = 1 + 1 + 1 = 3 4 Count Optimization Track count of 1s in nums1 Use segment tree for flips O(log n) per operation FINAL RESULT Final nums1: 1 1 1 Final nums2: 1 1 1 Output Array: [3] OK - Verified! sum(nums2) = 3 Key Insight: Count-Based Optimization Instead of updating each element, track the count of 1s in nums1 using a segment tree with lazy propagation. For Type 2 queries: new_sum = old_sum + count_of_ones * p. This reduces complexity from O(n) to O(log n). Flip operation: new_count = (r - l + 1) - old_count in the range. Maintain lazy tags for efficient range updates. TutorialsPoint - Handling Sum Queries After Update | Count-Based Optimization Approach
Asked in
Google 8 Amazon 5 Microsoft 3
12.8K Views
Medium Frequency
~35 min Avg. Time
485 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen