Logo
(追記) (追記ここまで)

30383번 - Major 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
1.5 초 1024 MB163222.222%

문제

In this problem, the mode of a sequence is the number that appears strictly more than half the times in the sequence. Please refer to this definition in the problem.

Initially, $n$ positive integer sequences of different lengths are given, numbered from 1ドル$ to $n,ドル and the sequences can be empty. These $n$ sequences are considered to exist, and the sequences corresponding to other numbers are considered to be non-existent.

There are $q$ operations, and operations are of the following types:

  • 1ドル\ x\ y$: Insert the number $y$ at the end of sequence $x$. It is guaranteed that sequence $x$ exists and 1ドル \le x, y \le n + q$.
  • 2ドル\ x$: Delete the number at the end of sequence $x$. It is guaranteed that sequence $x$ exists, is not empty, and 1ドル \le x \le n + q$.
  • 3ドル\ m\ x_1\ x_2\ ...\ x_m$: Concatenate sequences $x_1, x_2, \ldots, x_m$ to get a new sequence and return its mode. Return -1 if the mode does not exist. The data guarantees that for any 1ドル \le i \le m$ the sequence numbered $x_i$ still exists, 1ドル \le x_i \le n + q,ドル and the concatenated sequence is non-empty. There is no guarantee that $x_i$ are distinct. The concatenation done here won't affect future operations.
  • 4ドル\ x_1\ x_2\ x_3$: Create a new sequence numbered $x_3,ドル which is the concatenation of sequence $x_1$ and sequence $x_2$. Then, delete the sequences numbered $x_1$ and $x_2$. After this operation, sequence $x_3$ is considered to exist, and the sequences $x_1$ and $x_2$ are considered to be non-existent and will not be used again in subsequent operations. It is guaranteed that 1ドル \le x_1, x_2, x_3 \le n + q,ドル $x_1 \ne x_1,ドル the sequences $x_1$ and $x_2$ existed before the operation, and no operation used the sequence numbered $x_3$ before the operation.

입력

The first line of input contains two positive integers $n$ and $q,ドル which represent the number of initial sequences and the number of operations, respectively. It is guaranteed that $n, q \le 5 \times 10^5$.

In the next $n$ lines, the $i$-th line represents the sequence numbered $i$. The first non-negative integer $l_i$ of each line represents the length of the $i$-th sequence, followed by $l_i$ non-negative integers $a_{i,j}$ representing the elements of the sequence in order. Let $C_l = \Sigma l_i$ represent the sum of the input sequence lengths, then it is guaranteed that $C_l \le 5 \times 10^5$ and $a_{i,j} \le n + q$.

Each of the next $q$ lines represent an operation in the format described above, consisting of several integers. Let $C_m = \Sigma m$ represent the sum of all sequences that need to be concatenated in operation 3, then it is guaranteed that $C_m \leq 5 \times 10^{5}$.

출력

For each type 3 query, output an integer on a new line, the answer to the query.

제한

For all test data, it is guaranteed that 1ドル \le n, q, C_m, C_l \le 5 \times 10^{5}$.

예제 입력 1

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

1
3
-1
3
-1

The first query queries the mode of sequence 1ドル$. Since the sequence contains two 1ドル$s, more than half the length of the sequence, the mode is 1ドル$. The second query queries the mode of sequence 2ドル$. Since the sequence contains only 3ドル$s, the mode is 3ドル$. The third query asks for the mode of sequence 3ドル$. At this time, sequence 3ドル$ is $(1, 1, 2, 3, 3, 3),ドル and there is no number occuring more than 3ドル$ times, so the output is -1.

예제 입력 2

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

예제 출력 2

-1
2
2
4

The first query asks for the mode obtained after concatenating sequences 1,ドル 2, 3, 4$. The result of concatenation is $(1, 2, 3, 4),ドル and there is no number with more than two occurrences, so the output is -1. The fourth query is the mode of the sequence obtained after concatenating sequences 1,ドル 2, 3, 4$. The result of the concatenation is $(1, 2, 2, 4, 4, 4, 4),ドル which has a mode of 4ドル$.

힌트

추가 예제

출처

Olympiad > National Olympiad in Informatics (China) > NOI 2022 1번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /