7565 - Move & Count   

Description

Cake and Redline are playing a game with N (1 <= N <= 30,000) identical
cubes labeled 1 through N. They start with N stacks, each containing a single
cube. Cake asks Redline to perform P (1<= P <= 100,000) operations. There
are two types of operations: move and count.
* In a move operation, Cake asks Redline to move the stack containing
cube X on top of the stack containing cube Y.
* In a count operation, Cake asks Redline to count the number of cubes under
the cube X and report that value.

Write a program that can verify the results of the game.

Input

* Line 1: A single integer, P
* Lines 2..P+1: Each of these lines describes an operation(move/count). Each
line begins with a 'M' for a move operation or a 'C' for a count operation. For
move operations, the line also contains two integers: X and Y. For count
operations, the line also contains a single integer: X.

Note that if a move operation requests to move a stack onto itself. You don’t
need to do anything.

Output

For every count operation, output a single number in a line, indicating the
number of cubes that are under the cube X.

Sample Input  Download

Sample Output  Download

Tags




Discuss