Skip to main content
Back to the DSA atlas
Range queries Data structure Advanced

Persistent Segment Tree

Keep every version of a segment tree by path-copying only the changed nodes.

segment-treepersistencerare

Interactive visualization

Use the controls to connect the idea to concrete operations before diving into the full write-up.

idx 0

2

idx 1

5

idx 2

4

idx 3

7

Only O(log n) nodes changed from the previous version; the untouched nodes are shared structurally.

Versions are first-class

Persistence means updates do not destroy old history. Each new root points to a mostly shared tree, which is why old answers stay available cheaply.

Family

Range queries

Static and dynamic range aggregates via indexed trees, segment trees, and precomputed tables.

Builds on

1 topic

Read these first if you want the surrounding context.

Unlocks

0 next topics

Use these follow-ups when you want to apply the current idea in a richer setting.

Learning paths

4

This topic appears in curated progressions so you can keep moving without guessing the next step.

Problem

Sometimes you do not want only the latest array state. You want to ask queries against older versions too.

Core idea

When a segment-tree update changes one leaf, only the nodes on that root-to-leaf path need new copies. Every untouched subtree can be shared with earlier versions.

So each update returns a new root, while old roots remain valid.

Why it matters

Persistence turns “history” into something queryable.

That enables:

  • range queries on old versions
  • k-th order statistics over prefix versions
  • time-travel style offline solutions

Complexity

OperationTimeExtra space
One update creating a new versionO(logn)O(\log n)O(logn)O(\log n)
One query on any versionO(logn)O(\log n)-

Key takeaways

  • Persistent segment trees save history by path-copying, not by cloning the whole structure
  • A version is just a root pointer into mostly shared nodes
  • This is rare advanced range-query material, but it is one of the cleanest persistence examples in DSA
  • Heavy-light decomposition sometimes pairs with persistence when tree queries also need historical versions

Practice problems

ProblemDifficultyKey idea
K-th Number references🔴 HardPrefix versions plus order statistics
MKTHNUM🔴 HardClassic persistent segment tree problem
Persistent segment tree references🔴 HardPath copying and version roots

Relation to other topics

  • Segment Tree is the non-persistent base structure
  • Heavy-Light Decomposition can use segment trees on tree paths, and persistence can extend that story further
  • Mo’s Algorithm is another advanced answer when offline query structure matters more than online updates

Build on these first

These topics supply the mental model or underlying structure that this page assumes.

Related directions

These topics live nearby conceptually, even if they are not direct prerequisites.

More from Range queries

Stay in the same family when you want parallel variations of the same mental model.

Paths that include this topic

Follow one of these sequences if you want a guided next step instead of open-ended browsing.

Range-query toolkit

Start with static prefix tricks, then move into offline, persistent, dynamic, and precomputed range-query structures.

Tree path queries

Build the full ladder from ancestor queries to heavy-light decomposition and historical segment trees.

Offline toolkit

See how reordering queries, rolling back state, and preserving versions can replace online complexity.

From the blog

Pair the atlas with recent writing from the rest of the site when you want a broader engineering perspective alongside the topic graph.