Thor (Codeforces 705C)
Tags: Implement, brute force

Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).q events are about to happen (in chronological order). They are of three types:  Application x generates a notification (this new notification is unread).  Thor reads all notifications generated so far by application x (he may re-read some notifications).  Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation. Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.

Input: The first line of input contains two integers n and q (1 ≤ n, q ≤ 300 000) — the number of applications and the number of events to happen.The next q lines contain the events. The i-th of these lines starts with an integer typei — type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 ≤ typei ≤ 3, 1 ≤ xi ≤ n, 1 ≤ ti ≤ q).

Output: Print the number of unread notifications after each event.

Note: In the first sample:  Application 3 generates a notification (there is 1 unread notification).  Application 1 generates a notification (there are 2 unread notifications).  Application 2 generates a notification (there are 3 unread notifications).  Thor reads the notification generated by application 3, there are 2 unread notifications left. In the second sample test:  Application 2 generates a notification (there is 1 unread notification).  Application 4 generates a notification (there are 2 unread notifications).  Application 2 generates a notification (there are 3 unread notifications).  Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.  Application 3 generates a notification (there is 1 unread notification).  Application 3 generates a notification (there are 2 unread notifications). 

Sample Inputs:
3 4
1 3
1 1
1 2
2 3

4 6
1 2
1 4
1 2
3 3
1 3
1 3

Sample Outputs:
1
2
3
2

1
2
3
0
1
2

Solution (Python):
(n, q) = map(int, input().split())
a = [[0] * (n + 1) for i in range(2)]
b = []
n1 = 0
n2 = 0
n3 = 0
k = ''
for i in range(q):
    (op, x) = map(int, input().split())
    if op == 1:
        a[0][x] += 1
        b += [x]
        n1 += 1
        n2 += 1
    elif op == 3:
        for i in range(n3, x):
            if i + 1 > a[1][b[i]]:
                a[0][b[i]] -= 1
                n1 -= 1
            n3 = max(n3, x)
    else:
        n1 -= a[0][x]
        a[0][x] = 0
        a[1][x] = n2
    k += str(n1) + '\n'
print(k)

Solution (Python):
import random, math, sys
from copy import deepcopy as dc
from bisect import bisect_left, bisect_right
from collections import Counter, OrderedDict
input = sys.stdin.readline

def solution(n, quer):
    total_cell = [0] * n
    max_2 = [0] * n
    max_3 = 0
    message = []
    total = 0
    time = 0
    ans = ''
    for i in quer:
        (t, x_t) = (i[0], i[1])
        if t == 1:
            total_cell[x_t - 1] += 1
            message.append(x_t)
            total += 1
        elif t == 2:
            total -= total_cell[x_t - 1]
            total_cell[x_t - 1] = 0
            max_2[x_t - 1] = len(message)
        else:
            for j in range(max_3, x_t):
                if j >= max_2[message[j] - 1]:
                    total_cell[message[j] - 1] -= 1
                    total -= 1
            max_3 = max(max_3, x_t)
        ans += str(total) + '\n'
    print(ans)

def input_test():
    (n, q) = map(int, input().strip().split(' '))
    quer = []
    for i in range(q):
        li = list(map(int, input().strip().split(' ')))
        li.append(True)
        quer.append(li)
    out = solution(n, quer)

def test():
    pass
input_test()

Solution (Python):
import sys
from collections import defaultdict

def main():
    (n, q) = list(map(int, sys.stdin.readline().split()))
    al = 0
    d = defaultdict(list)
    sts = defaultdict(int)
    cur = 0
    msgs = []
    ans = []
    start = 0
    for i in range(q):
        (t, x) = list(map(int, sys.stdin.readline().split()))
        if t == 1:
            al += 1
            d[x].append(al)
            msgs.append(x)
            cur += 1
        elif t == 2:
            size = len(d[x])
            if size > 0:
                cur -= size - sts[x]
                d[x] = list()
                sts[x] = 0
        elif start < x:
            for i in range(start, x):
                v = msgs[i]
                dd = d[v]
                size = len(dd)
                cur_st = sts[v]
                while size > cur_st and dd[cur_st] <= x:
                    cur_st += 1
                    cur -= 1
                sts[v] = cur_st
            start = x
        ans.append(cur)
    print('\n'.join(map(str, ans)))
main()