#include <bits/stdc++.h>
using namespace std;
const int MX=1005;
int N,M,Q;
int C[MX][MX], deg[MX][MX], rdeg[MX][MX];
set<pair<int,int>> st[2*MX];
void dfs(int x, int y) {
if(x+1<=N && !C[x+1][y]) {
deg[x+1][y]-=1;
if(deg[x+1][y]==0) {
st[x+1+y-2].erase({x+1,y});
C[x+1][y]=1;
dfs(x+1,y);
}
}
if(y+1<=M && !C[x][y+1]) {
deg[x][y+1]-=1;
if(deg[x][y+1]==0) {
st[x+y+1-2].erase({x,y+1});
C[x][y+1]=1;
dfs(x,y+1);
}
}
}
void rdfs(int x, int y) {
if(x-1>=1 && !C[x-1][y]) {
rdeg[x-1][y]-=1;
if(rdeg[x-1][y]==0) {
st[x-1+y-2].erase({x-1,y});
C[x-1][y]=1;
rdfs(x-1,y);
}
}
if(y-1>=1 && !C[x][y-1]) {
rdeg[x][y-1]-=1;
if(rdeg[x][y-1]==0) {
st[x+y-1-2].erase({x,y-1});
C[x][y-1]=1;
rdfs(x,y-1);
}
}
}
bool r[MX][MX], rr[MX][MX];
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin>>N>>M;
for(int i=1;i<=N;i++) {
for(int j=1;j<=M;j++) {
cin>>C[i][j];
}
}
r[1][1]=1;
for(int i=1;i<=N;i++) {
for(int j=1;j<=M;j++) {
if(C[i][j]) continue;
if(!r[i][j]) continue;
r[i+1][j]|=r[i][j];
r[i][j+1]|=r[i][j];
deg[i][j+1]+=1;
deg[i+1][j]+=1;
}
}
rr[N][M]=1;
for(int i=N;i>=1;i--) {
for(int j=M;j>=1;j--) {
if(C[i][j]) continue;
if(!rr[i][j]) continue;
rr[i-1][j]|=rr[i][j];
rr[i][j-1]|=rr[i][j];
rdeg[i-1][j]+=1;
rdeg[i][j-1]+=1;
}
}
for(int i=1;i<=N;i++) {
for(int j=1;j<=M;j++) {
if(C[i][j]) {
r[i][j]=0;
rr[i][j]=0;
}
}
}
for(int i=1;i<=N;i++) {
for(int j=1;j<=M;j++) {
if(r[i][j] && rr[i][j]) {
st[i+j-2].insert({i,j});
}
}
}
cin>>Q;
for(int i=1;i<=Q;i++) {
int x,y;
cin>>x>>y;
if(!st[x+y-2].count({x,y})) {
cout<<1<<endl;
continue;
}
if(st[x+y-2].size()==1) {
cout<<0<<endl;
continue;
}
st[x+y-2].erase({x,y});
C[x][y]=1;
dfs(x,y);
rdfs(x,y);
cout<<1<<endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8792 KB |
Output is correct |
2 |
Correct |
6 ms |
10844 KB |
Output is correct |
3 |
Correct |
8 ms |
10844 KB |
Output is correct |
4 |
Correct |
12 ms |
11100 KB |
Output is correct |
5 |
Correct |
13 ms |
11352 KB |
Output is correct |
6 |
Correct |
16 ms |
11228 KB |
Output is correct |
7 |
Correct |
15 ms |
11352 KB |
Output is correct |
8 |
Correct |
15 ms |
11356 KB |
Output is correct |
9 |
Correct |
15 ms |
11376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8792 KB |
Output is correct |
2 |
Correct |
6 ms |
10844 KB |
Output is correct |
3 |
Correct |
8 ms |
10844 KB |
Output is correct |
4 |
Correct |
12 ms |
11100 KB |
Output is correct |
5 |
Correct |
13 ms |
11352 KB |
Output is correct |
6 |
Correct |
16 ms |
11228 KB |
Output is correct |
7 |
Correct |
15 ms |
11352 KB |
Output is correct |
8 |
Correct |
15 ms |
11356 KB |
Output is correct |
9 |
Correct |
15 ms |
11376 KB |
Output is correct |
10 |
Correct |
33 ms |
9052 KB |
Output is correct |
11 |
Correct |
10 ms |
9048 KB |
Output is correct |
12 |
Correct |
815 ms |
55020 KB |
Output is correct |
13 |
Correct |
177 ms |
41040 KB |
Output is correct |
14 |
Correct |
1456 ms |
58368 KB |
Output is correct |
15 |
Correct |
1549 ms |
63428 KB |
Output is correct |
16 |
Correct |
1448 ms |
67840 KB |
Output is correct |
17 |
Correct |
1483 ms |
71204 KB |
Output is correct |
18 |
Correct |
1614 ms |
69604 KB |
Output is correct |
19 |
Correct |
1483 ms |
72968 KB |
Output is correct |
20 |
Correct |
1509 ms |
72944 KB |
Output is correct |
21 |
Correct |
1520 ms |
72808 KB |
Output is correct |