#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define sz(a) (int)((a).size())
#define all(a) (a).begin(), (a).end()
#define lsb(x) (x & (-x))
#define vi vector<int>
#define YES { cout << "YES" << endl; return; }
#define NO { cout << "NO" << endl; return; }
using ll = long long;
using pii = std::pair<int, int>;
const int NMAX = 1e3;
const int dlin[] = {1, 0};
const int dcol[] = {0, 1};
using namespace std;
int mat[NMAX + 5][NMAX + 5], reachable[NMAX + 5][NMAX + 5], diag[2 * NMAX + 5], n, m, q;
bool inbound(int r, int c) {
return (r >= 1 && r <= n && c >= 1 && c <= m);
}
void f(int r, int c) {
reachable[r][c] = true;
++diag[r + c];
for (int dir = 0; dir < 2; ++dir) {
int x = r - dlin[dir];
int y = c - dcol[dir];
if (inbound(x, y) && mat[x][y] == 0 && reachable[x][y] == 0)
f(x, y);
}
}
void update(int r, int c) {
reachable[r][c] = 0;
mat[r][c] = 1;
--diag[r + c];
for (int dir = 0; dir < 2; ++dir) {
int x = r - dlin[dir];
int y = c - dcol[dir];
if (inbound(x, y) && reachable[x][y] && reachable[x + 1][y] + reachable[x][y + 1] == 0)
update(x, y);
}
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
cin >> mat[i][j];
f(n, m);
cin >> q;
for (int i = 0, x, y; i < q; ++i) {
cin >> x >> y;
if (reachable[x][y] && diag[x + y] == 1){
cout << "0\n";
continue;
}
cout << "1\n";
update(x, y);
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |