#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 positive-integer sequences with different lengths, numbered . An initial sequence may be empty. These sequences are considered to exist, and sequences with other indices are considered nonexistent.
There are operations of the following types:
- : Insert the number at the end of sequence . It is guaranteed that sequence exists, and .
- : Delete the last number of sequence . It is guaranteed that sequence exists and is non-empty, and .
- : Concatenate sequences in order to obtain a new sequence, and query its majority element. If there is no number satisfying the condition above, output . The testdata guarantees that for any , is a sequence that still exists, , and the concatenated sequence is non-empty. Note: are not guaranteed to be distinct, and the merge operation in a query does not affect subsequent operations.
- : Create a new sequence with index , which is the result of appending all numbers of sequence to the end of sequence in order, and then delete sequences and . After that, sequence is considered to exist, while sequences and are considered nonexistent and will not be used again in subsequent operations. It is guaranteed that , , sequences and exist before the operation, and no sequence has used index before this operation.
Input Format
The first line contains two positive integers and , representing the number of sequences and the number of operations. It is guaranteed that and .
The next lines describe the sequences. On line , the first non-negative integer denotes the number of elements in the initial sequence . Then follow non-negative integers in order, representing the elements in the sequence. Let be the total length of all input sequences. It is guaranteed that and .
The next lines each contain several positive integers describing one operation, given in the format specified in the statement.
Let be the total number of sequences to be concatenated across all type operations. It is guaranteed that .
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 . Since the sequence contains two 's, which is more than half of the sequence length, the majority element is .
The second query asks for the majority element of sequence . Since the sequence contains only , the majority element is .
The third query asks for the majority element of sequence . At this time, sequence is . There is no number that appears more than times, so output .
[Sample Explanation #2]
The first query asks for the majority element of the sequence obtained by concatenating sequences . The result is . There is no number that appears more than twice, so output .
The fourth query asks for the majority element of the sequence obtained by concatenating sequences . The result is , and the majority element is .
[Sample #3]
See major/major3.in and major/major3.ans in the attachment.
This sample satisfies the constraints of test points .
[Sample #4]
See major/major4.in and major/major4.ans in the attachment.
This sample satisfies the constraints of test points .
[Constraints]
For all test data, it is guaranteed that .
| Test Point IDs | Special Property A | Special Property B | Special Property C | ||
|---|---|---|---|---|---|
| No | Yes | ||||
| Yes | Yes | ||||
| No | No | ||||
| No | Yes | ||||
| No | Yes | ||||
| No | |||||
| Yes | Yes | ||||
| No | No | ||||
| No | Yes | ||||
| No | Yes | ||||
| No | |||||
Special Property A: It is guaranteed that and there is no operation of type .
Special Property B: It is guaranteed that at any time, every sequence contains only the numbers and .
Special Property C: It is guaranteed that there is no operation of type .
Translated by ChatGPT 5