A scoring ledger maintains a multiset of scores, each an integer from 1 to 50. Starting empty, it processes q operations, each one of:
INSERT x — add one occurrence of score x.ERASE x — remove one occurrence of score x. It is guaranteed at least one occurrence exists.RANK x — report how many scores currently in the multiset are strictly less than x.KTH k — report the k-th smallest score currently in the multiset (1-indexed, counting duplicates). It is guaranteed 1 <= k <= the current size.Line 1: an integer q.
Next q lines: INSERT x, ERASE x, RANK x, or KTH k.
For every RANK and every KTH operation, in order, print the requested integer on its own line.
Example 1
Input
6 INSERT 5 INSERT 2 INSERT 8 RANK 5 KTH 1 KTH 3
Expected
1 2 8
Explanation
The multiset is {2, 5, 8}. RANK 5 counts scores strictly below 5 (only 2), giving 1. KTH 1 is the smallest (2); KTH 3 is the largest (8).
Example 2
Input
6 INSERT 4 INSERT 4 INSERT 1 RANK 4 KTH 2 ERASE 4
Expected
1 4
Explanation
The multiset is {1, 4, 4}. RANK 4 counts scores below 4 (only 1), giving 1. KTH 2 is the second smallest, which is 4.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →