-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.cpp
41 lines (41 loc) · 1.34 KB
/
Solution.cpp
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
class Solution {
public:
vector<int> gridIllumination(int n, vector<vector<int>>& lamps, vector<vector<int>>& queries) {
auto f = [&](int i, int j) -> long long {
return (long long) i * n + j;
};
unordered_set<long long> s;
unordered_map<int, int> row, col, diag1, diag2;
for (auto& lamp : lamps) {
int i = lamp[0], j = lamp[1];
if (!s.count(f(i, j))) {
s.insert(f(i, j));
row[i]++;
col[j]++;
diag1[i - j]++;
diag2[i + j]++;
}
}
int m = queries.size();
vector<int> ans(m);
for (int k = 0; k < m; ++k) {
int i = queries[k][0], j = queries[k][1];
if (row[i] > 0 || col[j] > 0 || diag1[i - j] > 0 || diag2[i + j] > 0) {
ans[k] = 1;
}
for (int x = i - 1; x <= i + 1; ++x) {
for (int y = j - 1; y <= j + 1; ++y) {
if (x < 0 || x >= n || y < 0 || y >= n || !s.count(f(x, y))) {
continue;
}
s.erase(f(x, y));
row[x]--;
col[y]--;
diag1[x - y]--;
diag2[x + y]--;
}
}
}
return ans;
}
};