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
|
class Solution {
public int[] gridIllumination(int n, int[][] lamps, int[][] queries) {
Map<Integer, Integer> row = new HashMap<>(), col = new HashMap<>(), diag = new HashMap<>(), anti = new HashMap<>();
Set<Long> s = new HashSet<>();
for (int[] l : lamps) {
int x = l[0], y = l[1];
long key = ((long)x << 32) | y;
if (s.contains(key)) continue;
s.add(key);
row.put(x, row.getOrDefault(x, 0) + 1);
col.put(y, col.getOrDefault(y, 0) + 1);
diag.put(x - y, diag.getOrDefault(x - y, 0) + 1);
anti.put(x + y, anti.getOrDefault(x + y, 0) + 1);
}
int[] ans = new int[queries.length];
int[][] dirs = {{0,0},{0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}};
for (int i = 0; i < queries.length; i++) {
int x = queries[i][0], y = queries[i][1];
if (row.getOrDefault(x,0) > 0 || col.getOrDefault(y,0) > 0 || diag.getOrDefault(x-y,0) > 0 || anti.getOrDefault(x+y,0) > 0) {
ans[i] = 1;
}
for (int[] d : dirs) {
int nx = x + d[0], ny = y + d[1];
long key = ((long)nx << 32) | ny;
if (s.contains(key)) {
s.remove(key);
row.put(nx, row.getOrDefault(nx,0)-1);
col.put(ny, col.getOrDefault(ny,0)-1);
diag.put(nx-ny, diag.getOrDefault(nx-ny,0)-1);
anti.put(nx+ny, anti.getOrDefault(nx+ny,0)-1);
}
}
}
return ans;
}
}
|