This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int N = 2000 + 7;
int n;
int m;
int a[N][N];
bool down[N][N];
bool up[N][N];
bool on[N][N];
int cnt[2 * N];
void init()
{
for (int i = 1; i <= n + m; i++)
{
cnt[i] = 0;
}
down[n + 1][m] = 1;
up[1][0] = 1;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (a[i][j] == 0)
{
up[i][j] = up[i - 1][j] | up[i][j - 1];
}
else
{
up[i][j] = 0;
}
}
}
for (int i = n; i >= 1; i--)
{
for (int j = m; j >= 1; j--)
{
if (a[i][j] == 0)
{
down[i][j] = down[i + 1][j] | down[i][j + 1];
}
else
{
down[i][j] = 0;
}
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (up[i][j] && down[i][j])
{
cnt[i + j]++;
on[i][j] = 1;
}
else
{
on[i][j] = 0;
}
}
}
}
bool bdown(int r, int c)
{
return (on[r][c] == 1 && on[r + 1][c] == 0 && on[r][c + 1] == 0);
}
bool bup(int r, int c)
{
return (on[r][c] == 1 && on[r - 1][c] == 0 && on[r][c - 1] == 0);
}
void dfs_up(int r, int c)
{
if (on[r][c])
{
on[r][c] = 0;
cnt[r + c]--;
}
if (bdown(r - 1, c))
{
dfs_up(r - 1, c);
}
if (bdown(r, c - 1))
{
dfs_up(r, c - 1);
}
}
void dfs_down(int r, int c)
{
if (on[r][c])
{
on[r][c] = 0;
cnt[r + c]--;
}
if (bup(r + 1, c))
{
dfs_down(r + 1, c);
}
if (bup(r, c + 1))
{
dfs_down(r, c + 1);
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cin >> a[i][j];
}
}
init();
int q;
cin >> q;
while (q--)
{
int r, c;
cin >> r >> c;
if (on[r][c] == 0)
{
cout << "1\n";
continue;
}
if (cnt[r + c] == 1)
{
cout << "0\n";
continue;
}
cout << "1\n";
a[r][c] = 1;
dfs_up(r, c);
dfs_down(r, c);
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |