#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define all(x) x.begin(), x.end()
int main() {
ios::sync_with_stdio(false), cin.tie(0);
int n, m, q;
cin >> n >> m;
vector <vector <int>> a(n, vector <int> (m));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> a[i][j];
}
}
vector <vector <int>> dp1(n, vector <int> (m));
vector <vector <int>> dp2(n, vector <int> (m));
dp1[0][0] = 1, dp2.back().back() = 1;
for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) if (dp1[i][j]) {
if (i + 1 < n && !a[i + 1][j]) {
dp1[i + 1][j] = true;
}
if (j + 1 < m && !a[i][j + 1]) {
dp1[i][j + 1] = true;
}
}
for (int i = n - 1; ~i; --i) for (int j = m - 1; ~j; --j) if (dp2[i][j]) {
if (i && !a[i - 1][j]) {
dp2[i - 1][j] = true;
}
if (j && !a[i][j - 1]) {
dp2[i][j - 1] = true;
}
}
vector <vector <int>> ok(n, vector <int>(m));
vector <set <int>> cut(n + m);
for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) if (dp1[i][j] && dp2[i][j]) {
ok[i][j] = 1;
cut[i + j].insert(i);
}
vector <int> dx = {0, 0, 1, -1}, dy = {1, -1, 0, 0};
function <void(int, int)> dfs = [&](int x, int y) {
ok[x][y] = 0;
cut[x + y].erase(x);
for (int i = 0; i < 4; ++i) {
int nx = x + dx[i], ny = y + dy[i];
if (0 <= nx && nx < n && 0 <= ny && ny < m && ok[nx][ny]) {
int nnx = nx + dx[i ^ 3], nny = ny + dy[i ^ 3];
if (nnx < 0 || nnx >= n || nny < 0 || nny >= m || !ok[nnx][nny]) {
dfs(nx, ny);
}
}
}
};
cin >> q;
while (q--) {
int x, y;
cin >> x >> y, --x, --y;
if (!ok[x][y]) {
cout << "1\n";
dfs(x, y);
} else if (cut[x + y].size() == 1) {
cout << "0\n";
} else {
cout << "1\n";
dfs(x, y);
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
3 ms |
652 KB |
Output is correct |
4 |
Correct |
4 ms |
844 KB |
Output is correct |
5 |
Correct |
4 ms |
812 KB |
Output is correct |
6 |
Correct |
6 ms |
952 KB |
Output is correct |
7 |
Correct |
5 ms |
1076 KB |
Output is correct |
8 |
Correct |
5 ms |
952 KB |
Output is correct |
9 |
Correct |
5 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
3 ms |
652 KB |
Output is correct |
4 |
Correct |
4 ms |
844 KB |
Output is correct |
5 |
Correct |
4 ms |
812 KB |
Output is correct |
6 |
Correct |
6 ms |
952 KB |
Output is correct |
7 |
Correct |
5 ms |
1076 KB |
Output is correct |
8 |
Correct |
5 ms |
952 KB |
Output is correct |
9 |
Correct |
5 ms |
980 KB |
Output is correct |
10 |
Correct |
9 ms |
1612 KB |
Output is correct |
11 |
Correct |
4 ms |
852 KB |
Output is correct |
12 |
Correct |
688 ms |
52472 KB |
Output is correct |
13 |
Correct |
258 ms |
39564 KB |
Output is correct |
14 |
Correct |
800 ms |
51464 KB |
Output is correct |
15 |
Correct |
807 ms |
55568 KB |
Output is correct |
16 |
Correct |
629 ms |
60812 KB |
Output is correct |
17 |
Correct |
716 ms |
63924 KB |
Output is correct |
18 |
Correct |
836 ms |
62312 KB |
Output is correct |
19 |
Correct |
569 ms |
65932 KB |
Output is correct |
20 |
Correct |
635 ms |
66324 KB |
Output is correct |
21 |
Correct |
648 ms |
65968 KB |
Output is correct |