Kth smallest dynamic set

Write codeHard35 min

Your task

You maintain a dynamic set of integers in a BST and must answer order-statistic queries efficiently. Given a list of operations, process them in order: ['insert', x] adds x to the set (duplicates are ignored), and ['kth', k] asks for the k-th smallest value currently in the set (1-indexed; if k exceeds the set size, the answer is -1). Return the list of answers, one per 'kth' query, in order. Your solution must support each operation in average time better than O(size) by augmenting the BST with subtree sizes.

Examples

dynamic_kth_smallest(ops: list[list]) -> list[int]
Example 1
Inputdynamic_kth_smallest([["insert", 5], ["insert", 3], ["insert", 8], ["kth", 1], ["kth", 2], ["kth", 3]])
Output[3, 5, 8]