-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathFrequencyQueries.java
53 lines (45 loc) · 1.84 KB
/
FrequencyQueries.java
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
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
public class Solution {
// Complete the freqQuery function below.
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter out = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
List<Integer> result = new ArrayList<>();
int n = Integer.parseInt(in.readLine());
HashMap<Long, Long> map = new HashMap<Long, Long>();
HashMap<Long, Long> freq = new HashMap<Long, Long>();
for (int i = 0; i < n; i++) {
StringTokenizer s= new StringTokenizer(in.readLine());
int a = Integer.parseInt(s.nextToken());
final long b = Long.parseLong(s.nextToken());
switch (a) {
case 1:
freq.computeIfPresent(map.get(b), (k, v) -> v == 1 ? freq.remove(map.get(b)) : v - 1);
map.compute(b, (k, v) -> v == null ? 1 : v + 1);
freq.compute(map.get(b), (k, v) -> v == null ? 1 : v + 1);
break;
case 2:
freq.computeIfPresent(map.get(b), (k, v) -> v == 1 ? freq.remove(map.get(b)) : v - 1);
map.computeIfPresent(b, (k, v) -> v == 1 ? map.remove(b) : v - 1);
freq.compute(map.get(b), (k, v) -> v == null ? 1 : v + 1);
break;
case 3:
result.add(freq.containsKey(b) ? 1 : 0);
break;
}
}
out.write(result.stream().map(Object::toString).collect(joining("\n")) + "\n");
in.close();
out.close();
}
}