Uestc_ Qiushi brother playing games UESTC Training for Data Structures<problem h>

Source: Internet
Author: User
Tags uppercase letter

H-Qiushi Big Brother playing Games time limit:3000/1000ms (java/others) Memory limit:65535/65535kb (java/others)SubmitStatus

"Perhaps life is a game, but you are determined to cultivate the spring and autumn." "--Qiushi Big Brother sighs.

Qiushi elder brother is a likes to play the game, compared to other kinds of games, Qiushi elder brother prefers the free open sandbox game, especially minecraft .

Now, Qiushi found N Independent islets (numbered 1,2,3 ...). N), so he's going to connect these islands together.

Every time, brother Qiushi will choose two different islets.x(xis the center of the collection) andy(yNot necessarily the center of the collection), if the Islexand Small IslandyNot in a collection, create a distance of| x−y| the side of mod ,

Merge the two small islands into a new set, centered on the center of the collection where y was originally located.

But Qiushi wanted to know in real time how far an island is from the center of the current collection. As Qiushi is busy with the feast, he wants to ask for your help.

Input

The first line has an integer N representing the number of islets.

Next there are several lines, each of which behaves in one of the following two operations:

:  indicates that Qiushi wants to    Create an edge between x and Y. : asks  for the distance from X to the center of the current collection. 

The input ends with an uppercase letter O .

1≤N≤100000, operand ≤200000.

Output

For each query, output an integer, which is the distance from x to the center of the current collection, in one row.

Sample Input and output
Sample Input Sample Output
3I 1 2E 1I 3 1E 3O
13

Problem Solving Report

It is easy to think of and check the set, using dis[] array to maintain the distance from each point to the root, the only thing to note is to modify the distance, then update their father, otherwise it will be wrong.

1#include <iostream>2#include <algorithm>3#include <cstring>4#include <cstdio>5typedefLong Longll;6 using namespacestd;7 Constll MAXN = 6e5 + -;8 /*9 Dis is the distance between node and his rootTen */ One ll PRE[MAXN]; A ll DIS[MAXN]; -  - ll Getfather (ll cur) the { -    if(cur = =Pre[cur]) -     returncur; -ll fat =Getfather (Pre[cur]); +Dis[cur] + =Dis[pre[cur]]; -Pre[cur] =fat; +    returnfat; A } at  - Chartemp[150000]; -  - intMainintargcChar*argv[]) - { - ll N; inscanf"%lld",&n); -memset (DIS,0,sizeof(DIS)); to    for(inti =1; I <= N; ++i) +Pre[i] =(ll) I; -    while(1) the    { *scanf"%s", temp); $          if(temp[0] =='O')Panax Notoginseng            Break; -          if(temp[0] =='I') the           { + ll x, y; Ascanf"%lld%lld",&x,&y); the                 if(Getfather (y)! =x) +                  { -DIS[X] = ABS (x-y)% +; $PRE[X] =y; $                  } -        } -       Else the        { - ll X;Wuyiscanf"%lld",&x); theGetfather (x);//caculate ans -printf"%lld\n", dis[x]); Wu        } -    } About   return 0; $}

Uestc_ Qiushi brother playing games UESTC Training for Data Structures<problem h>

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.