Persistent Kth Smallest

Static range kk-th smallest, nn and qq up to 2⋅1052 \cdot 10^5, built from a persistent segment tree over the value domain. What does it cost and why?

A

O(n2)O(n^2) nodes and O(log⁡n)O(\log n) per query. Each prefix version copies the whole tree of O(n)O(n) nodes, and the query descends the difference of versions rr and l−1l-1.

B

O(nlog⁡n)O(n \log n) nodes and O(log⁡n)O(\log n) per query. Each prefix version copies only the O(log⁡n)O(\log n) nodes on one root-to-leaf path, and the query descends the difference of versions rr and l−1l-1.

C

O(n)O(n) nodes and O(log⁡n)O(\log n) per query. Each prefix version copies only the one changed leaf and shares every ancestor, and the query descends the difference of versions rr and l−1l-1.

D

O(nlog⁡n)O(n \log n) nodes and Θ(log⁡2n)\Theta(\log^2 n) per query. Each prefix version copies only the O(log⁡n)O(\log n) nodes on one root-to-leaf path, and the query binary searches the value domain, descending once per step.