-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC_Balls-and-Bag-Query.py
66 lines (50 loc) · 1.38 KB
/
C_Balls-and-Bag-Query.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 入力
Q = int(input())
# 袋に入っている数字カウント
count = {}
# 袋に入っている数字の種類
number = 0
for i in range(Q):
query_type, *x = map(int, input().split())
if query_type == 1:
if x[0] in count:
if count[x[0]] == 0:
number += 1
count[x[0]] += 1
else:
count[x[0]] = 1
number += 1
elif query_type == 2:
count[x[0]] -= 1
if count[x[0]] == 0:
number -= 1
else:
print(number)
##### 別解 #####
# from collections import defaultdict
# def c_balls_and_bag_query():
# # 入力
# Q = int(input())
# Queries = [[int(col) for col in input().split()] for row in range(Q)]
# # ボールの数をカウントする辞書、デフォルト値は0
# bag = defaultdict(int)
# # 結果
# ans = []
# for q in Queries:
# type = q[0]
# match type:
# case 1:
# x = q[1]
# bag[x] += 1
# case 2:
# x = q[1]
# bag[x] -= 1
# if bag[x] == 0:
# del bag[x]
# case 3:
# ans.append(len(bag))
# case _:
# pass
# # ansを改行で結合
# return "\n".join(map(str, ans))
# print(c_balls_and_bag_query())