Little kids, Jack and Evan like playing their favorite game Glass-and-Stone. Today they want to play something new and came across Twitter on their father's laptop.

They saw it for the first time but were already getting bored to see a bunch of sentences having at most 140 characters each. The only thing they liked to play with it is, closing and opening tweets.

There are N tweets on the page and each tweet can be opened by clicking on it, to see some statistics related to that tweet. Initially all the tweets are closed. Clicking on an open tweet closes it and clicking on a closed tweet opens it. There is also a button to close all the open tweets. Given a sequence of K clicks by Jack, Evan has to guess the total number of open tweets just after each click. Please help Evan in this game.

------ Input ------ 

First line contains two integers N K, the number of tweets (numbered 1 to N) and the number of clicks respectively (1 ≤ N, K ≤ 1000). Each of the following K lines has one of the following.
CLICK X , where X is the tweet number (1 ≤ X ≤ N)
CLOSEALL

------ Output ------ 

Output K lines, where the i^{th} line should contain the number of open tweets just after the i^{th} click.

----- Sample Input 1 ------ 
3 6
CLICK 1
CLICK 2
CLICK 3
CLICK 2
CLOSEALL
CLICK 1
----- Sample Output 1 ------ 
1
2
3
2
0
1
----- explanation 1 ------ 
Let open[x] = 1 if the xth tweet is open and 0 if its closed.
Initially open[1..3] = { 0 , 0 , 0 }. Here is the state of open[1..3] after each click and corresponding count of open tweets.
CLICK 1 : { 1, 0, 0 }, open count = 1
CLICK 2 : { 1, 1, 0 }, open count = 2
CLICK 3 : { 1, 1, 1 }, open count = 3
CLICK 2 : { 1, 0, 1 }, open count = 2
CLOSEALL : { 0, 0, 0 }, open count = 0
CLICK 1 : { 1, 0, 0 }, open count = 1

Difficulty: EASY
Tags: Data structures

Implementation

Complete search
Skill types: Data structures

Complete search

Starter code:

Solutions:
(n, k) = map(int, input().split())
arr = []
for i in range(k):
	new = [j for j in input().split()]
	arr.append(new)
stack = []
cnt = 0
for i in range(len(arr)):
	if arr[i] == ['CLOSEALL']:
		cnt = 0
		stack.clear()
		print(cnt)
	elif arr[i] not in stack:
		cnt += 1
		stack.append(arr[i])
		print(cnt)
	elif arr[i] in stack:
		cnt -= 1
		stack.remove(arr[i])
		print(cnt)


(a, b) = map(int, input().split())
list1 = [0 for x in range(a)]
opent = 0
for i in range(b):
	st1 = input()
	if st1 != 'CLOSEALL':
		c = int(st1[6:])
	if 'CLICK' in st1:
		if list1[c - 1] == 0:
			list1[c - 1] += 1
			opent += 1
			print(opent)
		else:
			list1[c - 1] -= 1
			opent -= 1
			print(opent)
	else:
		list1 = [0 for x in range(a)]
		opent = 0
		print(0)


(n, k) = map(int, input().split())
a = [0] * n
for i in range(k):
	l = [str(i) for i in input().split()]
	f1 = l[0]
	try:
		f2 = int(l[1]) - 1
	except:
		pass
	if f1 == 'CLICK':
		if a[f2] == 0:
			a[f2] = 1
		else:
			a[f2] = 0
	else:
		a = [0] * n
	print(a.count(1))


(n, k) = input().split()
states = []
for y in range(0, int(n)):
	states.append(0)
for x in range(0, int(k)):
	s = input().split()
	if s[0] == 'CLICK':
		if states[int(s[1]) - 1] == 0:
			states[int(s[1]) - 1] = 1
		else:
			states[int(s[1]) - 1] = 0
	if s[0] == 'CLOSEALL':
		for z in range(0, int(n)):
			states[z] = 0
	print(sum(states))


