#P8496. [NOI2022] 众数

[NOI2022] 众数

Problem Description

For a sequence, define its majority element as a number whose occurrence count is strictly greater than half of the sequence length. Note that this definition differs from the usual one. In this problem, please follow the definition given here.

Initially, you are given nn positive-integer sequences with different lengths, numbered 1∼n1 \sim n. An initial sequence may be empty. These nn sequences are considered to exist, and sequences with other indices are considered nonexistent.

There are qq operations of the following types:

  • 1 x y1 \ x \ y: Insert the number yy at the end of sequence xx. It is guaranteed that sequence xx exists, and 1≤x,y≤n+q1 \le x, y \le n + q.
  • 2 x2 \ x: Delete the last number of sequence xx. It is guaranteed that sequence xx exists and is non-empty, and 1≤x≤n+q1 \le x \le n + q.
  • 3 m x1 x2… xm3 \ m \ x_1 \ x_2 \dots \ x_m: Concatenate sequences x1,x2,…,xmx_1, x_2, \ldots, x_m in order to obtain a new sequence, and query its majority element. If there is no number satisfying the condition above, output −1-1. The testdata guarantees that for any 1≤i≤m1 \le i \le m, xix_i is a sequence that still exists, 1≤xi≤n+q1 \le x_i \le n + q, and the concatenated sequence is non-empty. Note: x1,…,xm\boldsymbol{x_1, \ldots, x_m} are not guaranteed to be distinct, and the merge operation in a query does not affect subsequent operations.
  • 4 x1 x2 x34 \ x_1 \ x_2 \ x_3: Create a new sequence with index x3x_3, which is the result of appending all numbers of sequence x2x_2 to the end of sequence x1x_1 in order, and then delete sequences x1x_1 and x2x_2. After that, sequence x3x_3 is considered to exist, while sequences x1x_1 and x2x_2 are considered nonexistent and will not be used again in subsequent operations. It is guaranteed that 1≤x1,x2,x3≤n+q1 \le x_1, x_2, x_3 \le n + q, x1≠x2x_1 \ne x_2, sequences x1x_1 and x2x_2 exist before the operation, and no sequence has used index x3x_3 before this operation.

Input Format

The first line contains two positive integers nn and qq, representing the number of sequences and the number of operations. It is guaranteed that n≤5×105n \le 5 \times {10}^5 and q≤5×105q \le 5 \times {10}^5.

The next nn lines describe the sequences. On line ii, the first non-negative integer lil_i denotes the number of elements in the initial sequence ii. Then follow lil_i non-negative integers ai,ja_{i,j} in order, representing the elements in the sequence. Let Cl=∑liC_l = \sum l_i be the total length of all input sequences. It is guaranteed that Cl≤5×105C_l \le 5 \times {10}^5 and ai,j≤n+qa_{i,j} \le n + q.

The next qq lines each contain several positive integers describing one operation, given in the format specified in the statement.

Let Cm=∑mC_m = \sum m be the total number of sequences to be concatenated across all type 33 operations. It is guaranteed that Cm≤5×105C_m \le 5 \times {10}^5.

Output Format

For each query, output one integer per line as the corresponding answer.

2 8
3 1 1 2
3 3 3 3
3 1 1
3 1 2
4 2 1 3
3 1 3
2 3
3 1 3
1 3 1
3 1 3

1
3
-1
3
-1

4 9
1 1
1 2
1 3
1 4
3 4 1 2 3 4
1 1 2
3 2 1 2
2 3
3 3 1 2 3
1 4 4
1 4 4
1 4 4
3 4 1 2 3 4

-1
2
2
4

Hint

[Sample Explanation #1]

The first query asks for the majority element of sequence 11. Since the sequence contains two 11's, which is more than half of the sequence length, the majority element is 11.

The second query asks for the majority element of sequence 22. Since the sequence contains only 33, the majority element is 33.

The third query asks for the majority element of sequence 33. At this time, sequence 33 is (3,3,3,1,1,2)(3, 3, 3, 1, 1, 2). There is no number that appears more than 33 times, so output −1-1.


[Sample Explanation #2]

The first query asks for the majority element of the sequence obtained by concatenating sequences 1,2,3,41, 2, 3, 4. The result is (1,2,3,4)(1, 2, 3, 4). There is no number that appears more than twice, so output −1-1.

The fourth query asks for the majority element of the sequence obtained by concatenating sequences 1,2,3,41, 2, 3, 4. The result is (1,2,2,4,4,4,4)(1, 2, 2, 4, 4, 4, 4), and the majority element is 44.


[Sample #3]

See major/major3.in and major/major3.ans in the attachment.

This sample satisfies the constraints of test points 1∼31 \sim 3.


[Sample #4]

See major/major4.in and major/major4.ans in the attachment.

This sample satisfies the constraints of test points 11∼1211 \sim 12.


[Constraints]

For all test data, it is guaranteed that 1≤n,q,Cm,Cl≤5×1051 \le n, q, C_m, C_l \le 5 \times {10}^5.

n,qn, q Cm,ClC_m, C_l Test Point IDs Special Property A Special Property B Special Property C
≤300\le 300 1∼31 \sim 3 No Yes
≤4000\le 4000 4∼74 \sim 7
≤105\le {10}^5 88 Yes Yes
99 No No
1010 No Yes
11∼1211 \sim 12 No Yes
1313 No
≤5×105\le 5 \times {10}^5 1414 Yes Yes
1515 No No
1616 No Yes
17∼1817 \sim 18 No Yes
19∼2019 \sim 20 No

Special Property A: It is guaranteed that n=1n = 1 and there is no operation of type 44.
Special Property B: It is guaranteed that at any time, every sequence contains only the numbers 11 and 22.
Special Property C: It is guaranteed that there is no operation of type 22.

Translated by ChatGPT 5