#NOI20221A. 「NOI2022」众数 (Majority element)
「NOI2022」众数 (Majority element)
Description
For a sequence, the majority element of it is defined as the number whose occurrence is strictly more than half the size of the sequence. This is different from the mode of a sequence. Please refer to this definition in this problem.
You're given positive integer sequences of varying length, labeled by . The initial sequence may be empty. These sequences exist while sequences labelled by other numbers are deemed non-existent.
There are operations of following types:
-
: Insert number at the end of sequence . It is guaranteed that sequence exists, and .
-
: Remove the last number from sequence . It is guaranteed that sequence exists, is non-empty, and .
-
: Concatenate sequences in order into a new sequence and query its majority element. If no such number exists, return . It is guaranteed that sequence still exists for any , , and the resulting sequence is non-empty. Note: it is not guaranteed that are distinct, and the combining operation in this query has no effect on following operations.
-
: Create a new sequence as the result of successively inserting numbers in sequence to the end of sequence , then remove the sequences corresponding to numbers . At this time sequence is deemed existent, sequence don't exist and won't be used in following operations. It is guaranteed that , , sequences exist before this operation, and sequence is not used by any preceding operations.
Input Format
Read from file major.in
.
The first line of input contains two integers and , each indicating the number of sequences and operations. It is guaranteed that , .
For the following lines, the th line corresponds to sequence . Each line begins with a non-negative integer indicating the number count of the initial th sequence. Following are nonnegative numbers representing the numbers in the sequence in order. Suppose is the sum of length of the inputted sequences, then it is guaranteed that , .
Each of the following lines consists of several integers representing an operation, formatted as stated in the description.
Suppose is the sum of number of sequences to be concatenated in all type operations, then it is guaranteed that .
Output Format
Output to file major.out
.
For each query, output a line of one integer indicating 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
Sample 3
See files [major3.in](file://major3.in) and [major3.ans](file://major3.ans) in the attachment. This sample satisfies the constraints of tests .
Sample 4
See files [major4.in](file://major4.in) and [major4.ans](file://major4.ans) in the attachment. This sample satisfies the constraints of tests .
Constraints and Hint
For all the tests, .
Test Number | Special Case A | Special Case B | Special Case C | ||
---|---|---|---|---|---|
No | Yes | ||||
Yes | Yes | ||||
No | No | ||||
No | Yes | ||||
No | Yes | ||||
No | |||||
Yes | Yes | ||||
No | No | ||||
No | Yes | ||||
No | Yes | ||||
No |
Special case A: It is guaranteed that and there's no type operation.
Special case B: It is guaranteed that at any time and in any sequence there are only numbers and .
Special case C: It is guaranteed that there's no type operation.