Sky株式会社プログラミングコンテスト2026(AtCoder Beginner Contest 455)
D
卡牌堆查询
Card Pile Query
AtCoder
⌀ 796
原题
← BG →

卡牌堆查询AI (CI翻译)

Problem Statement

题目描述

AI (CI翻译)

There are $N$ cards and $N$ piles of cards.

有 $N$ 张卡牌和 $N$ 堆卡牌。AI (CI翻译)

The cards and the piles are numbered $1, 2, \ldots, N$. Initially, pile $i$ contains only card $i$.

卡牌和卡牌堆的编号均为 $1, 2, \ldots, N$。初始时,第 $i$ 堆中仅有卡牌 $i$。AI (CI翻译)

Perform the following operation for each $i = 1, 2, \ldots, Q$ in order:

按顺序对每个 $i = 1, 2, \ldots, Q$ 执行以下操作:AI (CI翻译)

  • Move card $C_i$ and all cards stacked on top of card $C_i$, preserving their order, on top of card $P_i$. It is guaranteed that immediately before performing the operation, cards $C_i$ and $P_i$ are in different piles, and card $P_i$ is on top of some pile.
  • 将卡牌 $C_i$ 以及叠放在卡牌 $C_i$ 上方的所有卡牌保持顺序,一并移动到卡牌 $P_i$ 的上方。保证在执行操作前,卡牌 $C_i$ 和 $P_i$ 位于不同的卡牌堆中,且卡牌 $P_i$ 位于某一堆卡牌的最上方。
AI (CI翻译)

Find the number of cards in each pile after all operations are completed.

求所有操作完成后每一堆中的卡牌数量。AI (CI翻译)

Constraints

约束条件

AI (CI翻译)
  • $1 \leq N, Q \leq 3 \times 10^5$
  • $1 \leq C_i, P_i \leq N$
  • When the operations are performed in order, immediately before each operation, cards $C_i$ and $P_i$ are in different piles.
  • When the operations are performed in order, immediately before each operation, card $P_i$ is on top of some pile.
  • All input values are integers.
  • $1 \leq N, Q \leq 3 \times 10^5$
  • $1 \leq C_i, P_i \leq N$
  • 按顺序执行操作时,在每次操作前,卡牌 $C_i$ 和 $P_i$ 位于不同的卡牌堆中。
  • 按顺序执行操作时,在每次操作前,卡牌 $P_i$ 位于某一堆卡牌的最上方。
  • 所有输入值均为整数。
AI (CI翻译)

Input

输入格式

AI (CI翻译)

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

输入从标准输入中按以下格式给出:AI (CI翻译)

N Q
C_1 P_1
C_2 P_2
\vdots
C_Q P_Q

N Q
C_1 P_1
C_2 P_2
\vdots
C_Q P_Q
AI (CI翻译)

Output

输出格式

AI (CI翻译)

Let $A_i$ be the number of cards in pile $i$ at the end. Output $A_1, A_2, \ldots, A_N$ in this order, separated by spaces.

设操作结束时第 $i$ 堆中的卡牌数量为 $A_i$。按顺序输出 $A_1, A_2, \ldots, A_N$,相邻数字之间用空格分隔。AI (CI翻译)

样例

Input #1
5 4
1 3
4 5
1 4
4 2
Output #1
0 3 1 0 1
The sequence of operations proceeds as shown in the figure below. ![image](https://img.atcoder.jp/abc455/aca7dea26cc92ed4ff7e4992208922ab.png)
Input #2
7 8
3 1
5 4
2 5
5 7
2 3
6 2
3 4
5 1
Output #2
2 0 0 4 0 0 1
题目信息

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。