(n, k) = map(int, input().split())
l = []
for i in range(k):
	s = input().split()
	if len(s) == 2:
		s1 = s[0]
		s2 = s[1]
	else:
		s1 = s[0]
	if s1 == 'CLICK':
		if s2 not in l:
			l.append(s2)
		else:
			l.remove(s2)
	elif s1 == 'CLOSEALL':
		l.clear()
	print(len(l))


from collections import defaultdict

def solve(n, k):
	mp = defaultdict(bool)
	tot = 0
	for _ in range(k):
		a = input()
		if a[2] == 'I':
			(_, t) = a.split()
			mp[t] = not mp[t]
			if mp[t]:
				tot += 1
			else:
				tot = max(0, tot - 1)
		else:
			tot = 0
			mp = defaultdict(bool)
		print(tot)
(n, k) = map(int, input().split())
solve(n, k)


(tweets, n) = map(int, input().split())
l = list((0 for _ in range(n)))
for _ in range(n):
	s = input()
	s = s.split()
	k = 0
	if s[0] != 'CLOSEALL':
		k = int(s[-1]) - 1
		if l[k] == 0:
			l[k] = 1
		else:
			l[k] = 0
	else:
		l = list((0 for _ in range(n)))
	print(l.count(1))


(n, k) = map(int, input().split())
twt = [0 for i in range(n)]
for click in range(k):
	d = input()
	if d == 'CLOSEALL':
		twt = [0 for i in range(n)]
	else:
		d = d.split()
		idx = int(d[1]) - 1
		if twt[idx] == 0:
			twt[idx] = 1
		else:
			twt[idx] = 0
	print(twt.count(1))


(n, k) = map(int, input().split())
l = []
s1 = set()
for i in range(k):
	s = input()
	words = s.split()
	if len(words) == 2:
		word = words[1]
		val = int(word)
		l.append(val)
	else:
		l.append(-1)
ans = []
for j in range(len(l)):
	if l[j] == -1:
		s1.clear()
		ans.append(len(s1))
	elif l[j] not in s1:
		s1.add(l[j])
		ans.append(len(s1))
	else:
		s1.remove(l[j])
		ans.append(len(s1))
for j in ans:
	print(j)


(tweets, clicks) = map(int, input().split())
c = 0
l = []
for i in range(clicks):
	s = input()
	if s == 'CLOSEALL':
		l.clear()
		c = 0
	elif s not in l:
		l.append(s)
		c += 1
	else:
		ind = l.index(s)
		del l[ind]
		c -= 1
	print(c)


(n, k) = list(map(int, input().split()))
tweets = ['C' for _ in range(k)]
opened = 0
R = []
for _ in range(k):
	click = input()
	if click == 'CLOSEALL':
		tweets = ['C' for _ in range(k)]
		opened = 0
	else:
		no = int(click.split()[1])
		if tweets[no - 1] == 'C':
			tweets[no - 1] = 'O'
			opened += 1
		else:
			tweets[no - 1] = 'C'
			opened -= 1
	R.append(opened)
for r in R:
	print(r)


(n, k) = map(int, input().split())
clicks = {}
for i in range(k):
	s = input()
	c = ''
	if s.startswith('CLI'):
		(w, c) = s.split()
		if c in clicks:
			if clicks[c] == 1:
				clicks[c] = 0
			else:
				clicks[c] = 1
		else:
			clicks[c] = 1
	else:
		clicks.clear()
	print(sum(clicks.values()))


(n, k) = [int(i) for i in input().split()]
pl = []
for i in range(k):
	ma = input().split()
	if len(ma) == 1:
		print(0)
		pl.clear()
	else:
		if ma[-1] not in pl:
			pl.append(ma[-1])
		else:
			pl.remove(ma[-1])
		print(len(pl))


(n, k) = map(int, input().split())
s = n * [0]
for i in range(k):
	x = input()
	if x == 'CLOSEALL':
		s = n * [0]
	else:
		x = x.split()
		s1 = int(x[1]) - 1
		if s[s1] == 0:
			s[s1] = 1
		else:
			s[s1] = 0
	print(s.count(1))


(n, k) = map(int, input().split())
c = 0
newlst = []
for i in range(k):
	lst = list(input().split())
	if lst[0] == 'CLICK':
		if lst[1] in newlst:
			c -= 1
			newlst.remove(lst[1])
		else:
			newlst.append(lst[1])
			c += 1
	else:
		c = 0
		newlst = []
	print(c)


(n, k) = map(int, input().split())
l = []
ans = []
l2 = [0] * n
for i in range(k):
	l.append(input())
for i in l:
	if 'ALL' in i:
		print(0)
		l2 = [0] * n
	else:
		i = i.split()
		if l2[int(i[-1]) - 1] == 0:
			l2[int(i[-1]) - 1] = 1
			print(l2.count(1))
		else:
			l2[int(i[-1]) - 1] = 0
			print(l2.count(1))


(a, b) = map(int, input().split())
n = []
for i in range(b):
	m = input().split()
	if len(m) == 1:
		n = []
	elif m[1] in n:
		n.remove(m[1])
	else:
		n.append(m[1])
	print(len(n))


(x, y) = map(int, input().split())
l = []
for i in range(y):
	a = input()
	if a == 'CLOSEALL':
		l = []
		print(len(l))
	elif a in l:
		l.remove(a)
		print(len(l))
	else:
		l.append(a)
		print(len(l))


(n, k) = list(map(int, input().split()))
list1 = []
count = 0
for i in range(k):
	arr = list(input().split())
	if arr[0] == 'CLICK':
		if arr[1] not in list1:
			list1.append(arr[1])
			count = count + 1
		else:
			count = count - 1
			list1.remove(arr[1])
	else:
		list1 = []
		count = 0
	print(count)


(tweets, clicks) = map(int, input().split())
l = []
for i in range(clicks):
	a = input()
	if a == 'CLOSEALL':
		print(0)
		l.clear()
	else:
		if a in l:
			l.remove(a)
		else:
			l.append(a)
		print(len(l))


(N, K) = list(map(int, input().split(' ')))
l = [0] * N
for i in range(K):
	s = list(map(str, input().split(' ')))
	if s[0] != 'CLOSEALL':
		if l[int(s[1]) - 1]:
			l[int(s[1]) - 1] = 0
		else:
			l[int(s[1]) - 1] = 1
	else:
		l = [0] * N
	print(l.count(1))


(n, k) = map(int, input().split())
lis = []
for i in range(k):
	s = input().split()
	if s[-1] == 'CLOSEALL':
		lis.clear()
		print(0)
	else:
		if s in lis:
			lis.remove(s)
		else:
			lis.append(s)
		print(len(lis))


from collections import Counter
(n, k) = map(int, input().split())
arr = [str(input()) for xx in range(k)]
count = 0
ref = [0] * k
for i in range(k):
	if arr[i] == 'CLOSEALL':
		count = 0
		ref = [0] * k
	elif arr[i] in ref:
		count -= 1
		ref.remove(arr[i])
	else:
		ref.append(arr[i])
		count += 1
	print(count)


li = [0]

def check(b):
	if li[b - 1] == 0:
		li[b - 1] = 1
		return li.count(1)
	else:
		li[b - 1] = 0
		return li.count(1)
(n, k) = map(int, input().split())
li = li * n
while k:
	string = input()
	if string == 'CLOSEALL':
		print(0)
		li = [0] * n
	else:
		(a, b) = string.split(' ')
		print(check(int(b)))
	k -= 1


n_k = [int(x) for x in input().split()]
a = [0] * n_k[0]
opened = 0
for _ in range(n_k[1]):
	cmd = list(input().split())
	if cmd[0] == 'CLICK':
		if a[int(cmd[1]) - 1] == 0:
			a[int(cmd[1]) - 1] = 1
			opened = opened + 1
		else:
			a[int(cmd[1]) - 1] = 0
			opened = opened - 1
	else:
		a = [0] * n_k[0]
		opened = 0
	print(opened)


(n, k) = map(int, input().split())
dict = {}
sum = 0
for i in range(k):
	n = 0
	ar = list(map(str, input().split()))
	if len(ar) == 1:
		sum = 0
		dict.clear()
	else:
		n = int(ar[1])
		if n in dict.keys():
			sum -= 1
			dict.pop(n)
		else:
			dict[n] = 0
			sum += 1
	print(sum)


(n, k) = map(int, input().split())
c = 0
l = []
for i in range(k):
	x = input()
	ls = list(x.split())
	if x == 'CLOSEALL':
		print(0)
		l.clear()
		c = 0
	elif ls[1] not in l:
		c += 1
		l.append(ls[1])
		print(c)
	else:
		c -= 1
		l.remove(ls[1])
		print(c)


(t, o) = map(int, input().split())
ts = [0 for _ in range(t)]
for _ in range(o):
	l = input().split()
	if len(l) == 1:
		print(0)
		ts = [0] * t
	if len(l) > 1:
		if ts[int(l[1]) - 1] == 1:
			ts[int(l[1]) - 1] = 0
			print(ts.count(1))
		else:
			ts[int(l[1]) - 1] = 1
			print(ts.count(1))


(n, k) = map(int, input().split())
t = []
for i in range(k):
	o = input()
	if o == 'CLOSEALL':
		t.clear()
	elif o in t:
		t.remove(o)
	else:
		t.append(o)
	print(len(t))


(n, k) = map(int, input().split())
l = []
for i in range(k):
	s = input()
	if s == 'CLOSEALL':
		print(0)
		l = []
	elif s in l:
		l.remove(s)
		print(len(l))
	else:
		l.append(s)
		print(len(l))


(n, k) = map(int, input().split())
clicks = []
for i in range(k):
	clicks.append(input())
container = []
cnt = 0
for i in range(len(clicks)):
	if clicks[i] == 'CLOSEALL':
		cnt = 0
		container.clear()
		print(cnt)
	elif clicks[i] not in container:
		cnt += 1
		container.append(clicks[i])
		print(cnt)
	elif clicks[i] in container:
		cnt -= 1
		container.remove(clicks[i])
		print(cnt)


(n, k) = map(int, input().split())
status = [0] * n
status_total = 0
for _ in range(k):
	s = input()
	if s == 'CLOSEALL':
		status = [0] * n
		status_total = 0
	else:
		s = s.split()
		s = int(s[1]) - 1
		if status[s]:
			status_total -= 1
		else:
			status_total += 1
		status[s] = 1 - status[s]
	print(status_total)


(n, k) = map(int, input().split())
opened = [0] * 1001
for i in range(k):
	cmd = list(input().split())
	if cmd[0] == 'CLICK':
		opened[int(cmd[1])] = 1 - opened[int(cmd[1])]
	else:
		opened = [0] * 1001
	print(sum(opened))


(n, k) = map(int, input().split(' '))
dict1 = {}
open = 0
for i in range(k):
	j = input()
	if j == 'CLOSEALL':
		open = 0
		dict1 = dict.fromkeys(dict1, 0)
	else:
		click = int(j.split(' ')[1])
		if click in dict1:
			dict1[click] = dict1[click] + 1
			if dict1[click] % 2 != 0:
				open += 1
			else:
				open -= 1
		else:
			dict1[click] = 1
			open += 1
	print(open)


(n, k) = list(map(int, input().split()))
l = n * [0]
for i in range(k):
	s = input()
	if s == 'CLOSEALL':
		l = n * [0]
		print(sum(l))
	else:
		s = s.split()
		s1 = int(s[1]) - 1
		if l[s1] == 0:
			l[s1] = 1
		else:
			l[s1] = 0
		print(sum(l))


(n, k) = map(int, input().split())

def fill():
	return [False for i in range(n)]
open_tweet = 0
arr = fill()
for j in range(k):
	s = input()
	if s == 'CLOSEALL':
		arr = fill()
		open_tweet = 0
	else:
		(s, i) = s.split()
		i = int(i) - 1
		arr[i] = not arr[i]
		if arr[i]:
			open_tweet += 1
		else:
			open_tweet -= 1
	print(open_tweet)


(n, k) = map(int, input().split())
a = []
for _ in range(k):
	s = input()
	if s == 'CLOSEALL':
		a.clear()
		print(0)
	else:
		if s in a:
			a.remove(s)
		else:
			a.append(s)
		print(len(a))


(N, K) = map(int, input().split(' '))
l = []
for i in range(N):
	l.append(0)
for i in range(K):
	s = input()
	S = s.split()
	if len(S) == 1:
		for j in range(N):
			l[j] = 0
	else:
		k = int(S[1])
		if l[k - 1] == 1:
			l[k - 1] -= 1
		else:
			l[k - 1] += 1
	print(l.count(1))


(n, k) = [int(d) for d in input().split()]
final = []
for i in range(k):
	a = input().split(' ')
	if len(a) > 1:
		if int(a[1]) not in final:
			final.append(int(a[1]))
		else:
			final.remove(int(a[1]))
		print(len(final))
	else:
		final.clear()
		print(0)


(n, k) = map(int, input().split())
l = [0] * n
op = 0
for i in range(k):
	s = input()
	p = s.split()
	if len(p) == 2:
		cmd = p[0]
		i = int(p[1])
	else:
		cmd = p[0]
	if cmd == 'CLICK':
		if l[i - 1]:
			l[i - 1] = 0
			op = op - 1
		else:
			l[i - 1] = 1
			op = op + 1
		print(op)
	else:
		l = [0] * n
		op = 0
		print(op)


(n, k) = list(map(int, input().split()))
d = dict()
for i in range(k):
	l = input().strip().split()
	if l[0] == 'CLOSEALL':
		d = dict()
	elif l[1] in d:
		del d[l[1]]
	else:
		d[l[1]] = 1
	print(len(d))


(n, k) = map(int, input().split())
lst = [0 for _ in range(0, n)]
for j in range(0, k):
	a = list(map(str, input().split()))
	if a[0] == 'CLICK':
		idx = int(a[1]) - 1
		if lst[idx] == 0:
			lst[idx] = 1
		else:
			lst[idx] = 0
		print(lst.count(1))
	else:
		lst = [0 for _ in range(0, n)]
		print(lst.count(1))


(n, k) = map(int, input().split())
res = 0
temp1 = [False] * (n + 1)
for _ in range(k):
	temp = input().split(' ')
	if temp[0] == 'CLICK':
		if temp1[int(temp[1])] == False:
			res += 1
			temp1[int(temp[1])] = True
			print(res)
		elif temp1[int(temp[1])] == True:
			res -= 1
			temp1[int(temp[1])] = False
			print(res)
	elif temp[0] == 'CLOSEALL':
		res = 0
		for i in range(n + 1):
			temp1[i] = False
		print(res)


(n, k) = map(int, input().split())
temp = 0
for _ in range(k):
	operation = input().split()
	if len(operation) == 2:
		tweet_no = int(operation[-1])
		temp ^= 1 << tweet_no
	else:
		temp = 0
	print(bin(temp).count('1'))


(n, c) = map(int, input().split(' '))
l = []
for _ in range(c):
	a = input()
	if a == 'CLOSEALL':
		l = []
		print(len(l))
	elif a in l:
		l.remove(a)
		print(len(l))
	else:
		l.append(a)
		print(len(l))


(n, k) = map(int, input().split())
tweets = [False] * n
num_open = 0
for i in range(k):
	cmd = input().split()
	if cmd[0] == 'CLICK':
		tweet_id = int(cmd[1]) - 1
		if tweets[tweet_id]:
			tweets[tweet_id] = False
			num_open -= 1
		else:
			tweets[tweet_id] = True
			num_open += 1
	else:
		tweets = [False] * n
		num_open = 0
	print(num_open)


(N, K) = map(int, input().split())
P = []
for i in range(K):
	S = input()
	if S == 'CLOSEALL':
		P = []
		print(len(P))
	elif S in P:
		P.remove(S)
		print(len(P))
	else:
		P.append(S)
		print(len(P))


(n, k) = map(int, input().split())
l = [0] * 1001
for _ in range(k):
	l1 = list(input().split())
	if l1[0] == 'CLICK':
		l[int(l1[1])] = 1 - l[int(l1[1])]
	else:
		l = [0] * 1001
	print(sum(l))


(N, K) = map(int, input().split())
dct = dict(zip(range(1, N + 1), [0] * N))
for _ in range(K):
	S = input()
	if ' ' in S:
		(S, num) = S.split()
		num = int(num)
	if S[2] == 'I':
		dct[num] = 1 - dct[num]
	else:
		dct = dict(zip(range(1, N + 1), [0] * N))
	print(sum(dct.values()))


(n, k) = map(int, input().split())
array = [0] * n
for i in range(k):
	c = input().split()
	sum = 0
	if len(c) != 2:
		array = [0] * n
		print(0)
	else:
		click = int(c[1])
		if array[click - 1] == 1:
			array[click - 1] = 0
		else:
			array[click - 1] = 1
		for f in array:
			sum = sum + f
		print(sum)


(tweet, op) = map(int, input().split())
tweet_status = [0 for _ in range(tweet)]
for _ in range(op):
	l = input().split()
	if len(l) == 1:
		print(0)
		tweet_status = [0] * tweet
	if len(l) > 1:
		if tweet_status[int(l[1]) - 1] == 1:
			tweet_status[int(l[1]) - 1] = 0
			print(tweet_status.count(1))
		else:
			tweet_status[int(l[1]) - 1] = 1
			print(tweet_status.count(1))


(N, K) = [int(x) for x in input().split()]
arr = [0] * N
for i in range(K):
	e = input()
	if e != 'CLOSEALL':
		(com, pos) = [x for x in e.split()]
		if arr[int(pos) - 1] == 1:
			arr[int(pos) - 1] = 0
		else:
			arr[int(pos) - 1] = 1
	else:
		arr = [0] * N
	print(arr.count(1))


(n, k) = map(int, input().split())
value = list((0 for i in range(n)))
ans = 0
for a in range(k):
	b = input()
	if b == 'CLOSEALL':
		value = list((0 for i in range(n)))
		ans = 0
		print(ans)
	else:
		(d, e) = b.split()
		e = int(e)
		if value[e - 1] == 1:
			value[e - 1] = 0
			ans += -1
		else:
			value[e - 1] = 1
			ans += 1
		print(ans)


(n, k) = map(int, input().split())
arr = []
for e in range(k):
	s = input()
	if s == 'CLOSEALL':
		arr.clear()
		print(0)
	else:
		if s in arr:
			arr.remove(s)
		else:
			arr.append(s)
		print(len(arr))


(a, b) = map(int, input().split())
b1 = [0] * a
count = 0
for i in range(b):
	a1 = input().split()
	if a1[0] == 'CLOSEALL':
		print('0')
		b1 = [0] * a
		count = 0
	else:
		A = int(a1[1]) - 1
		b1[A] = b1[A] + 1
		if b1[A] % 2 == 0:
			count -= 1
			print(count)
		else:
			count += 1
			print(count)


(a, b) = map(int, input().split())
m = 0
l = []
for i in range(b):
	x = list(map(str, input().split()))
	if x[0] == 'CLOSEALL':
		l = []
		m = 0
		print(m)
	else:
		l.append(x[0] + x[1])
		if l.count(l[-1]) % 2 != 0:
			m += 1
			print(m)
		else:
			m -= 1
			print(m)


(n, k) = map(int, input().split())
count = 0
l = []
for i in range(k):
	s = input()
	if s not in l and s != 'CLOSEALL':
		l.append(s)
		count += 1
		print(count)
	elif s == 'CLOSEALL':
		l.clear()
		print('0')
		count = 0
	else:
		l.remove(s)
		count -= 1
		print(count)


(n, k) = map(int, input().split())
l = []
for i in range(k):
	s = input().split()
	if len(s) == 1:
		print(0)
		l.clear()
	else:
		if s[-1] not in l:
			l.append(s[-1])
		else:
			l.remove(s[-1])
		print(len(l))