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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl
#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//change less to less_equal for non distinct pbds, but erase will bug
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
int n,m,q;
int c[1005][1005];
bool fblock[1005][1005];
bool bblock[1005][1005];
int cnt[2005];
queue<ii> fq,bq;
void proc(int i,int j){
//cout<<"debug: "<<i<<" "<<j<<endl;
if (!fblock[i][j]) fq.push(ii(i,j));
if (!bblock[i][j]) bq.push(ii(i,j));
int a,b;
while (!fq.empty()){
tie(a,b)=fq.front(),fq.pop();
//cout<<"f: "<<a<<" "<<b<<endl;
fblock[a][b]=true;
if (!bblock[a][b]) cnt[a+b]--;
if (!fblock[a+1][b] && fblock[a+1][b-1]) fq.push(ii(a+1,b));
if (!fblock[a][b+1] && fblock[a-1][b+1]) fq.push(ii(a,b+1));
}
while (!bq.empty()){
tie(a,b)=bq.front(),bq.pop();
//cout<<"b: "<<a<<" "<<b<<endl;
bblock[a][b]=true;
if (!fblock[a][b]) cnt[a+b]--;
if (!bblock[a-1][b] && bblock[a-1][b+1]) bq.push(ii(a-1,b));
if (!bblock[a][b-1] && bblock[a+1][b-1]) bq.push(ii(a,b-1));
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin.exceptions(ios::badbit | ios::failbit);
cin>>n>>m;
rep(x,1,n+1) rep(y,1,m+1) cnt[x+y]++;
rep(x,1,m+1) fblock[0][x]=fblock[n+1][x]=bblock[0][x]=bblock[n+1][x]=true;
rep(x,1,n+1) fblock[x][0]=fblock[x][m+1]=bblock[x][0]=bblock[x][m+1]=true;
rep(x,1,n+1){
rep(y,1,m+1){
cin>>c[x][y];
if (c[x][y]) proc(x,y);
}
}
cin>>q;
int a,b;
while (q--){
cin>>a>>b;
if (fblock[a][b] || bblock[a][b] || cnt[a+b]>1){
cout<<1<<endl;
proc(a,b);
}
else{
cout<<0<<endl;
}
/*
rep(x,1,n+1){
rep(y,1,m+1) cout<<fblock[x][y]<<" ";
cout<<endl;
}
*/
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |