-
Notifications
You must be signed in to change notification settings - Fork 0
/
FrequencyQueries.py
48 lines (38 loc) · 1.05 KB
/
FrequencyQueries.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
#!/bin/python3
import math
import os
import random
import re
import sys
from collections import defaultdict
# Complete the freqQuery function below.
def freqQuery(queries):
d = defaultdict(lambda: 0)
f = defaultdict(lambda: 0)
result = []
for command, value in queries:
if command == 1:
f[d[value]] = max(0, f[d[value]] - 1)
d[value] += 1
f[d[value]] += 1
elif command == 2:
f[d[value]] = max(0, f[d[value]] - 1)
d[value] = max(0, d[value] - 1)
if d[value] > 0:
f[d[value]] += 1
else:
if f[value] > 0:
result.append(1)
else:
result.append(0)
return result
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
q = int(input().strip())
queries = []
for _ in range(q):
queries.append(list(map(int, input().rstrip().split())))
ans = freqQuery(queries)
fptr.write('\n'.join(map(str, ans)))
fptr.write('\n')
fptr.close()