This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//by szh
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define debug(x) cerr<<#x<<"="<<x<<endl
#define pq priority_queue
#define inf 0x3f
#define rep(i,a,b) for (int i=a;i<(b);i++)
#define MP make_pair
#define SZ(x) (int(x.size()))
#define ll long long
#define mod 1000000007
#define ALL(x) x.begin(),x.end()
void inc(int &a,int b) {a=(a+b)%mod;}
void dec(int &a,int b) {a=(a-b+mod)%mod;}
int lowbit(int x) {return x&(-x);}
ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;}
const int maxn = 1010;
int n,m,Q;
int st[maxn][maxn],ed[maxn][maxn];
vector <pii> pos;
int cnt[maxn*2];
void update(pii x) {
queue <pii> q;
if (st[x.fi][x.se]>0) st[x.fi][x.se]=1,q.push(x);
while (!q.empty()) {
int r = q.front().fi, c = q.front().se;
q.pop();
st[r][c]--;
if (st[r][c]==0) {
if (ed[r][c]>0) cnt[r+c]--;
if (r+1<n and st[r+1][c]>0 and MP(r+1,c)!=MP(n-1,m-1)) q.push({r+1,c});
if (c+1<m and st[r][c+1]>0 and MP(r,c+1)!=MP(n-1,m-1)) q.push({r,c+1});
}
}
if (ed[x.fi][x.se]>0) ed[x.fi][x.se]=1,q.push(x);
while (!q.empty()) {
int r = q.front().fi, c = q.front().se;
q.pop();
ed[r][c]--;
if (ed[r][c]==0) {
if (st[r][c]>0) cnt[r+c]--;
if (r>0 and ed[r-1][c]>0 and MP(r-1,c)!=MP(0,0)) q.push({r-1,c});
if (c>0 and ed[r][c-1]>0 and MP(r,c-1)!=MP(0,0)) q.push({r,c-1});
}
}
}
int main() {
// freopen("input.txt","r",stdin);
std::ios::sync_with_stdio(false);cin.tie(0);
cin>>n>>m;
rep(i,0,n)
rep(j,0,m) {
int x;cin>>x;
if (x==1) pos.pb({i,j});
st[i][j] = (i>0) + (j>0);
ed[i][j] = (i+1<n) + (j+1<m);
cnt[i+j]++;
}
for (auto it:pos) update(it);
cin>>Q;
while (Q--) {
pii x;cin>>x.fi>>x.se;
x.fi--,x.se--;
// debug(st[x.fi][x.se]);debug(ed[x.fi][x.se]);
if (st[x.fi][x.se]*ed[x.fi][x.se]==0 or cnt[x.fi+x.se]>1) cout<<"1\n",update(x);
else cout<<"0\n";
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |