#include<bits/stdc++.h>
#define ll long long
#define F first
#define S second
using namespace std;
int n,m;
int in[1010][1010],out[1010][1010],cnt[1010][1010],hv[2010];
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++){
in[i][j]=out[i][j]=2;
cnt[i][j]=1;
hv[i+j]++;
}
}
for(int i=1;i<=n;i++)in[i][1]--,out[i][m]--;
for(int i=1;i<=m;i++)in[1][i]--,out[n][i]--;
queue<pair<int,int>>q;
auto tp=[&](){
while(!q.empty()){
auto [x,y]=q.front();
//cout<<x<<" "<<y<<" pt\n";
q.pop();
cnt[x][y]--;
hv[x+y]--;
out[x-1][y]--;
if(out[x-1][y]==0)q.push({x-1,y});
out[x][y-1]--;
if(out[x][y-1]==0)q.push({x,y-1});
in[x+1][y]--;
if(in[x+1][y]==0)q.push({x+1,y});
in[x][y+1]--;
if(in[x][y+1]==0)q.push({x,y+1});
}
return;
};
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
int c;
cin>>c;
if(c==1)q.push({i,j});
}
}
tp();
int qu;
cin>>qu;
while(qu--){
int x,y;
cin>>x>>y;
if(cnt[x][y]==0){
cout<<"1\n";
continue;
}else if(hv[x+y]==1){
cout<<"0\n";
continue;
}else{
cout<<"1\n";
q.push({x,y});
tp();
}
}
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |