A persistent segment tree keeps earlier roots usable after an update. For a point replacement, it creates new nodes along one root-to-leaf path and shares every untouched subtree with the prior version. The initial build takes O(n) time and space; each update takes O(log n) time and new nodes, and a range sum takes O(log n) time in this balanced tree. Version roots must be retained intentionally. This is persistence of data-structure versions, not crash durability or database transactions; storing Python objects in memory does not make them survive a process restart.
Persistent segment trees: retain old range-sum versions
Operational case
A warehouse freezes counts 47, 31, 26, and 58. Version zero reports 115 over [1, 4). A late correction replaces index two with 29; version one reports 118 for the same range, while version zero still reports 115. The old right and left subtrees not on the changed path can be shared. A report can therefore state which revision it used without copying the entire input on every correction. The application still needs ownership, storage, and publication rules for roots if audit history must survive restarts.
Working Python program
def build(counts, left, right):
if right - left == 1:
return (counts[left], None, None)
middle = (left + right) // 2
first = build(counts, left, middle)
second = build(counts, middle, right)
return (first[0] + second[0], first, second)
def replace(node, left, right, changed_index, new_count):
if right - left == 1:
return (new_count, None, None)
middle = (left + right) // 2
first, second = node[1], node[2]
if changed_index < middle:
first = replace(first, left, middle, changed_index, new_count)
else:
second = replace(second, middle, right, changed_index, new_count)
return (first[0] + second[0], first, second)
def range_sum(node, left, right, query_left, query_right):
if query_right <= left or right <= query_left:
return 0
if query_left <= left and right <= query_right:
return node[0]
middle = (left + right) // 2
return range_sum(node[1], left, middle, query_left, query_right) + range_sum(node[2], middle, right, query_left, query_right)
counts = [47, 31, 26, 58]
version_zero = build(counts, 0, len(counts))
version_one = replace(version_zero, 0, len(counts), 2, 29)
print(range_sum(version_zero, 0, 4, 1, 4))
print(range_sum(version_one, 0, 4, 1, 4))
print(range_sum(version_zero, 0, 4, 1, 4))Output
115
118
115Time, space, and tradeoff
The example creates O(n) nodes in the first version and O(log n) additional nodes for each replacement. Queries use O(log n) time and O(log n) recursion stack in this implementation. With u retained updates, total node storage is O(n + u log n). Reject empty input, invalid point indices, and range bounds outside 0 <= left <= right <= n in a public wrapper. If no caller retains an old root, ordinary garbage collection may reclaim its unshared nodes. Retaining every root forever is a separate memory policy.
Common Mistakes
- Do not mutate an old node while constructing a new version.
- Do not claim in-memory versions survive process failure.
- Do not retain unlimited roots without a memory policy.
Connected lessons
- Range Queries
- Data Structures
- Segment trees: combine child ranges after updates
- Fenwick trees: update points and query prefix totals
- Sparse tables: precompute immutable range minima
- Projects
- Quizzes
Apply it: Project: design a versioned warehouse index and Advanced structure contracts.
Index snapshots: publish related maps as one in-memory version extends this operational boundary.
Persistent range MEX: search last occurrences in prefix versions examines a related structure with a different operation boundary.
Persistent subarray ranks: subtract prefix frequency trees examines a related structure with a different operation boundary.
