Codeforces Round 893 (Div. 2)
E2
Rollbacks (Hard Version)
Codeforces
2600
原题
← E1→
翻译中…

This is a hard version of this problem. The only difference between the versions is that you have to solve the hard version in online mode. You can make hacks only if both versions of the problem are solved.

翻译中…

You have an array $a$, which is initially empty. You need to process queries of the following types:

翻译中…
  • + $x$ — add the integer $x$ to the end of the array $a$.
  • - $k$ — remove the last $k$ numbers from the array $a$.
  • ! — roll back the last active change (i.e., make the array $a$ the way it was before the change). In this problem, only queries of the first two types (+ and -) are considered as changes.
  • ? — find the number of distinct numbers in the array $a$.
翻译中…

Input

翻译中…

The first line contains an integer $q$ ($1 \leq q \leq 10^6$) — the number of queries.

翻译中…

The next $q$ lines contain the queries as described above.

翻译中…

It is guaranteed that

翻译中…
  • in the queries of the first type, $1 \le x \le 10^6$;
  • in the queries of the second type, $k \ge 1$ and $k$ does not exceed the current length of the array $a$;
  • at the moment of the queries of the third type, there is at least one query of the first or of the second type that can be rolled back.
翻译中…

It is also guaranteed that the number of queries of the fourth type (?) does not exceed $10^5$.

翻译中…

Note that you should solve the problem in online mode. It means that you can't read the whole input at once. You can read each query only after writing the answer for the last query, so don't forget to flush output after printing answers. You can use functions like fflush(stdout) in C++ and BufferedWriter.flush in Java or similar after each writing in your program.

翻译中…

Output

翻译中…

For each query of the fourth type output one integer — the number of distinct elements in array $a$ at the moment of query.

翻译中…

Note

翻译中…

In the first example array $a$ changes as follows:

翻译中…
  1. After the first query, $a=[1]$.
  2. After the second query, $a=[1,2]$.
  3. After the third query, $a=[1,2,2]$.
  4. At the moment of the fourth query, there are $2$ distinct intergers in the array $a$: $1$ and $2$.
  5. After the fifth query, $a=[1,2]$ (rolled back the change + 2).
  6. After the sixth query, $a=[1,2,3]$.
  7. After the seventh query, $a=[1]$.
  8. At the moment of the eigth query, there is only one $1$ in the array $a$.
  9. After the ninth query, $a=[1,1]$.
  10. At the moment of the tenth query, there are only two $1$ in the array $a$.
翻译中…

In the second example array $a$ changes as follows:

翻译中…
  1. After the first query, $a=[1]$.
  2. After the second query, $a=[1, 1\,000\,000]$.
  3. At the moment of the third query, there are $2$ distinct intergers in the array $a$: $1$ and $1\,000\,000$.
  4. After the fourth query, $a=[1]$ (rolled back the change + 1000000).
  5. After the fifth query, $a=[]$ (rolled back the change + 1).
  6. At the moment of the sixth query, there are no integers in the array $a$, so the answer to this query is $0$.
翻译中…

样例

Input #1
10
+ 1
+ 2
+ 2
?
!
+ 3
- 2
?
+ 1
?
Output #1
2
1
1
InputCopy OutputCopy
Input #2
6
+ 1
+ 1000000
?
!
!
?
Output #2
2
0
InputCopy OutputCopy
题目信息
题号
1858E2
时间限制
3 s
内存限制
256 MB
标签
数据结构
交互题
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。