[Two Auxiliary Arrays for saving the world] Legends of Galaxy heroes

Source: Internet
Author: User

[Question]


One year in, the earth's residents migrated to the second planet of Alpha Taurus, where they issued a declaration of the creation of the galaxy Federation. In the same year, they changed the yuan to the age of the universe and began to expand to the depths of the Milky Way.
In the 799 s of the cosmic calendar, two military groups in the Milky Way broke out in the bamianne domain. Lainhart, the commander of the Taishan Summit group's cosmic fleet, led more than 100,000 warships to fly out, and Yang Weiwei, a star of the mountain river group, organized 30 thousand warships to welcome the enemy.
Yang Weiwei is good at arranging troops and skillfully uses various tactics to win more than once and for all, which inevitably leads to arrogance. In this decisive battle, he divided the battlefield of the bamianne domain into 30000 columns, with each column numbered 1, 2 ,..., 30000. Then, he numbers his warships as 1, 2 ,..., 30000. Place warship I in column I (I = 1, 2 ,..., 30000. This is the initial formation. When the enemy of the attacker arrives, Yang Willi will issue merge commands multiple times to concentrate most warships on certain columns for intensive attacks. The merging command is m I j, which means to make the entire warship queue where warship I is located as a whole (the first and last are behind) to the tail of the warship queue where warship J is located. Apparently, a warship queue is composed of one or more warships in the same column. The execution result of the merge command will increase the queue.
However, lainhart, who has been well-known, has already taken the initiative in strategy. During the battle, he can monitor Yang's fleet mobilization commands at any time through a large intelligence network.
While yang Willi Issued commands to mobilize the fleet, lainhart also issued some inquiry commands to learn about the distribution of the warships of Yang Willi in a timely manner: C I j. This command is used to ask the computer whether the warship I of Yang Wylie is in the same column as the warship J. If it is in the same column, how many warships are arranged between them.
As a seniorProgramDesigner, you are asked to write a program to analyze Yang Huili's instructions and to answer Lenhart's inquiry.
The final decisive battle has been launched, and the history of the galaxy has gone through another page ......

[Input]

The first line of the input file galaxy. In contains an integer T (1 <=t <= 500,000), indicating that there are a total of T commands.

There are t lines below, and each line has a command. There are two formats of commands:
1. m I j: I and j are two integers (1 <= I, j <= 30000), indicating the number of warships involved in the command. This command is a ship transfer command issued by Yang Huili, which was intercepted by lainhart, and ensures that warship I and warship J are not in the same column.
2. c I j: I and j are two integers (1 <= I, j <= 30000), indicating the warship number involved in the command. This command is a query command issued by lainhart.
[Output file]
The output file is galaxy. Out. Your program should analyze and process each input command in sequence:
If it is the fleet transfer command issued by Yang Huili, it indicates that the fleet arrangement has changed. Your program should pay attention to this point, but do not output any information;
If the query command is issued by lainhart, your program will output a line containing only one integer, indicating the number of warships arranged between warship I and warship J on the same column. If warship I and warship J are not in the same column, output-1.
[Example input]
4
M 2 3
C 1 2
M 2 4
C 4 2
[Sample output]
-1
1

[Analysis]

Apparently, the query set is added to the root node length record.

Count [I] indicates the number of vertices in the I set. Before [I] indicates several vertices before I.

Well, the Program understands.

#include 
  
    # include 
   
     # define maxn 30010int root [maxn], before [maxn], Count [maxn]; int n, x, y; char C; int Froot (int x) {If (! Root [x]) return X; int TE = Froot (root [x]); before [x] = before [root [x] + before [x]; root [x] = tE; return Te;} void merge (int x, int y) {int RX = Froot (x), Ry = Froot (y ); if (RX = ry) return; root [RX] = ry; before [RX] + = count [ry]; count [ry] + = count [RX];} int main () {// file freopen ("Hero. in "," r ", stdin); freopen (" Hero. out "," W ", stdout); // init for (INT I = 1; I <= 30000; ++ I) Count [I] = 1; // scan solve print scanf ("% d", & N); scanf ("% C", & C); For (INT I = 1; I <= N; ++ I) {scanf ("% C % d", & C, & X, & Y); If (C = 'M') Merge (X, y); else if (Froot (x) = Froot (y) printf ("% d \ n", ABS (before [x]-before [y]) -1); else printf ("-1 \ n"); scanf ("% C", & C) ;}// exit return 0 ;}
   
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.