CF1858E2.Rollbacks (Hard Version)

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 aa , which is initially empty. You need to process queries of the following types:

    • xx — add the integer xx to the end of the array aa .
    • kk — remove the last kk numbers from the array aa .
  • ! — roll back the last active change (i.e., make the array aa 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 aa .

输入格式

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

The next qq lines contain the queries as described above.

It is guaranteed that

  • in the queries of the first type, 1x1061 \le x \le 10^6 ;
  • in the queries of the second type, k1k \ge 1 and kk does not exceed the current length of the array aa ;
  • 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 10510^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.

输出格式

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

输入输出样例

  • 输入#1

    10
    + 1
    + 2
    + 2
    ?
    !
    + 3
    - 2
    ?
    + 1
    ?

    输出#1

    2
    1
    1
  • 输入#2

    6
    + 1
    + 1000000
    ?
    !
    !
    ?

    输出#2

    2
    0

说明/提示

In the first example array aa changes as follows:

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

In the second example array aa changes as follows:

  1. After the first query, a=[1]a=[1] .
  2. After the second query, a=[1,1000000]a=[1, 1\,000\,000] .
  3. At the moment of the third query, there are 22 distinct intergers in the array aa : 11 and 10000001\,000\,000 .
  4. After the fourth query, a=[1]a=[1] (rolled back the change + 1000000).
  5. After the fifth query, a=[]a=[] (rolled back the change + 1).
  6. At the moment of the sixth query, there are no integers in the array aa , so the answer to this query is 00 .
首页