National Olympiad in Informatics, China, 2002
In the year 2801 CE, the Earth's inhabitants have migrated to Theoria, the second planet of the Aldebaran Starzone in the Taurus constellation. There and then, the Galactic Federation was officially founded. That same year, the Common Era (CE) calendar was replaced by the Universal Calendar (UC) to commemorate this establishment. Subsequently, further expansion into the depths of the galaxy commenced.
In the year 799 UC, battle erupted between the two largest military forces of the galaxy in the Vermilion Starzone. The Galactic Empire ordered commander Reinhard von Lohengramm to take a fleet of roughly one hundred thousand warships to demolish the thirty thousand opposing ships under the command of admiral Yang Wen-li.
Yang Wen-li specializes in directing soldiers and battle formations. In
the past, he has won countless battles using brilliant tactics,
especially when grossly outnumbered. Inevitably, his pride grows with
every battle. During this ultimate battle, he has divided the
battlefield of Vermilion into columns, each column labeled with a
number from to . He has also labeled his warships with a number
from to , allowing ship to occupy column ,
creating a "one-rowed snake formation" to lure the enemy deep
within the trap. This is only the initial formation. When the invading
enemies arrive, Yang Wen-li will repeatedly issue commands to merge his
ships to focus attacks on particular rows. A merge command is of the
format M i j
, indicating that the entire fleet in the column
containing ship should move, as a whole, to the back of the entire
fleet containing ship . Obviously, the entire fleet can consist of
one or more warships from the same column. The merge command will result
in an increase in the size of the destination column.
However, the cunning Reinhard had long been a step ahead. At any time during the battle, he will be able to use a vast intelligence network to listen in on Yang Wen-li's commands for directing his fleet.
While Yang Wen-li is issuing a command to transfer his fleets, some
queries will be issued in the format C i j
. This command asks the
computer: do Yang Wen-li's ship and ship currently belong in the
same column? If so, how many ships are positioned between them?
As an experienced senior programmer, you have been requested to write a program that analyzes Yang Wen-li's orders, as well as answers Reinhard's queries.
The final battle has begun, thus starting a new chapter in the galaxy's history.
Input Specification
The first line of input contains a single integer , representing the total number of commands. The following lines each contain a command of the following two formats:
M i j
: where and are two integers , representing the numbers of the ships involved. These are commands issued by Yang Wen-li and secretly obtained by commander Reinhard. It is guaranteed that ship and ship are not already in the same column.C i j
: where and are two integers , representing the numbers of the ships involved. These are queries made by Reinhard about the status of Yang Wen-li's fleet.
Output Specification
You are to analyze and process the commands in the input. For each of
Yang Wen-li's commands that indicate a merge of fleets, your program
should take note of the change but not output any information. For each
of Reinhard's queries, your program must output one line containing a
single integer. If ships and are in the same column, output the
number of ships that exist in-between them. If ships and do not
belong in the same column, output -1
.
Sample Input
4
M 2 3
C 1 2
M 2 4
C 4 2
Sample Output
-1
1
Explanation
Here is an outline of the ship locations throughout the battle:
Column 1 | Column 2 | Column 3 | Column 4 | ... | |
---|---|---|---|---|---|
Initial | ... | ||||
M 2 3 |
|
... | |||
C 1 2 |
Ship number and are not in the same column, therefore output . | ||||
M 2 4 |
|
... | |||
C 4 2 |
There is a single ship (ship ) between ships and , therefore output . |
Problem translated to English by .
Comments