Tourney
Time limit2sMemory limit512 MB
Maintain a single-elimination bracket of 2^N players under point updates, and answer queries about the winner's position and how many rounds a given player wins.
- Level
Medium7 of 10
- Topics
- Tree, Segment tree, Simulation, Implementation
- Solved
- No attempts yet
Problem
A commentator has been hired to provide 24-hour coverage of a series of single-elimination, bracket-style furniture-disassembly tourneys (tournaments). Each competitor has a furniture-disassembly skill level, an integer between and 1,000,000,000. In every head-to-head match, the competitor with the larger skill level wins and advances, while the other is eliminated. At any moment the skill levels of all competitors are guaranteed to be distinct, so ties never occur.
There are () competitor positions in the tourney tree, numbered from left to right. In the first round, competitors and face off, as do competitors and , and so on. In each later round, the winners of the first two matches of the previous round compete, then the winners of the next two, and so on. After rounds a single winner remains. For example, when the tourney tree looks like this:

Here is the winner of the match between competitors and , is the winner of the match between competitors and , and is the winner of the match between and . is the winner of this tourney.
Because of sponsorship contracts, some competitors are replaced over time. Whenever a new person comes in, a fresh tourney is held.
Given a sequence of () commands (see the input format below), write a program that reports certain tourney statistics at various points in time.
Input
The first line contains two integers () and (), separated by a single space.
The next lines each contain one integer (for from to ): the skill level of the initial competitor at position in the tourney tree.
Each of the following lines is a command in one of three formats:
R i S— the competitor at position is removed and replaced by a new competitor with skill level . A new tourney is then held.W— determine who won the current tourney and print the position (between and ) of that competitor.S i— print the number of rounds that the competitor at position won in the current tourney.
Output
For each W or S i command, print the corresponding integer on its own line.