//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
const int mxn=1005;
int a[mxn][mxn];
int ok[mxn*2];
int cnt[mxn][mxn];
bool vis[mxn][mxn];
int in[mxn][mxn];
int out[mxn][mxn];
int main() {_
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>a[i][j];
}
}
{
queue<pair<int,int>> q;
if(a[n][m]==0){
vis[n][m]=true;
q.push({n,m});
}
while(!q.empty()){
auto [x,y]=q.front();
q.pop();
if(x>1){
if(a[x-1][y]==0){
in[x-1][y]++;
if(!vis[x-1][y]){
vis[x-1][y]=true;
q.push({x-1,y});
}
}
}
if(y>1){
if(a[x][y-1]==0){
in[x][y-1]++;
if(!vis[x][y-1]){
vis[x][y-1]=true;
q.push({x,y-1});
}
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(vis[i][j]) cnt[i][j]++;
vis[i][j]=false;
}
}
}
{
queue<pair<int,int>> q;
if(a[1][1]==0){
vis[1][1]=true;
q.push({1,1});
}
while(!q.empty()){
auto [x,y]=q.front();
q.pop();
if(x<n){
if(a[x+1][y]==0){
out[x+1][y]++;
if(!vis[x+1][y]){
vis[x+1][y]=true;
q.push({x+1,y});
}
}
}
if(y<m){
if(a[x][y+1]==0){
out[x][y+1]++;
if(!vis[x][y+1]){
vis[x][y+1]=true;
q.push({x,y+1});
}
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(vis[i][j]) cnt[i][j]++;
vis[i][j]=false;
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(cnt[i][j]==2) ok[i+j]++;
}
}
int ans=0;
for(int i=1+1;i<=n+m;i++){
if(ok[i]>0) ans++;
}
int sz=(n+m)-(1+1)+1;
int q;
cin>>q;
for(int t=0;t<q;t++){
int x,y;
cin>>x>>y;
if(ans<sz){
cout<<0<<'\n';
continue;
}
if(cnt[x][y]==2 and ok[x+y]==1){
cout<<0<<'\n';
continue;
}
cout<<1<<'\n';
if(cnt[x][y]<2) continue;
queue<pair<int,int>> q;
cnt[x][y]--;
ok[x+y]--;
if(ok[x+y]==0) ans--;
q.push({x,y});
while(!q.empty()){
auto [x,y]=q.front();
q.pop();
if(x<n){
if(cnt[x+1][y]==2){
out[x+1][y]--;
if(out[x+1][y]==0){
cnt[x+1][y]--;
ok[x+1+y]--;
if(ok[x+1+y]==0) ans--;
q.push({x+1,y});
}
}
}
if(y<m){
if(cnt[x][y+1]==2){
out[x][y+1]--;
if(out[x][y+1]==0){
cnt[x][1]--;
ok[x+y+1]--;
if(ok[x+y+1]==0) ans--;
q.push({x,y+1});
}
}
}
if(x>1){
if(cnt[x-1][y]==2){
in[x-1][y]--;
if(in[x-1][y]==0){
cnt[x-1][y]--;
ok[x-1+y]--;
if(ok[x-1+y]==0) ans--;
q.push({x-1,y});
}
}
}
if(y>1){
if(cnt[x][y-1]==2){
in[x][y-1]--;
if(in[x][y-1]==0){
cnt[x][y-1]--;
ok[x+y-1]--;
if(ok[x+y-1]==0) ans--;
q.push({x,y-1});
}
}
}
}
}
return 0;
}
//maybe its multiset not set
//yeeorz
//diaoborz
Compilation message (stderr)
furniture.cpp: In function 'void setIO(std::string)':
furniture.cpp:15:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
furniture.cpp:16:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |