QOJ.ac

QOJ

Time Limit: 1.0 s Memory Limit: 64 MB Total points: 140

#16372. STOGOVI

Statistics

Mirko 正在玩栈。在游戏开始时,他有一个编号为 $0$ 的空栈。在游戏的第 $i$ 步中,他会选择一个已存在的编号为 $v$ 的栈,将其复制,并执行以下操作之一:

a. 将数字 $i$ 压入新栈的栈顶。 b. 从新栈的栈顶弹出一个数字。 c. 选择另一个编号为 $w$ 的栈,并统计在新栈和编号为 $w$ 的栈中共同出现的不同数字的个数。

新创建的栈编号为 $i$。

Mirko 不喜欢操作栈,所以他希望你写一个程序来帮他完成。对于每个类型为 $b$ 的操作,输出从栈中弹出的数字;对于每个类型为 $c$ 的操作,统计并输出满足要求的数字个数。

输入格式

输入的第一行包含一个整数 $N$ ($1 \le N \le 300\,000$),表示 Mirko 游戏中的步数。

游戏的步骤按时间顺序用前 $N$ 个正整数表示。

接下来的 $N$ 行中,第 $i$ 行包含第 $i$ 步游戏的描述,格式为以下三种之一:

  • a v:表示类型 $a$ 的操作。
  • b v:表示类型 $b$ 的操作。
  • c v w:表示类型 $c$ 的操作。

每行的第一个字符表示操作类型,后面跟着的一个或两个字符表示相关的栈编号,这些编号总是区间 $[0, i - 1]$ 内的整数。

对于每个类型为 $b$ 的操作,被弹出元素的栈保证不为空。

输出格式

对于每个类型为 $b$ 或 $c$ 的操作,输出要求的数字,每个数字占一行,输出顺序与输入中操作给出的顺序一致。

样例

输入样例 1

5
a 0
a 1
b 2
c 2 3
b 4

输出样例 1

2
1
2

输入样例 2

11
a 0
a 1
a 2
a 3
a 2
c 4 5
a 5
a 6
c 8 7
b 8
b 8

输出样例 2

2
2
8
8

说明

样例 1 解释:

最初,我们有一个空栈 $S_0 = \{\}$。

  • 在第 1 步中,我们复制 $S_0$ 并将数字 $1$ 压入栈顶,得到 $S_1 = \{1\}$。
  • 在第 2 步中,我们复制 $S_1$ 并将数字 $2$ 压入栈顶,得到 $S_2 = \{1, 2\}$。
  • 在第 3 步中,我们复制 $S_2$ 并从中弹出数字 $2$,得到 $S_3 = \{1\}$。
  • 在第 4 步中,我们复制 $S_2$ 并将新栈记为 $S_4$(即 $S_4 = \{1, 2\}$),然后统计在新创建的栈 $S_4$ 和栈 $S_3$ 中共同出现的数字,唯一共同出现的数字是 $1$,因此答案为 $1$。
  • 在第 5 步中,我们复制 $S_4$ 并从中弹出数字 $2$,得到 $S_5 = \{1\}$。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.