#P10237. [yLCPC2024] E. Latent Kindom

[yLCPC2024] E. Latent Kindom

Background

Fusu and Teacher 10circle are playing the newest and hottest song Latent Kindom (LK) together on one setup.

The song LK has nn charts of different difficulties. The chart of difficulty ii has lil_i notes, which are ai,1,ai,2,ai,lia_{i, 1}, a_{i, 2}, \dots a_{i, l_i}, forming a sequence.

Fusu wants to know: if she plays the chart of difficulty ii and Teacher 10circle plays the chart of difficulty jj, what is the median of the note sequence after merging the two sequences?

Background

Fusu and Teacher 10circle are playing the newest and hottest song Latent Kindom (LK) together on one setup.

The song LK has nn charts of different difficulties. The chart of difficulty ii has lil_i notes, which are ai,1,ai,2,ai,lia_{i, 1}, a_{i, 2}, \dots a_{i, l_i}, forming a sequence.

Fusu wants to know: if she plays the chart of difficulty ii and Teacher 10circle plays the chart of difficulty jj, what is the median of the note sequence after merging the two sequences?

Problem Description

You are given nn sequences a1,a2,ana_1, a_2, \dots a_n. You need to answer qq queries. Each query gives i,ji, j, and you must find the median of the sequence obtained by concatenating aia_i and aja_j.

Concatenating two sequences x,yx, y means writing the numbers of sequence yy one by one after sequence xx. If the resulting sequence has length tt, the median is defined as the t2\left\lceil\frac t 2 \right\rceil-th smallest number in the sequence. Here, x\left\lceil x \right\rceil denotes the smallest integer that is not less than xx.

Note that the queries in this problem are independent. That is, although you are asked for the median after concatenating aia_i and aja_j, you will not actually perform any concatenation operation on the sequences.

Input Format

This problem contains multiple testcases within a single test point. The first line of the input is a positive integer TT, indicating the number of testcases. For each testcase:

The first line contains two integers n,qn, q (2n,q1062 \leq n, q \leq 10^6), representing the number of sequences and the number of queries.
The next nn lines each describe a sequence. In the ii-th line, the first integer is lil_i (1li1061 \leq l_i \leq 10^6), the length of sequence aia_i. Then follow lil_i integers, representing sequence aia_i. It is guaranteed that all numbers in the sequences are positive integers not greater than 101810^{18}.
The next qq lines each contain two integers i,ji, j (1i,jn1 \leq i, j \leq n, iji \neq j), representing a query.

It is guaranteed that, within a single test point, the sums of nn, qq, and all lil_i do not exceed 10610^6.

Output Format

For each query of each testcase, output one line with one integer, representing the answer.

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

Hint

Hint

Please note that reading and writing a large amount of data can affect the program’s efficiency. Use appropriate input/output methods, do not flush the output buffer frequently, and avoid TLE.

Translated by ChatGPT 5