#include <iostream>
#include <queue>
using namespace std;
const int N = 1005;
int n, m, q, cur, a[N][N], Lst[N][N], cnt[N<<1];
void process1(int i, int j){
++cur;
queue<pair<int,int>> Q;
Q.push({i, j});
while (Q.size() > 0){
auto [r, c] = Q.front();
Q.pop();
cnt[r+c] -= !a[r][c];
a[r][c] = 1;
if (Lst[r+1][c] != cur and a[r+1][c] == 0 and a[r+1][c-1] == 1)
Lst[r+1][c] = cur, Q.push({r+1, c});
if (Lst[r][c+1] != cur and a[r][c+1] == 0 and a[r-1][c+1] == 1)
Lst[r][c+1] = cur, Q.push({r, c+1});
}
}
void process2(int i, int j){
++cur;
queue<pair<int,int>> Q;
Q.push({i, j});
while (Q.size() > 0){
auto [r, c] = Q.front();
Q.pop();
cnt[r+c] -= !a[r][c];
a[r][c] = 1;
if (Lst[r-1][c] != cur and a[r-1][c] == 0 and a[r-1][c+1] == 1)
Lst[r-1][c] = cur, Q.push({r-1, c});
if (Lst[r][c-1] != cur and a[r][c-1] == 0 and a[r+1][c-1] == 1)
Lst[r][c-1] = cur, Q.push({r, c-1});
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin>>n>>m;
for (int i=0;i<N;i++)
a[0][i] = a[n+1][i] = a[i][0] = a[i][m+1] = 1;
for (int i=1;i<=n;i++){
for (int j=1, c;j<=m;j++){
cnt[i+j]++;
cin>>c;
if (c and a[i][j] == 0){
process1(i, j);
process2(i, j);
}
}
}
cin>>q;
for (int i=1;i<=q;i++){
int r, c;
cin>>r>>c;
if (a[r][c] == 1 or cnt[r+c] > 1){
cout<<"1\n";
process1(r, c);
process2(r, c);
}
else
cout<<"0\n";
}
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |