#P2713. 罗马游戏

罗马游戏

Description

The Roman emperor enjoys a killing game. His army has nn soldiers, and each soldier initially forms an independent group. A recent plane geometry test was held, and each soldier received a score. The emperor loves plane geometry and despises soldiers with low scores.

He decides to play the following game. He can issue two types of commands:

  • M i j Merge the groups containing ii and jj into one group. If either ii or jj is already dead, ignore this command.
  • K i Kill the lowest-scoring soldier in the group containing ii. If soldier ii is already dead, ignore this command.

After each K i command, the general should report the score of the soldier who was killed (if the command is ignored, report 00).

It is guaranteed that all soldiers’ scores are pairwise distinct.

Input Format

The first line contains an integer nn, the number of soldiers.

The second line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n, where aia_i is the score of the soldier with index ii.

The third line contains an integer mm.

The (3+i)(3 + i)-th line describes the ii-th command, which is either M i j or K i.

Output Format

For each K i command, output the score of the killed soldier (if such a person does not exist, output 00).

5
100 90 66 99 10
7
M 1 5
K 1
K 1
M 2 3
M 3 4
K 5
K 4
10
100
0
66

Hint

Constraints:

  • For 100%100\% of the testdata, 1n1061 \le n \le 10^6, 1m1051 \le m \le 10^5, 0ai1070 \le a_i \le 10^7. Note: In the testdata, in the command M i j, ii and jj may already be in the same group.

Translated by ChatGPT 5