#include <bits/stdc++.h>
using namespace std;
#define rep(i,s,e) for (int i = s; i <= e; ++i)
#define rrep(i,s,e) for (int i = s; i >= e; --i)
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define len(a) (int)a.size()
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
const int mx = 1e3;
int ftree[mx+1][mx+1][2];
void upd (int tp, int p, int x, int val) {
x++;
while (x <= mx) {
ftree[x][p][tp] += val;
x += x&(-x);
}
}
int pref (int tp, int p, int x) {
int sum = 0;
while (x>0) {
sum += ftree[x][p][tp];
x -= x&(-x);
}
return sum;
}
int query (int tp, int p, int l, int r) {
l++, r++;
return pref(tp, p, r)-pref(tp, p, l-1);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m; cin >> n >> m;
bool c[n+1][m+1] = {}, d[n+1][m+1] = {};
rep (i,0,n-1) {
rep (j,0,m-1) {
cin >> c[i][j];
c[i][j] ^= 1;
d[i][j]=c[i][j];
}
}
rrep (i,n-1,0) {
rrep (j,m-1,0) {
bool b = 0;
if (j<m-1) b |= c[i][j+1];
if (i<n-1) b |= c[i+1][j];
if (i==n-1 && j==m-1) b = 1;
c[i][j] &= b;
}
}
rep (i,0,n-1) {
rep (j,0,m-1) {
bool b = 0;
if (j) b |= d[i][j-1];
if (i) b |= d[i-1][j];
if (!i && !j) b = 1;
d[i][j] &= b;
if (c[i][j] && d[i][j]) {
upd(0,j,i,1);
upd(1,i,j,1);
}
}
}
int Q; cin >> Q;
while (Q--) {
int x, y; cin >> x >> y;
x--, y--;
if ((y==m-1 || query(0,y+1,0,x-1)==0) && (x==n-1 || query(1,x+1,0,y-1)==0)) cout << "0\n";
else {
cout << "1\n";
if (!c[x][y] || !d[x][y]) continue;
queue<ii> q;
q.push({x,y});
while (len(q)) {
int i = q.front().fi, j = q.front().se;
q.pop();
if (i!=x || j!=y) {
if (!c[i][j] || c[i+1][j] || c[i][j+1]) continue;
}
c[i][j] = 0;
if(d[i][j]){
upd (0, j, i, -1);
upd (1, i, j, -1);
}
if (i && c[i-1][j]) q.push({i-1,j});
if (j && c[i][j-1]) q.push({i,j-1});
}
d[x][y] = 0;
q.push({x+1,y});
q.push({x,y+1});
while (len(q)) {
int i = q.front().fi, j = q.front().se;
q.pop();
if (!d[i][j] || (i&&d[i-1][j]) || (j&&d[i][j-1])) continue;
d[i][j] = 0;
if(c[i][j]){
upd (0, j, i, -1);
upd (1, i, j, -1);
}
if (i<n-1 && d[i+1][j]) q.push({i+1,j});
if (j<m-1 && d[i][j+1]) q.push({i,j+1});
}
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
724 KB |
Output is correct |
2 |
Correct |
2 ms |
724 KB |
Output is correct |
3 |
Correct |
3 ms |
724 KB |
Output is correct |
4 |
Correct |
4 ms |
852 KB |
Output is correct |
5 |
Correct |
5 ms |
852 KB |
Output is correct |
6 |
Correct |
5 ms |
852 KB |
Output is correct |
7 |
Correct |
5 ms |
856 KB |
Output is correct |
8 |
Correct |
5 ms |
852 KB |
Output is correct |
9 |
Correct |
6 ms |
852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
724 KB |
Output is correct |
2 |
Correct |
2 ms |
724 KB |
Output is correct |
3 |
Correct |
3 ms |
724 KB |
Output is correct |
4 |
Correct |
4 ms |
852 KB |
Output is correct |
5 |
Correct |
5 ms |
852 KB |
Output is correct |
6 |
Correct |
5 ms |
852 KB |
Output is correct |
7 |
Correct |
5 ms |
856 KB |
Output is correct |
8 |
Correct |
5 ms |
852 KB |
Output is correct |
9 |
Correct |
6 ms |
852 KB |
Output is correct |
10 |
Correct |
14 ms |
4460 KB |
Output is correct |
11 |
Correct |
5 ms |
1364 KB |
Output is correct |
12 |
Correct |
287 ms |
14400 KB |
Output is correct |
13 |
Correct |
87 ms |
10960 KB |
Output is correct |
14 |
Correct |
461 ms |
18916 KB |
Output is correct |
15 |
Correct |
456 ms |
19148 KB |
Output is correct |
16 |
Correct |
382 ms |
20336 KB |
Output is correct |
17 |
Correct |
427 ms |
21236 KB |
Output is correct |
18 |
Correct |
525 ms |
20760 KB |
Output is correct |
19 |
Correct |
378 ms |
21672 KB |
Output is correct |
20 |
Correct |
429 ms |
21552 KB |
Output is correct |
21 |
Correct |
387 ms |
21576 KB |
Output is correct |