Kth Smallest Under Load

A leaderboard supports three operations, interleaved online, 2⋅1052 \cdot 10^5 of them in total, with scores that are integers in [1,109][1, 10^9]:

  • insert(v)
  • erase(v)
  • kth(k) returning the kk-th smallest score currently present

Which structure serves all three, and what exactly is missing from the near misses?

A

A sorted std::vector with lower_bound to find the position, so kth(k) is an O(1)O(1) index and inserts stay logarithmic.

B

A Fenwick tree of counts indexed by compressed score, giving insert and erase in O(log⁡n)O(\log n) and kth by descending the tree in O(log⁡n)O(\log n). Total about 2⋅105⋅18≈3.6⋅1062 \cdot 10^5 \cdot 18 \approx 3.6 \cdot 10^6 steps.

C

A min-heap. insert and erase are O(log⁡n)O(\log n) and kth(k) is the kk-th pop, so all three are logarithmic.

D

A plain balanced binary search tree keyed by score, with no augmentation. It keeps everything in sorted order and therefore answers all three in O(log⁡n)O(\log n).