Persistent Segment Trees: New Paths Keep Old Versions
2026-11-19

Persistent Segment Trees: New Paths Keep Old Versions

Persistent Segment Trees retain access to earlier states by copying the nodes changed by an update and sharing the nodes that remain unchanged. A root identifies a version. The essential rule is that constructing a later version must not overwrite a node that an earlier root still reaches. Persistence describes retained structure, rather than a requirement to copy an entire array for every change.

Copy the changed root-to-leaf path

Consider a segment tree whose leaves represent array positions and whose internal nodes store sums. Assigning a new value to one position changes that leaf and the aggregate at each ancestor. It does not change the values represented by the sibling subtrees along the path.

A persistent update creates a replacement leaf and new ancestors, linking each new ancestor to its changed child and the old unchanged sibling. The old root still reaches the original path. A query chooses its version by choosing a root and then applies the normal range aggregation rules within that version.

Old total10 right7 leaf4 vsnew15 right12 leaf9 with unchangedleftsum3shared
Illustrative changed path, not a drawing of every node in the tree. Zhilianhai editorial desk, original AI-assisted concept diagram.

Shared nodes must remain immutable

Take the independent four-element array [2,1,4,3], whose sum is ten. Change position two, using zero-based indexing, from four to nine. The old version must still report ten and the old single-position value four. The new version reports fifteen and value nine. Its subtree covering positions zero and one can be shared unchanged with the old version.

For this full four-leaf binary tree, the changed leaf, its parent and the root are the three new nodes on the update path. The two versions are not two fully independent copies of all nodes. Modifying the shared left subtree in place later would corrupt both versions and break the persistence guarantee.

A different tree can store value frequencies

For range order statistics, construct a tree over an ordered value domain instead of over original array positions. A leaf counts occurrences of its value. Begin with root R0 representing zero items. Root Ri represents the first i elements of the original sequence, so each next root increments the frequency of one value through a copied path.

Under this definition, the frequency histogram of the zero-based positional range [l,r) is the histogram represented by Rr minus the histogram represented by Rl. Both endpoints refer to prefix lengths. Some explanations mix inclusive positions with prefix counts; defining the root convention first prevents that off-by-one ambiguity.

R5hist2,1,1,1 minusR1hist0,0,0,1 equals2,1,1,0
Columns ordered values1through4; requested range[1,5)thirdsmallest2. Zhilianhai editorial desk, original AI-assisted concept diagram.

Subtract counts while descending two versions

Choose the sequence [4,1,3,1,2] and query positions [1,5). The selected values are [1,3,1,2]. Use a value domain containing one through four and compare roots R5 and R1. Over the left value half containing one and two, R5 counts three occurrences while R1 counts zero, so their difference is three.

To select the third smallest using one-based k equal to three, follow that left half. Within it, value one has difference count two. Since k exceeds two, follow the value-two leaf and replace k by one. The answer is two. Independent sorting gives [1,1,2,3], which confirms the result and shows why duplicate occurrences must be counted.

Version subtraction needs an additive interpretation

The two-root histogram technique works because subtracting prefix counts produces nonnegative range counts. A general persistent tree can support other aggregates, but that does not mean arbitrary aggregates can be subtracted to obtain a range version. Minimum, for example, does not recover a suffix minimum by subtracting two prefix minima.

Likewise, arbitrary versions created by changing unrelated older roots may not represent nested prefixes. Their count differences can be negative and need not describe a valid multiset. Path copying and two-prefix selection are related constructions, but the latter adds a specific invariant about how the roots were built.

A version root is a reference to a logical state, not a timestamp inferred from node allocation order. A program may retain several branches created from different older versions. Naming those roots explicitly keeps their ancestry understandable. A normal point query can inspect any retained branch even when the special prefix-subtraction invariant does not apply to the pair of branches being compared.

Memory follows new paths and retained roots

A balanced tree over M leaves needs a logarithmic number of newly allocated nodes for a point update, plus its retained root reference. The source implementations expose different allocation strategies, including ordinary allocation and a preallocated node pool. A fixed pool needs sufficient capacity, and reclaiming old nodes requires knowing that no retained root still uses them.

Coordinate compression can map ordered values to leaves without changing their order. The site's persistent data principles and algorithm features connect structure and query contracts. Primary sources: the persistent history and range-selection sections of CP Algorithms and Nyaan's original immutable-path update and version interfaces. Both figures are original AI-assisted structural diagrams.

18028484626 CONTACT US

公司:智联海(惠州)科技有限公司

地址:惠州仲恺高新区陈江街道白云山工业大道 5 号六楼 601

Q Q:18028484626

Copyright © 2002-2026

粤xxx

咨询 在线留言 电话:18028484626
微信 微信扫码添加我