Competitive Ranks (medium version)
An online multiplayer tournament tracks player ratings in real time. Players may join with an initial rating, update their current rating after each match, or leave the tournament entirely. At any point, organizers need to quickly look up who holds a particular rank.
You must support the following operations on the player rankings:
- JOIN
name rating: Playernamejoins the tournament with the givenrating. The player does not already exist. - RATE
name new_rating: Playername's rating is updated tonew_rating. The player already exists. - LEAVE
name: Playernameleaves the tournament. The player exists and will not appear in further RATE or LEAVE operations for this name. - RANK
r: Return the name of the player at rankr, where rank 1 is the player with the highest rating. If two players have the same rating, break ties alphabetically by player name (lexicographically smallest name comes first in rank order).
Input
The first line contains q (1 ≤ q ≤ 2·10^5), the number of operations.
Each of the next q lines is one of the operations above, in the format:
JOIN name ratingRATE name new_ratingLEAVE nameRANK r
where:
nameis a non-empty string of up to 10 lowercase lettersratingandnew_ratingare integers in the range [1, 10^9]ris a positive integer
Output
For each RANK query, print the name of the player at that rank on a separate line.
Notes
- The problem is designed to be solved using a balanced binary search tree (ordered set) data structure, such as
std::setin C++. - All RANK queries are guaranteed to have valid ranks (1 ≤ r ≤ number of active players).