This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// In the name of God
// : )
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
const int N = 1000 + 5;
bool di[2] = {0, 1}, dj[2] = {1, 0};
int n, m;
bool grid[N][N], ok[2][N][N];
int cnt[N * 2];
bool valid(int i, int j) {
return i >= 0 && j >= 0 && i < n && j < m && grid[i][j] == 0;
}
void dfs(int s, int t, int k) {
ok[k == -1][s][t] = true;
for (int i = 0; i < 2; i++) {
int x = s + k * di[i], y = t + dj[i] * k;
if (valid(x, y) && !ok[k == -1][x][y]) {
dfs(x, y, k);
}
}
}
void upd(int i, int j, int k) {
bool tmp = false;
for (int t = 0; t < 2; t++) {
tmp |= valid(i - k * di[t], j - k * dj[t]) && ok[k == -1][i - k * di[t]][j - k * dj[t]];
}
if (tmp)
return;
if (ok[0][i][j] && ok[1][i][j])
cnt[i + j]--;
ok[k == -1][i][j] = false;
for (int t = 0; t < 2; t++) {
int x = i + di[t] * k, y = j + dj[t] * k;
if (valid(x, y) && ok[k == -1][x][y])
upd(x, y, k);
}
}
void solve() {
cin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> grid[i][j];
dfs(0, 0, 1);
dfs(n - 1, m - 1, -1);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cnt[i + j] += ok[0][i][j] && ok[1][i][j];
int q; cin >> q;
while (q--) {
int i, j; cin >> i >> j;
i--, j--;
if (!ok[0][i][j] || !ok[1][i][j]) {
cout << 1 << '\n';
grid[i][j] = 1;
}
else if (cnt[i + j] > 1) {
cout << 1 << '\n';
grid[i][j] = 1;
ok[0][i][j] = ok[1][i][j] = false;
cnt[i + j]--;
for (int t = 0; t < 2; t++) {
int x = i + di[t], y = j + dj[t];
int x2 = i - di[t], y2 = j - dj[t];
if (valid(x, y) && ok[0][x][y])
upd(i + di[t], j + dj[t], 1);
if (valid(x2, y2) && ok[1][x2][y2])
upd(i - di[t], j - dj[t], -1);
}
}
else {
cout << 0 << '\n';
}
}
}
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |