Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #2569: Handling Sum Queries After Update

In this guide, we solve Leetcode #2569 Handling Sum Queries After Update in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

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].

Quick Facts

  • Difficulty: Hard
  • Premium: No
  • Tags: Segment Tree, Array

Intuition

The constraints allow a direct scan that keeps only the essential state.

By translating the requirements into a clean loop, the logic stays easy to reason about.

Approach

Iterate through the data once, updating the state needed to compute the answer.

Return the final state after the traversal is complete.

Steps:

  • Parse the input.
  • Iterate and update state.
  • Return the computed answer.

Example

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.

Python Solution

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

Complexity

The time complexity is O(n+m×log⁡n)O(n + m \times \log n)O(n+m×logn), and the space complexity is O(n)O(n)O(n). The space complexity is O(n)O(n)O(n).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy