#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pi3 = pair<pii, int>;
#define IOS ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define F first
#define S second
#define sz(x) x.size()
#define all(x) x.begin(), x.end()
#define pb push_back
#define minr(a, b) a = min(a, b);
#define maxr(a, b) a = max(a, b);
#define shit cout << "shit\n" << flush;
#define tl while(1&1) continue;
#define rand(l, r) uniform_int_distribution<int64_t>(l,r)(rng)
random_device device; default_random_engine rng(device());
const int Mod = 1e9 + 7; //998244353;
const int LG = 64;
const int SQ = 500;
const int Inf = 2e9 + 10;
const int maxN = 1e3 + 10;
const int maxNM = 1e6 + 10;
int n, m;
int q;
int idx[maxN][maxN];
bool c[maxN][maxN];
bool mark[maxN][maxN];
vector <pii> topol;
vector <pii> neighb[maxN][maxN];
vector <pii> bneighb[maxN][maxN];
struct SegTree {
struct Node {
int lazy;
int mn;
Node() {
lazy = 0;
mn = 0;
}
} t[maxNM<<2];
void spread(int id, int L, int R) {
if(L+1 == R)
return;
t[2*id+0].lazy += t[id].lazy;
t[2*id+0].mn += t[id].lazy;
t[2*id+1].lazy += t[id].lazy;
t[2*id+1].mn += t[id].lazy;
t[id].lazy = 0;
}
void update(int id, int L, int R, int l, int r, int val) {
spread(id, L, R);
if(L == l and R == r) {
t[id].mn += val;
t[id].lazy += val;
return;
}
int mid = (L+R)>>1;
if(l < mid)
update(2*id+0, L, mid, l, min(mid, r), val);
if(r > mid)
update(2*id+1, mid, R, max(l, mid), r, val);
t[id].mn = min(t[2*id+0].mn, t[2*id+1].mn);
}
} s;
void dfs(int x, int y) {
mark[x][y] = true;
for(auto [x2, y2] : neighb[x][y])
if(!mark[x2][y2] and c[x2][y2])
dfs(x2, y2);
topol.pb({x, y});
}
int main() {
IOS;
cin >> n >> m;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cin >> c[i][j];
c[i][j] = !c[i][j];
}
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(c[i][j]) {
if(c[i+1][j]) {
neighb[i][j].pb({i+1, j});
bneighb[i+1][j].pb({i, j});
}
if(c[i][j+1]) {
neighb[i][j].pb({i, j+1});
bneighb[i][j+1].pb({i, j});
}
}
}
}
dfs(1, 1);
reverse(all(topol));
vector <pii> tmp;
bool check = false;
for(auto x : topol) {
if(!check)
tmp.pb(x);
else
mark[x.F][x.S] = false;
if(x == pii(n, m))
check = true;
}
topol = tmp;
int k = sz(topol);
for(int i = 0; i < k; i++) {
idx[topol[i].F][topol[i].S] = i+1;
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(!mark[i][j])
continue;
for(auto [x, y] : neighb[i][j])
if(mark[x][y])
s.update(1, 1, k, idx[i][j], idx[x][y], 1);
}
}
cin >> q;
while(q--) {
int x, y;
cin >> x >> y;
if(!mark[x][y]) {
c[x][y] = 0;
cout << "1\n";
continue;
}
vector <pii> act;
for(auto [x2, y2] : neighb[x][y])
if(mark[x2][y2] and c[x2][y2])
act.pb({idx[x][y], idx[x2][y2]});
for(auto [x2, y2] : bneighb[x][y])
if(mark[x2][y2] and c[x2][y2])
act.pb({idx[x2][y2], idx[x][y]});
for(auto [l, r] : act)
s.update(1, 1, k, l, r, -1);
if(s.t[1].mn != 0) {
c[x][y] = 0;
cout << "1\n";
continue;
}
for(auto [l, r] : act)
s.update(1, 1, k, l, r, +1);
cout << "0\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |