cswork / OA打开练习工作台

NetflixOA #1

Command Undo Data Structure

估计简单可在线判题

考点(AI 标注)迭代器设计单调栈与堆

The state starts at 0. ADD x adds x, MUL x multiplies by x, and UNDO undoes the most recent change command that has not yet been undone; PRINT outputs the current state. PRINT never enters the change history, and there is no REDO.

输入格式

The first line contains q, followed by q lines of commands. This site uses 1≤q≤100000 and −10⁹≤x≤10⁹. Whenever UNDO runs, there is guaranteed to be a change that has not been undone, and all intermediate states fit in the signed64 range.

输出格式

On the first line output the total number of PRINT commands p; then output p lines with the state at each PRINT in order. If there is no PRINT, output only 0.

样例 1

输入:

4
ADD 5
MUL 3
UNDO
PRINT

输出:

1
5

样例 2

输入:

5
ADD 7
MUL 0
PRINT
UNDO
PRINT

输出:

2
0
7

样例 3

输入:

8
ADD 2
PRINT
ADD 4
UNDO
MUL -3
PRINT
UNDO
PRINT

输出:

3
2
-6
2

样例说明

Example 1: 5 times 3 gives 15; after undoing the multiplication, 5 is output. Example 2: multiplying by zero outputs 0, and undo can still restore 7; undo cannot be implemented by division. Example 3: 2, −6, 2 are output in order; PRINT does not take up an undo record, and after the add of 4 is undone, the new multiplication runs on state 2.