#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define endl "\n"
#define int long long
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
int n,m;
vector<vector<int>> v;
vector<int> freq;
bool valid(int i,int j){
return min(i,j)>=1 && i<=n && j<=m;
}
void upd(int i,int j){
if(!valid(i,j)) return;
if(v[i][j]==0) return;
freq[i+j]++;
if(valid(i+1,j) && v[i+1][j]==0){
if(!valid(i+1,j-1) || v[i+1][j-1]==1){
v[i+1][j]=1;
upd(i+1,j);
}
}
if(valid(i,j+1) && v[i][j+1]==0){
if(!valid(i-1,j+1) || v[i-1][j+1]==1){
v[i][j+1]=1;
upd(i,j+1);
}
}
if(valid(i-1,j) && v[i-1][j]==0){
if(!valid(i-1,j+1) || v[i-1][j+1]==1){
v[i-1][j]=1;
upd(i-1,j);
}
}
if(valid(i,j-1) && v[i][j-1]==0){
if(!valid(i+1,j-1) || v[i+1][j-1]==1){
v[i][j-1]=1;
upd(i,j-1);
}
}
}
void solve(){
cin >> n >> m;
v.assign(n+5,vector<int>(m+5,0));
freq.assign(n+m+5,0);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
int u;
cin >> u;
if(u==1 && v[i][j]==0){
v[i][j]=1;
upd(i,j);
}
}
}
int q;
cin >> q;
while(q--){
int a,b;
cin >> a >> b;
if(v[a][b]==1){
cout << 1 << endl;
continue;
}
int u = a + b;
if(freq[u]==min(a-1,m-b)+min(n-a,b-1)){
cout << 0 << endl;
continue;
}
cout << 1 << endl;
v[a][b]=1;
upd(a,b);
}
}
int32_t main(){
cin.tie(0); ios::sync_with_stdio(0);
int t=1;//cin >> t;
while(t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
856 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
600 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
3 ms |
604 KB |
Output is correct |
9 |
Correct |
3 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
856 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
600 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
3 ms |
604 KB |
Output is correct |
9 |
Correct |
3 ms |
604 KB |
Output is correct |
10 |
Correct |
7 ms |
1332 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
110 ms |
12688 KB |
Output is correct |
13 |
Correct |
44 ms |
9312 KB |
Output is correct |
14 |
Correct |
224 ms |
17124 KB |
Output is correct |
15 |
Correct |
200 ms |
17024 KB |
Output is correct |
16 |
Correct |
197 ms |
18380 KB |
Output is correct |
17 |
Correct |
238 ms |
19284 KB |
Output is correct |
18 |
Correct |
209 ms |
18900 KB |
Output is correct |
19 |
Correct |
241 ms |
20052 KB |
Output is correct |
20 |
Correct |
209 ms |
20048 KB |
Output is correct |
21 |
Correct |
232 ms |
19900 KB |
Output is correct |