#P3377. 【模板】可并堆 1

【模板】可并堆 1

Description

As stated, initially there are nn min-heaps, each containing exactly one number. Afterwards, you need to support two types of operations:

  1. 1 x y: Merge the min-heap containing the xx-th number and the min-heap containing the yy-th number (if the xx-th or yy-th number has already been deleted, or if the xx-th and yy-th numbers are in the same heap, ignore this operation).

  2. 2 x: Output the minimum number in the heap containing the xx-th number, and delete this minimum number (if there are multiple minimum numbers, delete the one that was inserted earlier; if the xx-th number has already been deleted, output 1-1 and ignore the deletion).

Input Format

The first line contains two positive integers n,mn, m, representing the initial number of min-heaps and the number of subsequent operations, respectively.

The second line contains nn positive integers, where the ii-th integer is the only number initially contained in the ii-th min-heap.

Each of the next mm lines contains 22 or 33 positive integers, representing one operation in the following formats:

Operation 1: 1 x y

Operation 2: 2 x

Output Format

The output contains several lines of integers, each corresponding to the result of an operation of type 2, in order.

5 5
1 5 4 2 3
1 1 5
1 2 5
2 2
1 4 2
2 2
1
2

Hint

【Constraints】

For 30%30\% of the testdata: n10n \le 10, m10m \le 10.
For 70%70\% of the testdata: n103n \le 10^3, m103m \le 10^3.
For 100%100\% of the testdata: n105n \le 10^5, m105m \le 10^5, and all initial numbers in the min-heaps are within the int range.

【Sample Explanation】

Initially, the five min-heaps are: {1}\{1\}, {5}\{5\}, {4}\{4\}, {2}\{2\}, {3}\{3\}.

In the first operation, merge the heap containing the 11-st number with the heap containing the 55-th number, resulting in four min-heaps: {1,3}\{1,3\}, {5}\{5\}, {4}\{4\}, {2}\{2\}.

In the second operation, merge the heap containing the 22-nd number with the heap containing the 55-th number, resulting in three min-heaps: {1,3,5}\{1,3,5\}, {4}\{4\}, {2}\{2\}.

In the third operation, output and delete the minimum of the heap containing the 22-nd number, so output 11; the first number is deleted, and the three min-heaps become: {3,5}\{3,5\}, {4}\{4\}, {2}\{2\}.

In the fourth operation, merge the heap containing the 44-th number with the heap containing the 22-nd number, resulting in two min-heaps: {2,3,5}\{2,3,5\}, {4}\{4\}.

In the fifth operation, output and delete the minimum of the heap containing the 22-nd number, so output 22; the fourth number is deleted, and the two min-heaps become: {3,5}\{3,5\}, {4}\{4\}.

Therefore, the outputs are 11 and 22 in order.

Translated by ChatGPT 5