atcoder#ABC298C. [ABC298C] Cards Query Problem

[ABC298C] Cards Query Problem

Score : 300300 points

Problem Statement

We have NN boxes numbered 11 to NN that are initially empty, and an unlimited number of blank cards. Process QQ queries in order. There are three kinds of queries as follows.

  • 1 i j  ⁣:\colon Write the number ii on a blank card and put it into box jj.
  • 2 i  ⁣:\colon Report all numbers written on the cards in box ii, in ascending order.
  • 3 i  ⁣:\colon Report all box numbers of the boxes that contain a card with the number ii, in ascending order.

Here, note the following.

  • In a query of the second kind, if box ii contains multiple cards with the same number, that number should be printed the number of times equal to the number of those cards.
  • In a query of the third kind, even if a box contains multiple cards with the number ii, the box number of that box should be printed only once.

Constraints

  • 1N,Q2×1051 \leq N, Q \leq 2 \times 10^5
  • For a query of the first kind:- 1i2×1051 \leq i \leq 2 \times 10^5
    • 1jN1 \leq j \leq N
  • 1i2×1051 \leq i \leq 2 \times 10^5
  • 1jN1 \leq j \leq N
  • For a query of the second kind:- 1iN1 \leq i \leq N
    • Box ii contains some cards when this query is given.
  • 1iN1 \leq i \leq N
  • Box ii contains some cards when this query is given.
  • For a query of the third kind:- 1i2×1051 \leq i \leq 2 \times 10^5
    • Some boxes contain a card with the number ii when this query is given.
  • 1i2×1051 \leq i \leq 2 \times 10^5
  • Some boxes contain a card with the number ii when this query is given.
  • At most 2×1052 \times 10^5 numbers are to be reported.
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NN

QQ

query1\mathrm{query}_1

query2\mathrm{query}_2

\vdots

queryQ\mathrm{query}_Q

Here, queryq\mathrm{query}_q denotes the qq-th query, which is in one of the following formats:

11 ii jj

22 ii

33 ii

Output

Respond to the queries of the second and third kinds in order. For each of those queries, print one line containing the elements to be reported in ascending order, with spaces in between.

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

Let us process the queries in order.

  • Write 11 on a card and put it into box 11.
  • Write 22 on a card and put it into box 44.
  • Write 11 on a card and put it into box 44.
  • Box 44 contains cards with the numbers 11 and 22.- Print 11 and 22 in this order.
  • Print 11 and 22 in this order.
  • Write 11 on a card and put it into box 44.
  • Box 44 contains cards with the numbers 11, 11, and 22.- Note that you should print 11 twice.
  • Note that you should print 11 twice.
  • Boxes 11 and 44 contain a card with the number 11.- Note that you should print 44 only once, even though box 44 contains two cards with the number 11.
  • Note that you should print 44 only once, even though box 44 contains two cards with the number 11.
  • Boxes 44 contains a card with the number 22.
1
5
1 1 1
1 2 1
1 200000 1
2 1
3 200000
1 2 200000
1