luogu#P4810. [COCI 2014/2015 #3] STOGOVI

[COCI 2014/2015 #3] STOGOVI

Problem Description

Translated from COCI 2014/2015 Contest 3 T5 “STOGOVI”.

Mirko is playing with stacks. At the start of the game, he has an empty stack numbered 00. In step ii of the game, he chooses an existing stack numbered vv, makes a copy of it, and then performs one of the following operations:

  1. Push the number ii onto the top of the new stack.

  2. Pop (delete) the number on the top of the new stack.

  3. Choose another stack numbered ww, and count how many distinct numbers appear in both the new stack and stack ww.

The newly created stack is numbered ii.

Mirko does not like simulating this process with stacks by himself, so he wants you to write a program to carry it out. For each pop operation, output the deleted number, and for each counting operation, output the result.

Input Format

The first line contains an integer NN (1N300 000)(1 \le N \le 300\ 000), the number of steps in the game.

The steps of the game are numbered by the first NN integers in chronological order.

Each of the next NN lines describes step ii in one of the following three formats:

  • a v, meaning a push operation.

  • b v, meaning a pop operation.

  • c v w, meaning a counting operation.

All indices involved in an operation are guaranteed to be in [0,i1][0, i - 1].

It is guaranteed that no pop operation will be applied to an empty stack.

Output Format

For each pop or counting operation, output one line containing the requested number, in the order of operations given in the input file.

5
a 0
a 1
b 2
c 2 3
b 4
2
1
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
8
8

Hint

Sample Explanation 1

At the start, we have the stack S0={}S_0 = \{\}.

In the first step, we copy S0S_0 and push the number 11 onto the top, so S1={1}S_1 = \{1\}.

In the second step, we copy S1S_1 and push the number 22 onto the top, so S2={1,2}S_2 = \{1,2\}.

In the third step, we copy S2S_2 and pop the number 22, so S3={1}S_3 = \{1\}.

In the fourth step, we copy S2S_2 as S4S_4, and count how many distinct numbers appear in both S4S_4 and S3S_3. The only number that appears in both stacks is 11, so the answer is 11.

In the fifth step, we copy S4S_4 and pop the number 22, so S5={1}S_5 = \{1\}.

Translated by ChatGPT 5