#P3224. [HNOI2012] 永无乡

    ID: 2273 远端评测题 1000ms 512MiB 尝试: 1 已通过: 0 难度: 8 上传者: 标签>2012线段树各省省选平衡树湖南

[HNOI2012] 永无乡

Description

Neverland contains nn islands, numbered from 11 to nn. Each island has its own unique importance. According to importance, these nn islands can be ranked, with ranks from 11 to nn. Some islands are connected by large bridges, through which one can travel from one island to another. If starting from island aa you can reach island bb after crossing several (including 00) bridges, then islands aa and bb are said to be connected.

There are two operations:

B x y means building a new bridge between island xx and island yy.

Q x k asks which island currently ranks the kk-th by importance among all islands connected to island xx, i.e., among all islands connected to xx, which island has the kk-th smallest importance rank. Please output the index of that island.

Input Format

The first line contains two integers separated by a space, representing the number of islands nn and the initial number of bridges mm.

The second line contains nn integers; the ii-th integer is the rank pip_i of island ii.

The next mm lines each contain two integers u,vu, v, indicating that initially there is a bridge connecting island uu and island vv.

The next line contains an integer, the number of operations qq.

The next qq lines each describe an operation. Each line first contains a character opop indicating the operation type, followed by two integers x,yx, y.

  • If opop is Q, it asks which island ranks the yy-th smallest by importance among all islands connected to xx; output the index of that island.
  • If opop is B, it means building a new bridge between island xx and island yy.

Output Format

For each query operation, output one integer on a separate line, indicating the index of the island being asked for. If such an island does not exist, output 1-1.

5 1
4 3 2 5 1
1 2
7
Q 3 2
Q 2 1
B 2 3
B 1 5
Q 2 1
Q 2 4
Q 2 3

-1
2
5
1
2

Hint

Constraints

  • For 20%20\% of the testdata, n103n \leq 10^3, q103q \leq 10^3.
  • For 100%100\% of the testdata, 1mn1051 \leq m \leq n \leq 10^5, 1q3×1051 \leq q \leq 3 \times 10^5, pp is a permutation of 1n1 \sim n, op{Q,B}op \in \{\texttt Q, \texttt B\}, 1u,v,x,yn1 \leq u, v, x, y \leq n.

Translated by ChatGPT 5