Merge groups

0

0 votes
Disjoint-set data structure, Hard, Basics of Disjoint Data Structures, Data Structures, Disjoint set
Problem

You are given an array A of N elements. Each element belongs to its own group initially.

You need to process the following two types of queries:

1  X  Y: The array elements which are at positions  X and Y in the array  A  merge their group.
2  X: Print the maximum absolute difference of the array values that belong to the group of Xth element of the array.

Note: It is possible that the elements X and Y in the query of type 1 already be in the same group. You must not solve that query.

Input format

  • First line: An integer N as input that denotes the total number of elements of the array
  • Next line: N space-separated integers that denote the elements of that array
  • Next line: An integer Q that denotes the total number of queries
  • Next Q lines: Description of each query that begins with an integer type that is 1 for the first type of queries and 2 for the second type of queries

Output format
For each query of type 2, print the answer in a new line.

Constraints

Subtask 1: 50 marks

1N103

|A[i]|103

1Q103

1X,YN

Subtask 2: 50 marks

1N106

|A[i]|109

1Q5105

1X,YN

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Query 1: 4th element is alone in group, So maximum absolute difference will be 0. ([5], [3], [2], [9]).

Query 2: 1st and 4th element will join to form a group. ([5, 9], [3], [2], [5, 9]).

Query 3: 4th element had formed a group with 1st element [5,9]. So maximum absolute difference will be 4 (as 9-5=4). ([5, 9], [3], [2], [5, 9])

Query 4: 3rd element is alone in group. So maximum absolute difference will be 0. ([5, 9], [3], [2], [5, 9]).

Editor Image

?