Sub topic 1
The Problem: Range Queries on a Changing Array
Prerequisites
This chapter assumes you know:
- Zero-based and one-based array indexing.
- Big-O notation, especially O(log n).
- Basic recursion and binary trees. Later chapters use them.
- Binary numbers and bitwise operators. The Fenwick chapters use them.
The task
You have an array of n numbers. You must handle a mix of two kinds of operations:
- Range query: return the sum of the elements from index
lto indexr, inclusive. - Point update: set the element at index
ito a new value.
Examples are a leaderboard whose scores change while players ask for the total over a rank range, or a sensor log that receives corrections while analysts request window totals.
With q operations on n elements, you want each operation to take about O(log n) time instead of O(n).
Approach 1: loop over the range
The direct method sums the slice on every query.
a = [5, 2, 7, 1, 3]
def range_sum(arr, l, r):
return sum(arr[l:r + 1])
def point_update(arr, i, value):
arr[i] = value
print(range_sum(a, 1, 3)) # 2 + 7 + 1 = 10
point_update(a, 2, 10)
print(range_sum(a, 1, 3)) # 2 + 10 + 1 = 13An update costs O(1). A query costs O(n) in the worst case, because the range can cover the whole array.
Approach 2: prefix sums
A prefix sum array stores pre[k], the sum of the first k elements. A range sum is then one subtraction.
from itertools import accumulate
a = [5, 2, 7, 1, 3]
pre = [0] + list(accumulate(a)) # pre[k] = a[0] + ... + a[k-1]
def range_sum(l, r):
return pre[r + 1] - pre[l]
print(range_sum(1, 3)) # 10
a[2] = 10
pre = [0] + list(accumulate(a)) # rebuild: O(n)
print(range_sum(1, 3)) # 13Queries now cost O(1). A single update changes pre[3] and every entry after it, so an update costs O(n).
The trade-off
| Approach | Query | Update |
|---|---|---|
| Loop over the range | O(n) | O(1) |
| Prefix sums | O(1) | O(n) |
| Segment tree or Fenwick tree | O(log n) | O(log n) |
Each simple approach makes one operation cheap and the other expensive. When queries and updates are interleaved, one of them dominates the running time. With n = q = 100,000, either simple approach does up to about 10^10 elementary steps. A structure with O(log n) for both does about 100,000 × 17 = 1.7 × 10^6.
What comes next
The rest of the tutorial builds the third row of the table. You will first see which operations such structures can support. Then you will build a segment tree, which stores answers for segments of the array, and a Fenwick tree, which stores partial prefix sums at cleverly chosen positions. Both give O(log n) queries and updates.
Exercise
An array has n = 200,000 elements and receives 200,000 operations, half point updates and half range sums. Using prefix sums rebuilt after each update, roughly how many elementary steps do the updates alone take?
Show answer
Each update rebuilds an array of about n entries, so 100,000 updates × 200,000 entries is about 2 × 10^10 steps. This is far too slow. An O(log n) structure needs about 200,000 × 18 ≈ 3.6 × 10^6 steps in total.