Tourney

Time limit2sMemory limit512 MB

Summary
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 11 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 2N2^N (1≤N≤201 \le N \le 20) competitor positions in the tourney tree, numbered 1,2,…,2N1, 2, \ldots, 2^N from left to right. In the first round, competitors 11 and 22 face off, as do competitors 33 and 44, 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 NN rounds a single winner remains. For example, when N=2N = 2 the tourney tree looks like this:

Here AA is the winner of the match between competitors 11 and 22, BB is the winner of the match between competitors 33 and 44, and CC is the winner of the match between AA and BB. CC 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 MM (1≤M≤1,000,0001 \le M \le 1{,}000{,}000) 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 NN (1≤N≤201 \le N \le 20) and MM (1≤M≤1,000,0001 \le M \le 1{,}000{,}000), separated by a single space.

The next 2N2^N lines each contain one integer SiS_i (for ii from 11 to 2N2^N): the skill level of the initial competitor at position ii in the tourney tree.

Each of the following MM lines is a command in one of three formats:

  • R i S — the competitor at position ii is removed and replaced by a new competitor with skill level SS. A new tourney is then held.
  • W — determine who won the current tourney and print the position ii (between 11 and 2N2^N) of that competitor.
  • S i — print the number of rounds that the competitor at position ii won in the current tourney.

Output

For each W or S i command, print the corresponding integer on its own line.

Examples4

  1. Example 1

    Input
    2 8
    30
    20
    10
    40
    S 1
    W
    R 4 9
    S 4
    W
    R 2 35
    S 2
    W
    
    Expected output
    1
    4
    0
    1
    2
    2
    
  2. Example 2

    Input
    1 3
    5
    8
    W
    S 1
    S 2
    
    Expected output
    2
    0
    1
    
  3. Example 3

    Input
    1 4
    10
    20
    W
    R 1 100
    W
    S 1
    
    Expected output
    2
    1
    1
    
  4. Example 4

    Input
    3 9
    3
    7
    1
    9
    5
    2
    8
    6
    W
    S 1
    S 2
    S 3
    S 4
    S 5
    S 6
    S 7
    S 8
    
    Expected output
    4
    0
    1
    0
    3
    1
    0
    2
    0