Segment Tree Range Query Problem

viaGlassdoor

Problem: Support range-sum (or range-min/max) queries and point/range updates efficiently over an array.

Constraints: Array size and number of queries can both be up to 10^5, ruling out O(n) per query.

Example: Given array [1,3,5,7,9,11], query sum(1,3) = 3+5+7=15; update index 2 to 10, then sum(1,3) becomes 3+10+7=20.

Approach: Build a segment tree where each node stores the aggregate (sum/min/max) of a range; queries and point updates both take O(log n) by recursively descending only into the relevant child ranges. For range updates, add lazy propagation to defer pushing updates to children until needed, keeping updates at O(log n) as well.

Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account