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
|
import java.util.*;
public class BinaryMatrix3D {
private int n, m, l;
private Set<Pair<Integer, Integer>>[] layers;
public BinaryMatrix3D(int n, int m, int l) {
this.n = n; this.m = m; this.l = l;
layers = new HashSet[l];
for (int i = 0; i < l; i++) layers[i] = new HashSet<>();
}
public void set(int x, int y, int z) {
layers[z].add(new Pair<>(x, y));
}
public void unset(int x, int y, int z) {
layers[z].remove(new Pair<>(x, y));
}
public boolean isSet(int x, int y, int z) {
return layers[z].contains(new Pair<>(x, y));
}
public List<int[]> getLayer(int z) {
List<int[]> ans = new ArrayList<>();
for (Pair<Integer, Integer> p : layers[z]) {
ans.add(new int[]{p.getKey(), p.getValue()});
}
return ans;
}
private static class Pair<K, V> {
private final K key;
private final V value;
public Pair(K key, V value) { this.key = key; this.value = value; }
public K getKey() { return key; }
public V getValue() { return value; }
@Override public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) o;
return Objects.equals(key, pair.key) && Objects.equals(value, pair.value);
}
@Override public int hashCode() {
return Objects.hash(key, value);
}
}
}
|