#include <bits/stdc++.h>
#define int long long
using namespace std;
namespace std {
template <typename T> ostream &operator<<(ostream &out, const vector<T> &vec) {
out << "[";
for (int i = 0; i < (int)vec.size(); ++i) {
out << vec[i];
if (i + 1 < (int)vec.size())
out << ", ";
}
return out << "]";
}
} // namespace std
void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
cout << ' ' << H;
dbg_out(T...);
}
#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
signed main(void) {
ios_base::sync_with_stdio(false);
cin.tie(0);
int nbLig, nbCol;
cin >> nbLig >> nbCol;
vector<int> onDiag(nbLig + nbCol - 1);
vector<vector<bool>> isUseful(nbLig, vector<bool>(nbCol, true));
vector<vector<int>> cntReach(nbLig, vector<int>(nbCol));
vector<vector<int>> cntReachRev(nbLig, vector<int>(nbCol));
for (int lig = 0; lig < nbLig; ++lig)
for (int col = 0; col < nbCol; ++col) {
if (lig + 1 < nbLig)
cntReach[lig + 1][col]++, cntReachRev[lig][col]++;
if (col + 1 < nbCol)
cntReach[lig][col + 1]++, cntReachRev[lig][col]++;
onDiag[lig + col] += 1;
}
auto tryDisable = [&](int lig, int col) {
if (!isUseful[lig][col])
return true;
if (onDiag[lig + col] == 1)
return false;
queue<pair<int, int>> q;
q.emplace(lig, col);
while (!q.empty()) {
auto [r, c] = q.front();
q.pop();
assert(isUseful[r][c]);
isUseful[r][c] = false;
onDiag[r + c]--;
// try right
if (c + 1 < nbCol) {
cntReach[r][c + 1]--;
if (!cntReach[r][c + 1] and isUseful[r][c + 1])
q.emplace(r, c + 1);
}
// try bot
if (r + 1 < nbLig) {
cntReach[r + 1][c]--;
if (!cntReach[r + 1][c] and isUseful[r + 1][c])
q.emplace(r + 1, c);
}
// try left
if (c > 0) {
cntReachRev[r][c - 1]--;
if (!cntReachRev[r][c - 1] and isUseful[r][c - 1])
q.emplace(r, c - 1);
}
// try top
if (r > 0) {
cntReachRev[r - 1][c]--;
if (!cntReachRev[r - 1][c] and isUseful[r - 1][c])
q.emplace(r - 1, c);
}
}
return true;
};
for (int lig = 0; lig < nbLig; ++lig)
for (int col = 0; col < nbCol; ++col) {
int x;
cin >> x;
if (x)
assert(tryDisable(lig, col));
}
int q;
cin >> q;
for (int i = 0; i < q; ++i) {
for (int lig = 0; lig < nbLig; ++lig)
dbg(isUseful[lig]);
dbg(onDiag);
int lig, col;
cin >> lig >> col;
--lig, --col;
cout << tryDisable(lig, col) << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
3 ms |
468 KB |
Output is correct |
5 |
Correct |
4 ms |
468 KB |
Output is correct |
6 |
Correct |
3 ms |
468 KB |
Output is correct |
7 |
Correct |
3 ms |
468 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
3 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
3 ms |
468 KB |
Output is correct |
5 |
Correct |
4 ms |
468 KB |
Output is correct |
6 |
Correct |
3 ms |
468 KB |
Output is correct |
7 |
Correct |
3 ms |
468 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
3 ms |
468 KB |
Output is correct |
10 |
Correct |
11 ms |
1236 KB |
Output is correct |
11 |
Correct |
3 ms |
588 KB |
Output is correct |
12 |
Correct |
182 ms |
16020 KB |
Output is correct |
13 |
Correct |
79 ms |
16012 KB |
Output is correct |
14 |
Correct |
279 ms |
23848 KB |
Output is correct |
15 |
Correct |
274 ms |
23492 KB |
Output is correct |
16 |
Correct |
269 ms |
25520 KB |
Output is correct |
17 |
Correct |
279 ms |
26892 KB |
Output is correct |
18 |
Correct |
291 ms |
26208 KB |
Output is correct |
19 |
Correct |
291 ms |
27756 KB |
Output is correct |
20 |
Correct |
290 ms |
27692 KB |
Output is correct |
21 |
Correct |
301 ms |
27728 KB |
Output is correct |