#P9216. [入门赛 #11] 写大作业 (Hard Version)

[入门赛 #11] 写大作业 (Hard Version)

Background

The difference from the Easy Version is that the input consists of sequences rather than strings, the input/output format is different, and the data scale is different.

Problem Description

Fusu has been awake for 3636 hours to write a Theory of Computation final project.

To be able to sleep sooner, Fusu found nn references. The ii-th reference is a sequence aia_i, and she plans to combine these references.

Specifically, there are two types of operations:

  • 1 x y: Concatenate the whole sequence axa_x to the end of aya_y, then delete axa_x.
  • 2 x y: Query whether axa_x and aya_y are similar.

We guarantee that after an operation 1 x y occurs, the sequence axa_x will not appear in any subsequent operations. That is, operation type 11 occurs at most n1n - 1 times.

The definition of similar is: for two sequences axa_x and aya_y, if there exists a way to reorder axa_x such that the reordered axa_x is equal to aya_y, then axa_x and aya_y are called similar.

For example, suppose a1=1,2a_1 = 1,2, a2=3,4a_2 = 3,4, and a3=1,2,3,4a_3 = 1,2,3,4. After executing 1 1 2, a1a_1 is deleted, and a2=3,4,1,2a_2 = 3,4,1,2, while a3=1,2,3,4a_3 = 1,2,3,4. Continuing with 2 2 3, since we can reorder a2a_2 into 1,2,3,41,2,3,4, a2a_2 and a3a_3 are similar.

Note that operation type 22 does not actually modify any sequence.

Input Format

The first line contains two integers, denoting the number of references nn and the number of operations qq.
The next nn lines each describe a sequence. Line ii describes aia_i:
the first number is mim_i, the length of aia_i, followed by mim_i numbers describing the sequence aia_i.
The next qq lines each contain three integers op,x,yop, x, y, with meanings as described in the Description.

Output Format

To avoid excessive output, output one line with one integer, denoting the bitwise XOR of the operation indices of all queries whose answer is the two sequences are similar.

Operation indices start from 11. Both types of operations increase the index by 11. You may refer to the sample explanation to understand the output format.

4 5
2 1 2
2 3 4
4 1 2 3 4
4 1 2 3 3
1 1 2
2 2 3
2 3 4
2 2 4
2 3 2
7

Hint

Sample Explanation

There are five operations in total. Their indices and answers are as follows: | Index | Operation | Answer | | :-: | :-: | :-: | | 11 | 1 1 2 | Not a query operation | | 22 | 2 2 3 | Similar | | 33 | 2 3 4 | Not similar | | 44 | 2 2 4 | Not similar | | 55 | 2 2 3 | Similar |

We can see that the operation indices of queries answered the two sequences are similar are 22 and 55. Their bitwise XOR is 77. Therefore, the output is 77.

Constraints and Notes

For all test points, it is guaranteed that 1n1051 \leq n \leq 10^5, 1q5×1061 \leq q \leq 5 \times 10^6, 1mi1051 \leq m_i \leq 10^5, i=1nmi5×105\sum_{i = 1}^n m_i \leq 5 \times 10^5. All elements in the sequences are non-negative integers not exceeding 10910^9.

The testdata guarantees that the elements are generated in the following way: for a sequence, an upper bound kk on the element values is specified, then mim_i integers are generated uniformly at random in [0,k][0, k] as the sequence aia_i. Note that kk is not necessarily 10910^9.

Translated by ChatGPT 5