Stack Copying Game
Time limit1sMemory limit64 MB
Maintain up to 300,000 persistent stack versions built by push, pop, or copy, and answer popped values and common-element counts for pairs of versions.
Problem
Mirko is playing with stacks. When the game starts he has a single empty stack, numbered . In step of the game he picks one of the stacks that already exist, calls its number , copies that stack, and then does one of these three things.
- Push the number onto the top of the copy.
- Pop the number on top of the copy.
- Pick one more stack, call its number , and count how many distinct numbers sit in both the copy and stack .
The stack he just created is numbered .
Mirko does not want to handle the stacks himself, so write a program that does it for him. For every operation of type b, print the number popped off the stack. For every operation of type c, print how many numbers satisfy the condition.
Input
The first line contains , the number of steps in Mirko's game. ()
The steps of the game are numbered through in chronological order.
The th of the next lines describes step in one of these three forms.
- "a v" for an operation of type a.
- "b v" for an operation of type b.
- "c v w" for an operation of type c.
The first character of the line gives the type of the operation. The one or two numbers after it are the stack labels the operation uses, and they are always integers in .
For an operation of type b, the stack the element is popped from is not empty.
Output
For every operation of type b and every operation of type c, print the number you computed, one per line, in the order the operations appear in the input.
Note
Consider the first sample. At the start the only stack is . Step 1 copies and pushes on top, so . Step 2 copies and pushes on top, so . Step 3 copies and pops , so . Step 4 copies and calls the copy , then counts the numbers that appear in both and . The only such number is , so the answer is . Step 5 copies and pops , so .