Sub topic 14
Range update and range sum with two Fenwick trees
This chapter assumes you know the Fenwick add and prefix operations, prefix sums, and difference arrays. A single Fenwick tree handles point updates with range sums. It cannot add a value to a whole range and still answer range sums. Two trees can.
Start from the difference array
Let d be the difference array of a, so d[j] = a[j] - a[j-1] and a[i] = d[1] + ... + d[i]. Adding v to a[l..r] changes only two entries of d: d[l] += v and d[r+1] -= v. That is two point updates.
Now write the prefix sum of a in terms of d:
prefix(i) = sum over k<=i of a[k]
= sum over k<=i of sum over j<=k of d[j]
= sum over j<=i of d[j] * (i - j + 1)
= i * sum(d[j]) - sum(d[j] * (j - 1))Both sums run over j from 1 to i. Each one is an ordinary Fenwick prefix query:
B1storesd[j].B2storesd[j] * (j - 1).prefix(i) = B1.prefix(i) * i - B2.prefix(i).
Implementation
class Fenwick:
# 1-indexed Fenwick tree for prefix sums.
def __init__(self, n):
self.n = n
self.t = [0] * (n + 1)
def add(self, i, delta):
while i <= self.n:
self.t[i] += delta
i += i & -i
def prefix(self, i):
s = 0
while i > 0:
s += self.t[i]
i -= i & -i
return s
class RangeFenwick:
# Range add and range sum using two Fenwick trees (1-indexed, starts as all zeros).
def __init__(self, n):
self.n = n
self.b1 = Fenwick(n)
self.b2 = Fenwick(n)
def range_add(self, l, r, v):
self.b1.add(l, v)
self.b1.add(r + 1, -v)
self.b2.add(l, v * (l - 1))
self.b2.add(r + 1, -v * r)
def prefix(self, i):
return self.b1.prefix(i) * i - self.b2.prefix(i)
def range_sum(self, l, r):
return self.prefix(r) - self.prefix(l - 1)
rf = RangeFenwick(6)
rf.range_add(2, 5, 3)
print(rf.range_sum(1, 6)) # 12
print(rf.range_sum(3, 4)) # 6The second b2 update at r + 1 uses -v * r because the difference entry there is -v and its weight is (r + 1) - 1 = r.
Tracing one query
After range_add(2, 5, 3), the trees hold these point values:
| Position | B1 (`d[j]`) | B2 (`d[j] * (j-1)`) |
|---|---|---|
| 2 | 3 | 3 |
| 6 | -3 | -15 |
For prefix(4), B1.prefix(4) = 3 and B2.prefix(4) = 3. The result is 3 * 4 - 3 = 9. This matches the real array [0, 3, 3, 3, 3, 0], where the first four entries sum to 9.
Practical notes
Fenwick.addignores indexes aboven, sor + 1 == n + 1needs no special case.- A single element is
range_sum(i, i). It is alsoB1.prefix(i)when the array started as zeros. - To start from a nonzero array, keep an ordinary prefix-sum array of the original values and add it to the result. Alternatively, apply one
range_add(i, i, a[i])per element. - Each
range_addcosts fourO(log n)updates. Eachrange_sumcosts fourO(log n)queries. - Intermediate values such as
B1.prefix(i) * ican be large. Python integers do not overflow, but in C++ or Java use a 64-bit type.
Exercise
After rf = RangeFenwick(6) and rf.range_add(2, 5, 3), what are B1.prefix(6), B2.prefix(6), and rf.prefix(6)?
Show answer
B1.prefix(6) = 3 - 3 = 0. B2.prefix(6) = 3 - 15 = -12. So rf.prefix(6) = 0 * 6 - (-12) = 12, which equals the sum of [0, 3, 3, 3, 3, 0].