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>
using namespace std;
typedef long long ll;
#define int long long
typedef pair<int, int> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0)
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define lowbit(x) x&-x;
const int maxn = 1005;
const int maxq = 1e6 + 5;
const int INF = 1e18;
int c[maxn][maxn];
int st[maxn][maxn];
pll coord[maxq];
int cnt[maxn * 2];
int ans[maxq];
int n, m, q;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
bool inbound(int x, int y){ return x >= 0 && y >= 0 && x < n && y < m;}
void remove(int x, int y){
st[x][y] = 0, cnt[x + y] --;
queue<pll> q;
q.push({x, y});
while(!q.empty()){
auto [cx, cy] = q.front(); q.pop();
for(int d = 2; d < 4; d++){
int xx = cx + dx[d], yy = cy + dy[d];
if(!inbound(xx, yy) || !st[xx][yy]) continue;
if(!st[xx + 1][yy] && !st[xx][yy + 1]){
cnt[xx + yy] --;
st[xx][yy] = 0;
q.push({xx, yy});
}
}
}
q.push({x, y});
while(!q.empty()){
auto [cx, cy] = q.front(); q.pop();
for(int d = 0; d < 2; d++){
int xx = cx + dx[d], yy = cy + dy[d];
if(!inbound(xx, yy) || !st[xx][yy]) continue;
if(!st[xx - 1][yy] && !st[xx][yy - 1]){
cnt[xx + yy] --;
st[xx][yy] = 0;
q.push({xx, yy});
}
}
}
}
signed main(void){
fastio;
cin>>n>>m;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++) cin>>c[i][j];
}
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++) st[i][j] = 1, cnt[i + j] ++;
}
cin>>q;
for(int i = 0; i < q; i++){
cin>>coord[i].f>>coord[i].s;
coord[i].f--, coord[i].s--;
}
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(c[i][j]){
remove(i, j);
}
}
}
for(int i = 0; i < q; i++){
auto [x, y] = coord[i];
if(st[x][y] && cnt[x + y] == 1) continue;
if(st[x][y]) remove(x, y);
ans[i] = 1;
}
for(int i = 0; i < q; i++) cout<<ans[i]<<"\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |