#abc298c. C - Cards Query Problem

C - Cards Query Problem

Score : 300300 points

问题描述

我们有 NN 个编号为 11NN 的空盒子,以及无限数量的空白卡片。
按照顺序处理 QQ 个查询请求。查询请求分为以下三种类型。

  • 1 i j  ⁣:\colon 在一张空白卡片上写上数字 ii,并将其放入编号为 jj 的盒子中。
  • 2 i  ⁣:\colon升序报告盒子 ii 中卡片上书写的全部数字。
  • 3 i  ⁣:\colon升序报告包含数字 ii 的卡片的所有盒子编号。

请注意以下几点:

  • 对于第二类查询,如果盒子 ii 中有多张卡片写着相同的数字,则应打印该数字,其重复次数等于拥有这些卡片的数量。
  • 对于第三类查询,即使某个盒子包含多张带有数字 ii 的卡片,也只需打印该盒子的编号一次。

以上为通义千问 qwen-max 翻译,仅供参考。

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
  • For a query of the second kind:
    • 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.
  • 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.

Sample Input 1

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

Sample Output 1

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.
  • 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.
  • 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.
  • Boxes 44 contains a card with the number 22.

Sample Input 2

1
5
1 1 1
1 2 1
1 200000 1
2 1
3 200000

Sample Output 2

1 2 200000
1

update @ 2024/3/10 12:21:56