#P11244. Sequence Merging and Querying

    ID: 13315 Type: Default 1000ms 256MiB

Sequence Merging and Querying

Sequence Merging and Querying

There are \(m\) integer sequences \(a_1, a_2, \dots, a_m\), each of length \(n\). You are to process \(q\) operations. Each operation is one of the following:

  • Type 1: Given two distinct indices \(x\) and \(y\) (i.e. \(x \neq y\)), merge the two sequences \(a_x\) and \(a_y\) into a sequence \(b\) of length \(2n\). Then, sort \(b\) in ascending order and update \(a_x\) with the first \(n\) elements of \(b\) and \(a_y\) with the last \(n\) elements.
  • Type 2: Given two integers \(i\) and \(j\), output the value of the \(j\)th element in sequence \(a_i\) (i.e. \(a_{i,j}\)).

You are guaranteed that all operations are valid.

inputFormat

The first line contains three integers \(m\), \(n\), and \(q\).

The next \(m\) lines each contain \(n\) space-separated integers representing the sequences \(a_1, a_2, \dots, a_m\).

Then \(q\) lines follow. Each operation is described in one line. The line begins with an integer representing the operation type:

  • If the operation type is 1, then two integers \(x\) and \(y\) (with \(x \neq y\)) follow, indicating a merge operation.
  • If the operation type is 2, then two integers \(i\) and \(j\) follow, indicating a query operation that asks for the value of \(a_{i,j}\).

outputFormat

For each query operation (operation type 2), output a single integer representing the answer on a separate line.

sample

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

6

</p>