2002 NOI national Competition
Address: http://www.luogu.org/problem/show?pid=1196
Title Description
In 5801 A.D., the inhabitants of the Earth migrated to the second planet of Taurus Alpha, where they published the Galactic Federation
The founding of the manifesto, reign title for the first year of the cosmic calendar, and began to expand deep into the Milky Way.
In the 799-year universe, the two largest military groups in the galaxy erupted in the Battle of the Basque star. Taishan pressure
Top Group sent Cosmic fleet commander Reinhart led more than 100,000 warships to battle, Majestic group name will Yang
Willy organized 30,000 warships to meet the enemy.
Yang Weili good at formations, skillfully use various tactics repeatedly beat, inevitably pleases birth overbearing. In
In this decisive battle, he divides the field of the Fermi star battlefield into 30000 columns, numbered 1, 2, ..., in each row.
30000. After that, he numbered his battleship 1, 2, ..., 30000, so that battleship I was in
Column I (i = 1, 2, ..., 30000), forming "Higo", lure. This is the initial pattern. When
When the enemy arrives, Yang Weili will release the merger instructions several times, concentrating most of the warships on a few columns,
Implement intensive attacks. The merge instruction is M i j, meaning that the entire battleship fleet where the battleship I is located is
For a whole (head at the front end in the rear) to the tail of the battleship queue where USS J is located. Apparently the battleship
A queue is made up of one or more warships in the same column. The execution result of the merge instruction will increase the queue
Big.
However, the wily Reinhart has already made a strategic initiative. During the war, he was able to pass
The vast intelligence network is ready to monitor Yang Weili's fleet transfer instructions.
While Yang Weili issued instructions to mobilize the fleet, Reinhart in order to keep abreast of the current Yang Weili war
The distribution of the ship, will also issue some inquiry command: C i J. The instruction means, ask the computer, Yang Weili
Battleship No. I and Battleship J are currently in the same column, if they are in the same column, then their
How many warships are arranged between them.
As a senior senior programmer, you are asked to write a program to analyze Yang Weili's instructions to
And answer the question of Eli Hart.
The final battle has begun, and the history of the Milky Way has turned over a page ...
Input/output format
Input format:
The first line of the input file galaxy.in has an integer t (1<=t<=500,000), which means that there is a total of t
Instructions.
There are t-lines below, with one instruction per line. Directives are available in two formats:
1. M I j:i and J are two integers (1<=i, j<=30000), indicating the battleship number involved in the instruction.
The directive is a fleet transfer instruction issued by Reinhart eavesdropping on the Yang Weili and ensures that war I
The ship is not in the same column as the USS No. J.
2. C I j:i and J are two integers (1<=i, j<=30000), indicating the battleship number involved in the instruction.
This directive is an inquiry instruction issued by Reinhart.
Output format:
The output file is galaxy.out. Your program should analyze each of the input commands in turn and
Processing:
If it is a fleet transfer order issued by Yang Weili, it means that the fleet alignment has changed and your program
Be aware of this, but do not export any information;
If it is a Reinhart issue, your program will output a line that contains only an integer,
Represents the number of warships arranged between Battleship I and Battleship J on the same column. If the Battle of number I
Ship and USS J are not currently on the same column, output-1.
Input/Output sample
Input Sample # #:
4M 2 3C 1 2M 2 4C 4 2
Sample # # of output:
-11
Description
"Sample description"
Battleship Location Map: Arabic numerals in tables indicate battleship numbers
Ideas
If only write a bare and check the rally timeout, the optimizer is to maintain her queue length and the last battleship.
typeArr=Array[0..30000] ofLongint;varA,f,last:arr;//A[i] Indicates the number of warships (including itself) in the queue before the first warship, F[i] indicates who is the oldest of the first warships, Last[i] represents the last battleship headed by the first battleship. I,n,b,c,p,q:longint; S:char;functionFind (K:longint): Longint;varT:longint;begin ifF[k]=k Thenexit (k); T:=find (F[k]); Inc (A[k],a[f[k])-1); F[K]:=T; Exit (t);End;proceduremm;varI,k,sum:longint;beginF[p]:=Last[q]; Inc (A[p]); LAST[Q]:=Last[p];End;procedurecc;varAns:longint;begin ifP<>q Then beginWriteln (-1); Exit End ElseWriteln (ABS (A[B]-A[C))-1);End;begin fori:=0 to 30000 Do beginF[i]:=i; A[i]:=1; Last[i]:=i; End; READLN (n); fori:=1 toN Do beginreadln (S,B,C); P:=find (b); Q:=find (c); CaseS of 'M': mm; 'C': CC; End; End;End.
Legend of the Galactic heroes