#P11266. 【模板】可并堆 2

【模板】可并堆 2

Background

Thanks to

https://www.luogu.com.cn/user/121027
ing the first version of the testdata generator.

gen

Problem Description

Given positive integers nn and mm, and an integer sequence a1,,na_{1,\dots,n} of length nn.

You need to maintain the sequence a1,,na_{1,\dots,n} and nn sets S1,,nS_{1,\dots,n}. Initially, Si={i}S_i=\{i\}.

Then you will perform the following four types of operations for a total of mm times. Each operation is in one of these forms:

  • 0 x y: Delete element yy from set SxS_x. It is guaranteed that yy is in SxS_x at this moment.
  • 1 x: Query miniSxai\min_{i\in S_x} a_i. It is guaranteed that SxS_x is non-empty at this moment.
  • 2 x y: Merge set SyS_y into SxS_x and clear SyS_y. It is guaranteed that both SxS_x and SyS_y are non-empty at this moment, and after this operation there will be no further operations involving set SyS_y.
  • 3 x y z: Assign aya_y to be zz. It is guaranteed that yy is in SxS_x at this moment, and z<ayz<a_y.

It is not hard to see that this is a heap template problem, so please finish it now.

Input Format

The first line contains two positive integers nn and mm.

The second line contains nn positive integers; the ii-th positive integer is aia_i.

The next mm lines each contain one operation as described above.

Output Format

For each operation 1, output one integer per line (note that it is not guaranteed to remain positive) as the answer.

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

Hint

For 20%20\% of the data, n=m=10n=m=10.

For 80%80\% of the data, n=m=105n=m=10^5.

For 100%100\% of the data, 1n,m1061\le n,m\le10^6, 1ai2×1091\le a_i\le2\times10^9. It is guaranteed that at any time, the absolute value of any element in any heap does not exceed 101510^{15} (in plain words: each operation 3 decreases a single value by at most 5×1085\times10^8).


For the last two test points, the setter’s handwritten heap and the pairing heap in pbds both ran in a few hundred milliseconds. If you are getting constant-factor TLE, you can message privately.

Translated by ChatGPT 5