#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) begin(x), end(x)
#define sz(x) int((x).size())
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
#ifdef LOCAL
auto operator<<(auto& o, auto x) -> decltype(x.first, o);
auto operator<<(auto& o, auto x) -> decltype(x.end(), o) {
o << "{";
for (int i = 0; auto y : x) o << ", " + !i++ * 2 << y;
return o << "}"; }
auto operator<<(auto& o, auto x) -> decltype(x.first, o) {
return o << "(" << x.first << ", " << x.second << ")"; }
void __print(auto... x) { ((cerr << x << " "), ...) << endl; }
#define debug(x...) __print("[" #x "]:", x)
#else
#define debug(...) 2137
#endif
struct RollbackUF {
vi e; vector<pii> st;
vi c; vector<pii> sc;
int suma = 0; vi ss;
RollbackUF(int n) : e(n, -1), c(n, 0) {}
int size(int x) { return -e[find(x)]; }
int find(int x) { return e[x] < 0 ? x : find(e[x]); }
int time() { return sz(st); }
void rollback(int t) {
for (int i = time(); i --> t;)
e[st[i].first] = st[i].second;
for (int i = time(); i --> t;)
c[sc[i].first] = sc[i].second;
for (int i = time(); i --> t;)
suma = ss[i];
st.resize(t);
sc.resize(t);
ss.resize(t);
}
bool join(int a, int b) {
a = find(a), b = find(b);
if (a == b) return false;
if (e[a] > e[b]) swap(a, b);
st.push_back({a, e[a]});
st.push_back({b, e[b]});
sc.push_back({a, c[a]});
sc.push_back({b, c[b]});
ss.push_back(suma); ss.push_back(suma);
if (c[a]) suma -= -e[a];
if (c[b]) suma -= -e[b];
e[a] += e[b]; e[b] = a;
c[a] |= c[b];
if (c[a]) suma += -e[a];
return true;
}
void wlacz(int a) {
a = find(a);
if (!c[a]) {
st.push_back({a, e[a]}); st.push_back({a, e[a]});
sc.push_back({a, c[a]}); sc.push_back({a, c[a]});
ss.push_back(suma); ss.push_back(suma);
suma += -e[a];
c[a] = 1;
}
}
};
const int Q = 75075;
int n, m, k, q;
vi wlacz[2 * Q];
vector<pii> dodaj[2 * Q];
int odp[Q];
ll id(int i, int j) { return 1ll * m * i + j; }
map<ll, int> mp; // unordered_map
int nri(ll x) {
if (!mp.count(x)) return mp[x] = sz(mp);
return mp[x];
}
int nr(int i, int j) { return nri(id(i, j)); }
void wrzuc(int u, int lo, int hi, int l, int r, auto& gdzie, auto co) {
if (l <= lo && hi <= r) {
gdzie[u].push_back(co);
return;
}
int mid = (lo + hi) / 2;
if (l < mid) wrzuc(u + 1, lo, mid, l, r, gdzie, co);
if (r > mid) wrzuc(u + (mid - lo) * 2, mid, hi, l, r, gdzie, co);
}
// krawedzie istnieja jezeli obaj nie sa w 2x2
// jest wlaczany jezeli ma legalny ruch
set<ll> s; // wszyscy
map<ll, int> nie, ruch; // nie w 2x2, wlaczone
void flip(int x, int y, int t) {
auto id2 = [&](int i, int j) { return id(x - 2 + i, y - 2 + j); };
auto nr2 = [&](int i, int j) { return nr(x - 2 + i, y - 2 + j); };
// zczytujemy terazniejszy grid
int g[5][5] = {};
rep(i, 0, 5) {
int xx = x - 2 + i;
if (xx >= 0 && xx < n) {
auto it = s.lower_bound(id(xx, max(0, y - 2)));
while (it != s.end()) {
int xi = *it / m, yi = *it % m;
if (xi != xx || yi > min(m - 1, y + 2)) break;
g[i][yi - (y - 2)] = 1;
it++;
}
}
}
// zczytujemy od kiedy nie sa czescia kwadratu 2x2
int dwa[5][5];
rep(i, 0, 5) rep(j, 0, 5) dwa[i][j] = -1;
rep(i, 0, 5) {
int xx = x - 2 + i;
if (xx >= 0 && xx < n) {
auto it = nie.lower_bound(id(xx, max(0, y - 2)));
while (it != nie.end()) {
int xi = it->first / m, yi = it->first % m;
if (xi != xx || yi > min(m - 1, y + 2)) break;
dwa[i][yi - (y - 2)] = it->second;
it++;
}
}
}
// zczytujemy czy moga zrobic ruch
bool czy[5][5] = {};
rep(i, 1, 4) rep(j, 1, 4) if (g[i][j]) {
czy[i][j] = (!g[i - 1][j] && !g[i + 1][j]) || (!g[i][j - 1] && !g[i][j + 1]);
}
// flip
int g2[5][5] = {};
rep(i, 0, 5) rep(j, 0, 5) g2[i][j] = g[i][j];
g2[2][2] ^= 1;
// czy po flipie jest w kwadr 2x2
bool dwa2[5][5] = {};
rep(i, 0, 4) rep(j, 0, 4) {
if (g2[i][j] + g2[i + 1][j] + g2[i][j + 1] + g2[i + 1][j + 1] == 4) {
dwa2[i][j] = dwa2[i + 1][j] = dwa2[i][j + 1] = dwa2[i + 1][j + 1] = 1;
}
}
rep(i, 0, 5) {
if (dwa[i][0] == -1) dwa2[i][0] = 1;
if (dwa[0][i] == -1) dwa2[0][i] = 1;
if (dwa[i][4] == -1) dwa2[i][4] = 1;
if (dwa[4][i] == -1) dwa2[4][i] = 1;
}
// czy po flipie moze zrobic ruch
bool czy2[5][5] = {};
rep(i, 1, 4) rep(j, 1, 4) if (g2[i][j]) {
czy2[i][j] = (!g2[i - 1][j] && !g2[i + 1][j]) || (!g2[i][j - 1] && !g2[i][j + 1]);
}
// porownojemy
// poziome
rep(i, 1, 4) rep(j, 0, 4) {
bool wcz = g[i][j] && g[i][j + 1] && dwa[i][j] != -1 && dwa[i][j + 1] != -1;
bool ter = g2[i][j] && g2[i][j + 1] && !dwa2[i][j] && !dwa2[i][j + 1];
if (wcz && !ter) {
int kiedy = max(dwa[i][j], dwa[i][j + 1]);
wrzuc(0, 0, q, kiedy, t, dodaj, pii(nr2(i, j), nr2(i, j + 1)));
}
}
// pionowe
rep(i, 0, 4) rep(j, 1, 4) {
bool wcz = g[i][j] && g[i + 1][j] && dwa[i][j] != -1 && dwa[i + 1][j] != -1;
bool ter = g2[i][j] && g2[i + 1][j] && !dwa2[i][j] && !dwa2[i + 1][j];
if (wcz && !ter) {
int kiedy = max(dwa[i][j], dwa[i + 1][j]);
wrzuc(0, 0, q, kiedy, t, dodaj, pii(nr2(i, j), nr2(i + 1, j)));
}
}
// updatujemy nie
rep(i, 1, 4) rep(j, 1, 4) {
bool wcz = g[i][j] && dwa[i][j] != -1;
bool ter = g2[i][j] && !dwa2[i][j];
if (wcz && !ter) {
nie.erase(id2(i, j));
} else if (!wcz && ter) {
//debug(x - 2 + i, y - 2 + j);
nie[id2(i, j)] = t;
}
}
// ruchy
rep(i, 1, 4) rep(j, 1, 4) {
if (czy[i][j] == 0 && czy2[i][j] == 1) {
ruch[id2(i, j)] = t;
} else if (czy[i][j] == 1 && czy2[i][j] == 0) {
wrzuc(0, 0, q, ruch[id2(i, j)], t, wlacz, nr2(i, j));
ruch.erase(id2(i, j));
}
}
if (s.contains(id(x, y))) s.erase(id(x, y));
else s.insert(id(x, y));
}
RollbackUF uf(0);
void dfs(int u, int lo, int hi) {
int t = uf.time();
for (auto [i, j] : dodaj[u]) uf.join(i, j);
for (int i : wlacz[u]) uf.wlacz(i);
for (int i : wlacz[u]) uf.wlacz(i);
if (lo + 1 == hi) {
odp[lo] = uf.suma;
uf.rollback(t);
return;
}
int mid = (lo + hi) / 2;
dfs(u + 1, lo, mid);
dfs(u + (mid - lo) * 2, mid, hi);
uf.rollback(t);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m >> k >> q;
q++;
rep(i, 0, k) {
int x, y;
cin >> x >> y;
x--; y--;
flip(x, y, 0);
}
rep(i, 1, q) {
int x, y;
cin >> x >> y;
x--; y--;
flip(x, y, i);
}
for (auto [i, j] : ruch) wrzuc(0, 0, q, j, q, wlacz, nri(i));
for (auto [i, j] : nie) {
int x = i / m, y = i % m;
rep(dx, -1, 2) rep(dy, -1, 2) if (abs(dx) + abs(dy) == 1) {
int u = x + dx, v = y + dy;
if (u >= 0 && u < n && v >= 0 && v < m) {
ll ii = id(u, v);
if (nie.count(ii)) {
int kiedy = max(j, nie[ii]);
wrzuc(0, 0, q, kiedy, q, dodaj, pii(nr(x, y), nr(u, v)));
}
}
}
}
uf = RollbackUF(sz(mp));
dfs(0, 0, q);
rep(i, 0, q) cout << odp[i] << '\n';
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = (a); i < (b); i++) #define all(x) begin(x), end(x) #define sz(x) int((x).size()) using ll = long long; using pii = pair<int, int>; using vi = vector<int>; #ifdef LOCAL auto operator<<(auto& o, auto x) -> decltype(x.first, o); auto operator<<(auto& o, auto x) -> decltype(x.end(), o) { o << "{"; for (int i = 0; auto y : x) o << ", " + !i++ * 2 << y; return o << "}"; } auto operator<<(auto& o, auto x) -> decltype(x.first, o) { return o << "(" << x.first << ", " << x.second << ")"; } void __print(auto... x) { ((cerr << x << " "), ...) << endl; } #define debug(x...) __print("[" #x "]:", x) #else #define debug(...) 2137 #endif struct RollbackUF { vi e; vector<pii> st; vi c; vector<pii> sc; int suma = 0; vi ss; RollbackUF(int n) : e(n, -1), c(n, 0) {} int size(int x) { return -e[find(x)]; } int find(int x) { return e[x] < 0 ? x : find(e[x]); } int time() { return sz(st); } void rollback(int t) { for (int i = time(); i --> t;) e[st[i].first] = st[i].second; for (int i = time(); i --> t;) c[sc[i].first] = sc[i].second; for (int i = time(); i --> t;) suma = ss[i]; st.resize(t); sc.resize(t); ss.resize(t); } bool join(int a, int b) { a = find(a), b = find(b); if (a == b) return false; if (e[a] > e[b]) swap(a, b); st.push_back({a, e[a]}); st.push_back({b, e[b]}); sc.push_back({a, c[a]}); sc.push_back({b, c[b]}); ss.push_back(suma); ss.push_back(suma); if (c[a]) suma -= -e[a]; if (c[b]) suma -= -e[b]; e[a] += e[b]; e[b] = a; c[a] |= c[b]; if (c[a]) suma += -e[a]; return true; } void wlacz(int a) { a = find(a); if (!c[a]) { st.push_back({a, e[a]}); st.push_back({a, e[a]}); sc.push_back({a, c[a]}); sc.push_back({a, c[a]}); ss.push_back(suma); ss.push_back(suma); suma += -e[a]; c[a] = 1; } } }; const int Q = 75075; int n, m, k, q; vi wlacz[2 * Q]; vector<pii> dodaj[2 * Q]; int odp[Q]; ll id(int i, int j) { return 1ll * m * i + j; } map<ll, int> mp; // unordered_map int nri(ll x) { if (!mp.count(x)) return mp[x] = sz(mp); return mp[x]; } int nr(int i, int j) { return nri(id(i, j)); } void wrzuc(int u, int lo, int hi, int l, int r, auto& gdzie, auto co) { if (l <= lo && hi <= r) { gdzie[u].push_back(co); return; } int mid = (lo + hi) / 2; if (l < mid) wrzuc(u + 1, lo, mid, l, r, gdzie, co); if (r > mid) wrzuc(u + (mid - lo) * 2, mid, hi, l, r, gdzie, co); } // krawedzie istnieja jezeli obaj nie sa w 2x2 // jest wlaczany jezeli ma legalny ruch set<ll> s; // wszyscy map<ll, int> nie, ruch; // nie w 2x2, wlaczone void flip(int x, int y, int t) { auto id2 = [&](int i, int j) { return id(x - 2 + i, y - 2 + j); }; auto nr2 = [&](int i, int j) { return nr(x - 2 + i, y - 2 + j); }; // zczytujemy terazniejszy grid int g[5][5] = {}; rep(i, 0, 5) { int xx = x - 2 + i; if (xx >= 0 && xx < n) { auto it = s.lower_bound(id(xx, max(0, y - 2))); while (it != s.end()) { int xi = *it / m, yi = *it % m; if (xi != xx || yi > min(m - 1, y + 2)) break; g[i][yi - (y - 2)] = 1; it++; } } } // zczytujemy od kiedy nie sa czescia kwadratu 2x2 int dwa[5][5]; rep(i, 0, 5) rep(j, 0, 5) dwa[i][j] = -1; rep(i, 0, 5) { int xx = x - 2 + i; if (xx >= 0 && xx < n) { auto it = nie.lower_bound(id(xx, max(0, y - 2))); while (it != nie.end()) { int xi = it->first / m, yi = it->first % m; if (xi != xx || yi > min(m - 1, y + 2)) break; dwa[i][yi - (y - 2)] = it->second; it++; } } } // zczytujemy czy moga zrobic ruch bool czy[5][5] = {}; rep(i, 1, 4) rep(j, 1, 4) if (g[i][j]) { czy[i][j] = (!g[i - 1][j] && !g[i + 1][j]) || (!g[i][j - 1] && !g[i][j + 1]); } // flip int g2[5][5] = {}; rep(i, 0, 5) rep(j, 0, 5) g2[i][j] = g[i][j]; g2[2][2] ^= 1; // czy po flipie jest w kwadr 2x2 bool dwa2[5][5] = {}; rep(i, 0, 4) rep(j, 0, 4) { if (g2[i][j] + g2[i + 1][j] + g2[i][j + 1] + g2[i + 1][j + 1] == 4) { dwa2[i][j] = dwa2[i + 1][j] = dwa2[i][j + 1] = dwa2[i + 1][j + 1] = 1; } } rep(i, 0, 5) { if (dwa[i][0] == -1) dwa2[i][0] = 1; if (dwa[0][i] == -1) dwa2[0][i] = 1; if (dwa[i][4] == -1) dwa2[i][4] = 1; if (dwa[4][i] == -1) dwa2[4][i] = 1; } // czy po flipie moze zrobic ruch bool czy2[5][5] = {}; rep(i, 1, 4) rep(j, 1, 4) if (g2[i][j]) { czy2[i][j] = (!g2[i - 1][j] && !g2[i + 1][j]) || (!g2[i][j - 1] && !g2[i][j + 1]); } // porownojemy // poziome rep(i, 1, 4) rep(j, 0, 4) { bool wcz = g[i][j] && g[i][j + 1] && dwa[i][j] != -1 && dwa[i][j + 1] != -1; bool ter = g2[i][j] && g2[i][j + 1] && !dwa2[i][j] && !dwa2[i][j + 1]; if (wcz && !ter) { int kiedy = max(dwa[i][j], dwa[i][j + 1]); wrzuc(0, 0, q, kiedy, t, dodaj, pii(nr2(i, j), nr2(i, j + 1))); } } // pionowe rep(i, 0, 4) rep(j, 1, 4) { bool wcz = g[i][j] && g[i + 1][j] && dwa[i][j] != -1 && dwa[i + 1][j] != -1; bool ter = g2[i][j] && g2[i + 1][j] && !dwa2[i][j] && !dwa2[i + 1][j]; if (wcz && !ter) { int kiedy = max(dwa[i][j], dwa[i + 1][j]); wrzuc(0, 0, q, kiedy, t, dodaj, pii(nr2(i, j), nr2(i + 1, j))); } } // updatujemy nie rep(i, 1, 4) rep(j, 1, 4) { bool wcz = g[i][j] && dwa[i][j] != -1; bool ter = g2[i][j] && !dwa2[i][j]; if (wcz && !ter) { nie.erase(id2(i, j)); } else if (!wcz && ter) { //debug(x - 2 + i, y - 2 + j); nie[id2(i, j)] = t; } } // ruchy rep(i, 1, 4) rep(j, 1, 4) { if (czy[i][j] == 0 && czy2[i][j] == 1) { ruch[id2(i, j)] = t; } else if (czy[i][j] == 1 && czy2[i][j] == 0) { wrzuc(0, 0, q, ruch[id2(i, j)], t, wlacz, nr2(i, j)); ruch.erase(id2(i, j)); } } if (s.contains(id(x, y))) s.erase(id(x, y)); else s.insert(id(x, y)); } RollbackUF uf(0); void dfs(int u, int lo, int hi) { int t = uf.time(); for (auto [i, j] : dodaj[u]) uf.join(i, j); for (int i : wlacz[u]) uf.wlacz(i); for (int i : wlacz[u]) uf.wlacz(i); if (lo + 1 == hi) { odp[lo] = uf.suma; uf.rollback(t); return; } int mid = (lo + hi) / 2; dfs(u + 1, lo, mid); dfs(u + (mid - lo) * 2, mid, hi); uf.rollback(t); } int main() { cin.tie(0)->sync_with_stdio(0); cin >> n >> m >> k >> q; q++; rep(i, 0, k) { int x, y; cin >> x >> y; x--; y--; flip(x, y, 0); } rep(i, 1, q) { int x, y; cin >> x >> y; x--; y--; flip(x, y, i); } for (auto [i, j] : ruch) wrzuc(0, 0, q, j, q, wlacz, nri(i)); for (auto [i, j] : nie) { int x = i / m, y = i % m; rep(dx, -1, 2) rep(dy, -1, 2) if (abs(dx) + abs(dy) == 1) { int u = x + dx, v = y + dy; if (u >= 0 && u < n && v >= 0 && v < m) { ll ii = id(u, v); if (nie.count(ii)) { int kiedy = max(j, nie[ii]); wrzuc(0, 0, q, kiedy, q, dodaj, pii(nr(x, y), nr(u, v))); } } } } uf = RollbackUF(sz(mp)); dfs(0, 0, q); rep(i, 0, q) cout << odp[i] << '\n'; } |
English