Rokua OJ P1196 Galactic Heroes legend (with right and check set)

Source: Internet
Author: User

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.

    1. 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 sampleInput 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

Simply take the right and check the set to draw a picture can be written, and POJ the building blocks and look similar

Code:

#include <iostream> #include <algorithm> #include <cstdlib> #include <sstream> #include < cstring> #include <bitset> #include <cstdio> #include <string> #include <deque> #include < stack> #include <cmath> #include <queue> #include <set> #include <map>using namespace std;# Define INF 0x3f3f3f3f#define CLR (x, y) memset (x,y,sizeof (×)) #define LC (x) (x<<1) #define RC (x) ((x<<1) +1) # Define MID (x, y) ((x+y) >>1) typedef pair<int,int> PII;TYPEDEF Long Long ll;const double Pi=acos ( -1.0); const int n=30010;struct info{int up;int total;int pre;}; Info node[n];void init () {for (int i=0; i<n; ++i) {node[i].up=0;node[i].total=1;node[i].pre=i;}} int find (int n) {if (node[n].pre==n) return n;int tpre;tpre=node[n].pre;node[n].pre=find (node[n].pre); node[n].up+= Node[tpre].up;return Node[n].pre;} void M (int a,int b) {int fa=find (a), fb=find (b); NODE[FA].PRE=FB;NODE[FA].UP+=NODE[FB].TOTAL;NODE[FB].TOTAL+=NODE[FA] . Total;} int C (int a,int b) {int FA=find (a), fb=find (b); if (FA!=FB) Return-1;return ABS (NODE[B].UP-NODE[A].UP)-1;} int main (void) {int N,i,j,a,b,c;char ops[3];while (~scanf ("%d", &n)) {init (); while (n--) {scanf ("%s%d%d", Ops,&a , &b), if (ops[0]== ' m ') m (A, B), elseprintf ("%d\n", C (b));}} return 0;}

Rokua OJ P1196 Galactic Hero Legends (with rights and collections)

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.