Description
Submission
class Solution {
using pii = pair<int, int>;
public:
vector<int> gridIllumination(int n, vector<vector<int>>& lamps, vector<vector<int>>& queries) {
int m = queries.size();
vector<int> rets(m);
unordered_map<int, int> row, column, diagonal, antidiagonal;
auto hashPair = [](const pii& p) -> size_t {
static hash<long long> hash_ll;
return hash_ll(p.first + (static_cast<long long>(p.second) << 32));
};
unordered_set<pii, decltype(hashPair)> points(0, hashPair);
for(auto& lamp: lamps) {
int x = lamp[0], y = lamp[1];
if(points.count({x, y}) > 0) continue;
row[x]++;
column[y]++;
diagonal[x-y]++;
antidiagonal[x+y]++;
points.insert({x, y});
}
for(int i = 0; i < m; ++i) {
int x = queries[i][0], y = queries[i][1];
if(row.count(x) > 0 && row[x] > 0) {
rets[i] = 1;
} else if(column.count(y) > 0 && column[y] > 0) {
rets[i] = 1;
} else if(diagonal.count(x-y) > 0 && diagonal[x-y] > 0) {
rets[i] = 1;
} else if(antidiagonal.count(x+y) > 0 && antidiagonal[x+y] > 0) {
rets[i] = 1;
}
for(int j = -1; j <= 1; ++j) {
for(int k = -1; k <= 1; ++k) {
int x2 = x + j, y2 = y + k;
if(x2 < 0 || y2 < 0 || x2 >= n || y2 >= n) continue;
auto it = points.find({x2, y2});
if(it != points.end()) {
points.erase(it);
row[x2]--;
column[y2]--;
diagonal[x2-y2]--;
antidiagonal[x2+y2]--;
}
}
}
}
return rets;
}
};
// 0 1 2 3 4 5
// 1
// 2
// 3
// 4