Did you solve the previous problem? You surely realized that treaps don't seem more useful than a regular std::set. This is going to change now.
Since we have implemented the treaps ourselves, we can easily extend them to do more things. The situation is very similar to segment trees: they are not just useful to store individual elements, but they can also store additional information that allows us to do bulk queries or updates faster.
We will ask you to implement the easiest of those extensions for treaps: compute the order of an element, and find the element with a given order. The order of an element of key $$$k$$$ is its (0-based) index when you traverse the treap in preorder (that is, from smaller to largest key, or from left to right). If you are familiar with GNU C++ STL PBDS containers, you might know these operations as order_of_key and find_by_order.
How can we implement this in logarithmic time? Make every node have its size precomputed, where the size of a node is just the number of nodes of the subtreap rooted at it. To find the element of order $$$t$$$ in a treap, compare $$$t$$$ with the size of the root's left child: if the size is larger than $$$t$$$, the element must be in the left child; if the size is exactly $$$t$$$, the element is the root; if the size is less than $$$t$$$, the element is in the right child.
The only difficulty is to make sure that, whenever split and join operations modify the treaps, these precomputed sizes don't get outdated. To show us that you can handle that, we will ask you to write a program that implements the following operations efficiently.
Starting with an empty treap, execute a sequence of orders, as described. Integers $$$k$$$ will be from $$$0$$$ to $$$10^5$$$, heights $$$h$$$ will be from $$$0$$$ to $$$10^9$$$. All requested indexes and orders will be non-negative numbers.
At all times the nodes in the tree will not have repeated keys or heights. We will not ask you to remove nodes that do not exist.
For each command ORDER_OF_KEY or FIND_BY_ORDER, output a line with the corresponding answer.
ADD 1 23ADD 2 19ADD 4 95ADD 6 13ADD 8 25ADD 9 71ORDER_OF_KEY 1ORDER_OF_KEY 2ORDER_OF_KEY 4ORDER_OF_KEY 6ORDER_OF_KEY 8ORDER_OF_KEY 9ORDER_OF_KEY 10FIND_BY_ORDER 0FIND_BY_ORDER 1FIND_BY_ORDER 2FIND_BY_ORDER 3FIND_BY_ORDER 4FIND_BY_ORDER 5FIND_BY_ORDER 6
0 1 2 3 4 5 -1 1 2 4 6 8 9 -1
ADD 0 50ADD 1 74ADD 2 23DEL 1ADD 3 91ADD 4 43ADD 5 17DEL 3ADD 9 65ADD 8 35ADD 1 74ADD 7 92DEL 9ADD 6 36ADD 3 19ADD 9 66ORDER_OF_KEY 3ORDER_OF_KEY 5ORDER_OF_KEY 9ORDER_OF_KEY 0FIND_BY_ORDER 4FIND_BY_ORDER 7FIND_BY_ORDER 2FIND_BY_ORDER 1
3 5 9 0 4 7 2 1
| Name |
|---|


