#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define sadd(a, b) a = Add(a, b)
#define smul(a, b) a = Mul(a, b)
using namespace std;
const int N = 1e3 + 2;
const int mod = 1e9 + 7;
int n, m;
bool a[N][N], uni[N][N], x[N][N], y[N][N], z[N][N];
set<pair<int, int>> pos[2 * N];
bool Can(int i, int j) {
if (i == 1 && j == 1) return 1;
if (i == n && j == m) return 1;
if (z[i][j] == 0) return 1;
return (z[i - 1][j] | z[i][j - 1]) & (z[i + 1][j] | z[i][j + 1]);
}
void Dfs(int i, int j) {
if (i <= 0 || i > n) return;
if (j <= 0 || j > m) return;
z[i][j] = 0;
pos[i + j].erase({i, j});
if (!Can(i - 1, j)) Dfs(i - 1, j);
if (!Can(i, j - 1)) Dfs(i, j - 1);
if (!Can(i + 1, j)) Dfs(i + 1, j);
if (!Can(i, j + 1)) Dfs(i, j + 1);
}
void Add(int i, int j) {
Dfs(i, j);
for (int l = 2; l <= n + m; l++) {
if (pos[l].size() == 1) {
int ii = pos[l].begin()->first;
int jj = pos[l].begin()->second;
uni[ii][jj] = 1;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
pos[i + j].insert({i, j});
}
}
for (int l = 2; l <= n + m; l++) {
if (pos[l].size() == 1) {
int ii = pos[l].begin()->first;
int jj = pos[l].begin()->second;
uni[ii][jj] = 1;
}
}
x[1][1] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
x[i][j] |= x[i - 1][j] | x[i][j - 1];
}
}
y[n][m] = 1;
for (int i = n; i >= 1; i--) {
for (int j = m; j >= 1; j--) {
y[i][j] |= y[i + 1][j] | y[i][j + 1];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
z[i][j] = x[i][j] & y[i][j];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i][j] == 1) Add(i, j);
}
}
int q; cin >> q;
while (q--) {
int i, j;
cin >> i >> j;
if (uni[i][j] == 1) {
cout << 0 << en;
}
else {
cout << 1 << en;
Add(i, j);
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
980 KB |
Output is correct |
2 |
Correct |
5 ms |
1108 KB |
Output is correct |
3 |
Correct |
6 ms |
1236 KB |
Output is correct |
4 |
Correct |
13 ms |
1364 KB |
Output is correct |
5 |
Correct |
14 ms |
1364 KB |
Output is correct |
6 |
Correct |
15 ms |
1492 KB |
Output is correct |
7 |
Correct |
16 ms |
1492 KB |
Output is correct |
8 |
Correct |
16 ms |
1452 KB |
Output is correct |
9 |
Correct |
16 ms |
1492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
980 KB |
Output is correct |
2 |
Correct |
5 ms |
1108 KB |
Output is correct |
3 |
Correct |
6 ms |
1236 KB |
Output is correct |
4 |
Correct |
13 ms |
1364 KB |
Output is correct |
5 |
Correct |
14 ms |
1364 KB |
Output is correct |
6 |
Correct |
15 ms |
1492 KB |
Output is correct |
7 |
Correct |
16 ms |
1492 KB |
Output is correct |
8 |
Correct |
16 ms |
1452 KB |
Output is correct |
9 |
Correct |
16 ms |
1492 KB |
Output is correct |
10 |
Correct |
165 ms |
3644 KB |
Output is correct |
11 |
Correct |
13 ms |
1236 KB |
Output is correct |
12 |
Correct |
1831 ms |
54204 KB |
Output is correct |
13 |
Correct |
537 ms |
47300 KB |
Output is correct |
14 |
Execution timed out |
5067 ms |
53276 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |