Path copying: each update clones only the O(log n) nodes on the root-to-leaf path (green) and shares every other subtree by pointer (blue). Old versions stay queryable forever. Second mode walks two versions to find the k-th smallest in a range.
| Operation | Time | New nodes | Note |
|---|---|---|---|
| Build v0 | O(n) | ~2n | full tree once |
| Update → new version | O(log n) | ⌈log₂n⌉+1 | only the path is copied |
| Range query (any version) | O(log n) | 0 | same as ordinary segtree |
| Pick / switch version | O(1) | 0 | choose a root |
| K-th smallest in range | O(log n) | 0 | walk version[r] & version[l−1] |
| Total after m updates | — | O(n + m log n) | vs naive O(n·m) |