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>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m; cin >> n >> m;
bool **s = new bool*[n], **blocked = new bool*[n], **nice = new bool*[n];
for(int i = 0; i < n; i++){
s[i] = new bool[m], blocked[i] = new bool[m], nice[i] = new bool[m];
fill(s[i], s[i]+m, 0);
fill(blocked[i], blocked[i]+m, 0);
fill(nice[i], nice[i]+m, 1);
}
int* dig = new int[n+m-1];
fill(dig, dig+n+m-1, 0);
for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) dig[i+j]++;
auto valid = [&](int x, int y){
return x >= 0 && x < n && y >= 0 && y < m;
};
auto is_blocked = [&](int x, int y){
return !valid(x, y) || blocked[x][y];
};
auto is_nice = [&](int x, int y){
return valid(x, y) && nice[x][y];
};
function<void(int, int)> make_unnice = [&](int x, int y){
if(!nice[x][y]) return;
nice[x][y] = 0;
dig[x+y]--;
if(is_nice(x-1, y) && !is_nice(x-1, y+1)) make_unnice(x-1, y);
if(is_nice(x, y-1) && !is_nice(x+1, y-1)) make_unnice(x, y-1);
};
function<void(int, int)> block = [&](int x, int y){
blocked[x][y] = 1;
make_unnice(x, y);
if(!is_blocked(x+1, y) && is_blocked(x+1, y-1)) block(x+1, y);
if(!is_blocked(x, y+1) && is_blocked(x-1, y+1)) block(x, y+1);
};
auto add = [&](int x, int y){
if(nice[x][y] && dig[x+y] == 1) return 0;
block(x, y);
return 1;
};
for(int i = 0; i < n; i++) for(int j = 0; j < m; j++){
bool x; cin >> x;
if(x) add(i, j);
}
int q; cin >> q;
while(q--){
int x, y; cin >> x >> y; x--, y--;
cout << add(x, y) << "\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |