#P6864. [RC-03] 记忆

[RC-03] 记忆

Description

There is a bracket string SS. Initially, SS contains only one pair of brackets (that is, the initial SS is ()). Then there are nn operations of three types:

  1. Append one pair of brackets to the end of the current SS (that is, SS becomes S());

  2. Add one pair of brackets around the outside of the current SS (that is, SS becomes (S));

  3. Cancel the xx-th operation, i.e., remove all effects caused by the xx-th operation (for example, if the xx-th operation is also a cancel operation and it canceled the yy-th operation, then the essence of the current operation is to restore the effect of the yy-th operation).

After each operation, you need to output the number of non-empty substrings (substrings must be contiguous) of SS that can be matched as brackets.

A bracket string can be matched if and only if the numbers of left and right brackets are equal, and in every prefix, the number of left brackets is not less than the number of right brackets.

Input Format

The first line: an integer nn, the number of operations.

The next nn lines: each line starts with an integer opop, indicating the type of the operation:

If op=1op=1, it means operation 11 is performed.

If op=2op=2, it means operation 22 is performed.

If op=3op=3, there is also an integer xx afterwards, meaning operation 33 is performed and the xx-th operation is canceled (operations are numbered from 11 to nn, and it is guaranteed that the xx-th operation has already happened). Note that a cancel operation does not affect any operation indices. Indices depend only on the input order.

Output Format

Output nn lines in total. On line ii, output an integer ansians_i, the number of non-empty substrings of the whole bracket string that can be matched after the ii-th operation ends.

6
1
2
3 1
1
3 3
3 5

3
4
2
4
6
4

10
1
2
2
3 2
1
3 3
3 6
1
2
1

3
4
5
4
6
6
6
9
10
12

Hint

[Sample 11 Explanation]

Use S[i,j]S[i,j] to denote the substring from SiS_i to SjS_j (indices start from 11).

Initially, SS is (). After each operation:

After the 11-st operation: SS is ()(). The matched substrings are S[1,2]S[1,2], S[1,4]S[1,4], and S[3,4]S[3,4], for a total of 33.

After the 22-nd operation: SS is (()()). The matched substrings are S[1,6]S[1,6], S[2,3]S[2,3], S[2,5]S[2,5], and S[4,5]S[4,5], for a total of 44.

After the 33-rd operation: SS is (()). The matched substrings are S[1,4]S[1,4] and S[2,3]S[2,3], for a total of 22.

After the 44-th operation: SS is (())(). The matched substrings are S[1,4]S[1,4], S[1,6]S[1,6], S[2,3]S[2,3], and S[5,6]S[5,6], for a total of 44.

After the 55-th operation: SS is (()())(). The matched substrings are S[1,6]S[1,6], S[1,8]S[1,8], S[2,3]S[2,3], S[2,5]S[2,5], S[4,5]S[4,5], and S[7,8]S[7,8], for a total of 66.

After the 66-th operation: SS is (())(). The matched substrings are S[1,4]S[1,4], S[1,6]S[1,6], S[2,3]S[2,3], and S[5,6]S[5,6], for a total of 44.


[Constraints]

This problem uses bundled testdata.

For all data: 1n2×1051\leq n\leq 2\times 10^5, op{1,2,3}op\in \{1,2,3\}, 1xn1\leq x\leq n. Each operation can be canceled at most once in form (i.e., all xx are distinct).

Subtask ID nn\leq opop\in Score
Subtask 1 100100 {1,2,3}\{1,2,3\} 1010
Subtask 2 10310^3
Subtask 3 10510^5 3030
Subtask 4 2×1052\times 10^5 {1,2}\{1,2\} 2020
Subtask 5 {1,2,3}\{1,2,3\} 3030

Translated by ChatGPT 5