Rollbacks (Hard Version) (Codeforces 1858E2)
Tags: Others, interactive

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:   After the first query, $$$a=[1]$$$.  After the second query, $$$a=[1,2]$$$.  After the third query, $$$a=[1,2,2]$$$.  At the moment of the fourth query, there are $$$2$$$ distinct intergers in the array $$$a$$$: $$$1$$$ and $$$2$$$.  After the fifth query, $$$a=[1,2]$$$ (rolled back the change + 2).  After the sixth query, $$$a=[1,2,3]$$$.  After the seventh query, $$$a=[1]$$$.  At the moment of the eigth query, there is only one $$$1$$$ in the array $$$a$$$.  After the ninth query, $$$a=[1,1]$$$.  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:   After the first query, $$$a=[1]$$$.  After the second query, $$$a=[1, 1\,000\,000]$$$.  At the moment of the third query, there are $$$2$$$ distinct intergers in the array $$$a$$$: $$$1$$$ and $$$1\,000\,000$$$.  After the fourth query, $$$a=[1]$$$ (rolled back the change + 1000000).  After the fifth query, $$$a=[]$$$ (rolled back the change + 1).  At the moment of the sixth query, there are no integers in the array $$$a$$$, so the answer to this query is $$$0$$$. 

Sample Inputs:
10
+ 1
+ 2
+ 2
?
!
+ 3
- 2
?
+ 1
?

6
+ 1
+ 1000000
?
!
!
?

Sample Outputs:
2
1
1

2
0

Solution (Python):
import sys, os
input = sys.stdin.buffer.readline

def f(u, v):
    return u * (q + 1) + v
q = int(input())
(u, m) = (0, 0)
st = [0]
st0 = []
parent = [-1] * (20 * (q + 1))
a = [0] * (q + 1)
pow2 = [1]
for _ in range(20):
    pow2.append(2 * pow2[-1])
l = pow(10, 6) + 5
c = [l] * l
q0 = []
ans0 = [0]
ans = 0
for _ in range(q):
    t = list(input().rstrip().decode().split())
    if t[0] == '+':
        x = int(t[1])
        m += 1
        (a[m], parent[m]) = (x, u)
        st.append(m)
        for i in range(20):
            p = parent[f(i, u)]
            if p == -1:
                break
            parent[f(i + 1, m)] = p
            u = p
        if c[x] == l:
            st0.append((x, c[x]))
            c[x] = m
            ans += 1
        else:
            u = m
            for i in range(19, -1, -1):
                if parent[f(i, u)] >= c[x]:
                    u = parent[f(i, u)]
            if u ^ c[x]:
                st0.append((x, c[x]))
                c[x] = m
                ans += 1
            else:
                st0.append((-1, -1))
        ans0.append(ans)
        u = m
    elif t[0] == '-':
        k = int(t[1])
        for i in range(20):
            if k & pow2[i]:
                k ^= pow2[i]
                u = parent[f(i, u)]
            if not k:
                break
        ans = ans0[u]
        st.append(u)
    elif t[0] == '!':
        v = st.pop()
        u = st[-1]
        if u < v:
            (x, cx) = st0.pop()
            if x ^ -1:
                c[x] = cx
        ans = ans0[u]
    else:
        ans = ans0[u]
        os.write(1, b'%d\n' % ans)

Solution (Python):
import sys, os
input = sys.stdin.buffer.readline

def f(u, v):
    return u * l + v

def g(u, v):
    return u + c0[v]
q = int(input())
(u, m) = (0, 0)
st = [0]
st0 = []
l = pow(10, 6) + 5
parent = [0] * (20 * l)
c0 = [0] * (q + 1)
a = [0] * (q + 1)
pow2 = [1]
for _ in range(20):
    pow2.append(2 * pow2[-1])
c = [l] * l
q0 = []
ans0 = [0]
ans = 0
j = 0
for _ in range(q):
    t = list(input().rstrip().decode().split())
    if t[0] == '+':
        x = int(t[1])
        m += 1
        parent[j] = u
        j += 1
        a[m] = x
        st.append(m)
        for i in range(20):
            if not i < c0[u + 1] - c0[u]:
                break
            p = parent[g(i, u)]
            if p == -1:
                break
            parent[j] = p
            j += 1
            u = p
        c0[m + 1] = j
        if c[x] == l:
            st0.append(c[x])
            c[x] = m
            ans += 1
        else:
            u = m
            for i in range(19, -1, -1):
                if i < c0[u + 1] - c0[u] and parent[g(i, u)] >= c[x]:
                    u = parent[g(i, u)]
            if u ^ c[x]:
                st0.append(c[x])
                c[x] = m
                ans += 1
            else:
                st0.append(0)
        ans0.append(ans)
        u = m
    elif t[0] == '-':
        k = int(t[1])
        for i in range(20):
            if k & pow2[i]:
                k ^= pow2[i]
                u = parent[g(i, u)]
            if not k:
                break
        ans = ans0[u]
        st.append(u)
    elif t[0] == '!':
        v = st.pop()
        u = st[-1]
        if u < v:
            cx = st0.pop()
            if cx:
                c[a[v]] = cx
        ans = ans0[u]
    else:
        ans = ans0[u]
        os.write(1, b'%d\n' % ans)

Solution (Python):
import sys, os
input = sys.stdin.buffer.readline

def g(u, v):
    (x, y) = (u + c0[v] >> 1, u + c0[v] & 1)
    return parent[x] & z if not y else parent[x] >> 20
q = int(input())
(u, m) = (0, 0)
st = [0]
st0 = []
l = pow(10, 6) + 5
parent = [0] * (10 * l)
c0 = [0] * (q + 1)
a = [0] * (q + 1)
pow2 = [1]
for _ in range(19):
    pow2.append(2 * pow2[-1])
z = sum(pow2)
c = [l] * l
ans0 = [0]
ans = 0
j = 0
for _ in range(q):
    t = list(input().rstrip().decode().split())
    if t[0] == '+':
        x = int(t[1])
        m += 1
        parent[j >> 1] ^= u if not j & 1 else u << 20
        j += 1
        a[m] = x
        st.append(m)
        for i in range(20):
            if not i < c0[u + 1] - c0[u]:
                break
            p = g(i, u)
            parent[j >> 1] ^= p if not j & 1 else p << 20
            j += 1
            u = p
        c0[m + 1] = j
        if c[x] == l:
            st0.append(c[x])
            c[x] = m
            ans += 1
        else:
            u = m
            for i in range(19, -1, -1):
                if i < c0[u + 1] - c0[u] and g(i, u) >= c[x]:
                    u = g(i, u)
            if u ^ c[x]:
                st0.append(c[x])
                c[x] = m
                ans += 1
            else:
                st0.append(0)
        ans0.append(ans)
        u = m
    elif t[0] == '-':
        k = int(t[1])
        for i in range(20):
            if k & pow2[i]:
                k ^= pow2[i]
                u = g(i, u)
            if not k:
                break
        ans = ans0[u]
        st.append(u)
    elif t[0] == '!':
        v = st.pop()
        u = st[-1]
        if u < v:
            cx = st0.pop()
            if cx:
                c[a[v]] = cx
        ans = ans0[u]
    else:
        ans = ans0[u]
        os.write(1, b'%d\n' % ans)