/*#pragma GCC optimize("O3")
#pragma GCC optimization("Ofast,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")*/
#include <bits/stdc++.h>
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>*/
#define ll long long
#define ll1 long long
#define ull unsigned long long
#define dou long double
#define str string
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define vpll vector<pair<ll, ll>>
#define vbool vector<bool>
#define vstr vector<str>
#define vvll vector<vll>
#define pb push_back
#define pf push_front
#define endl "\n"
#define fr first
#define se second
// #define sortcmp(a) sort(a.begin(), a.end(), cmp)
#define sort(a) sort(a.begin(), a.end())
#define reverse(a) reverse(a.begin(), a.end())
#define speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define ordered_set tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update>
using namespace std;
//using namespace __gnu_pbds;
const ll INF = 1e18+7;
const int lg = 20;
const ll MOD = 1e9+7;
//const ll MOD2 = 998244353;
//const ll MOD3 = 1e6+3;
mt19937 rng(1488);
ll randll(ll l, ll r) {
return uniform_int_distribution<ll>(l, r)(rng);
}
vll p, s;
vpll d;
ll find(ll v) {
if (p[v] == v) return v;
return find(p[v]);
}
void unite(ll a, ll b) {
a = find(a);
b = find(b);
if (a == b) {
d.pb({-1, -1});
return;
}
if (s[a] < s[b]) {
d.pb({a, b});
p[a] = b;
s[b] += s[a];
}
else {
d.pb({b, a});
p[b] = a;
s[a] += s[b];
}
}
void erase() {
pll p1 = d.back();
d.pop_back();
if (p1.fr == -1) return;
s[p1.se] -= s[p1.fr];
p[p1.fr] = p1.fr;
}
int main() {
speed;
ll n, m;
cin >> n >> m;
vvll a(n, vll(m));
p.resize(n*m+7);
s.resize(n*m+7, 1);
for (int i = 0; i < n; i ++) {
for (int j = 0; j < m; j ++) {
cin >> a[i][j];
p[i*m+j] = i*m+j;
//cout << i*m+j << ' ';
}
//cout << endl;
}
p[n*m] = n*m;
p[n*m+1] = n*m+1;
p[n*m+2] = n*m+2;
p[n*m+3] = n*m+3;
for (int i = 0; i < n; i ++) {
for (int j = 0; j < m; j ++) {
if (a[i][j] == 0) continue;
ll x = i, y = j;
for (int i = -1; i <= 1; i ++) {
for (int j = -1; j <= 1; j ++) {
if (i == 0 && j == 0) continue;
ll k = (x+i)*m+y+j;
if (x+i < 0) k = n*m-1+1;
if (x+i >= n) k = n*m-1+3;
if (y+j < 0) k = n*m-1+4;
if (y+j >= m) k = n*m-1+2;
if (k >= n*m || a[x+i][y+j] == 1) unite(x*m+y, k);
}
}
}
}
/*for (int i = 0; i < n; i ++) {
for (int j = 0; j < m; j ++) {
cout << find(i*m+j) << ' ';
}
cout << endl;
}
cout << find(n*m-1+1) << ' ' << find(n*m-1+2) << ' ' << find(n*m-1+3) << ' ' << find(n*m-1+4) << endl;
cout << endl;*/
ll q;
cin >> q;
while (q --) {
ll x, y;;
cin >> x >> y;
x --, y --;
/*for (int i = 0; i < n; i ++) {
for (int j = 0; j < m; j ++) {
cout << find(i*m+j) << ' ';
}
cout << endl;
}
cout << find(n*m-1+1) << ' ' << find(n*m-1+2) << ' ' << find(n*m-1+3) << ' ' << find(n*m-1+4) << endl;
cout << endl;*/
ll cnt = 0;
for (int i = -1; i <= 1; i ++) {
for (int j = -1; j <= 1; j ++) {
if (i == 0 && j == 0) continue;
ll k = (x+i)*m+y+j;
if (x+i < 0) k = n*m-1+1;
if (x+i >= n) k = n*m-1+3;
if (y+j < 0) k = n*m-1+4;
if (y+j >= m) k = n*m-1+2;
if (k >= n*m || a[x+i][y+j] == 1) {
cnt ++;
unite(x*m+y, k);
}
}
}
/*for (int i = 0; i < n; i ++) {
for (int j = 0; j < m; j ++) {
cout << find(i*m+j) << ' ';
}
cout << endl;
}
cout << find(n*m-1+1) << ' ' << find(n*m-1+2) << ' ' << find(n*m-1+3) << ' ' << find(n*m-1+4) << endl;
cout << endl;*/
a[x][y] = 1;
if (find(n*m-1+1) == find(n*m-1+4) || find(n*m-1+1) == find(n*m-1+3) || find(n*m-1+2) == find(n*m-1+4) || find(n*m-1+2) == find(n*m-1+3)) {
cout << 0 << endl;
a[x][y] = 0;
for (int i = 0; i < cnt; i ++) {
erase();
}
}
else cout << 1 << endl;
/*for (int i = 0; i < n; i ++) {
for (int j = 0; j < m; j ++) {
cout << find(i*m+j) << ' ';
}
cout << endl;
}
cout << find(n*m-1+1) << ' ' << find(n*m-1+2) << ' ' << find(n*m-1+3) << ' ' << find(n*m-1+4) << endl;
cout << endl;
cout << endl;*/
}
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |