Hints — p91 Range Sum Query Mutable
-
O(n) per query is too slow for many queries; O(n) per update is too slow if frequent. Need O(log n) for both.
-
Fenwick Tree (BIT): 1-indexed array where
tree[i]stores the sum of a range ending atiof lengthlowbit(i) = i & -i. Update propagatesi += lowbit(i); prefix sum walksi -= lowbit(i). -
Iterative Segment Tree: 2n-node array. Leaves at
[n, 2n). Internal nodeiaggregatestree[2i]andtree[2i+1]. Range query: walk both endpoints upward, accumulate off-spine nodes. -
Range sum
[l, r]via BIT =prefix(r+1) - prefix(l)using 1-indexed inclusive prefix. -
Choice: BIT for sum/XOR (has inverse); segment tree for max/min/gcd (no inverse) or for lazy propagation (range updates).
If stuck: see solution.py.