//#pragma GCC optimize("Ofast") //#pragma GCC optimize ("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma warning(disable:4786) #pragma warning(disable:4996) #include <bits/stdc++.h> using namespace std; #define MEM(a, b) memset(a, (b), sizeof(a)) #define CLR(a) memset(a, 0, sizeof(a)) #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define ABS(X) ( (X) > 0 ? (X) : ( -(X) ) ) #define S(X) ( (X) * (X) ) #define SZ(V) (int )V.size() #define FORN(i, n) for(int i = 0; i < n; i++) #define FORAB(i, a, b) for(int i = a; i <= b; i++) #define ALL(V) V.begin(), V.end() #define IN(A, B, C) ((B) <= (A) && (A) <= (C)) #define AIN(A, B, C) assert(IN(A, B, C)) string to_string(string s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto& x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } void dbg_out() { cerr << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << to_string(H); dbg_out(T...); } #ifdef DBG_PRINT #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) #else #define dbg(...) 0 #endif template<class... T> void input(T&... a) { (cin >> ... >> a); } void printsuc(int suc){ if (suc == 1) cout << "\n"; if (suc == 2) cout << " "; } template<class t> void print_single(t x, int suc = 1){ cout << x; printsuc(suc); } template<class t, class u> void print_single(const pair<t, u>& p, int suc = 1) { print_single(p.first, 2); print_single(p.second, suc); } template<class T> void print_single(const vector<T>& V, int suc = 1) { FORN(i, V.size()) print_single(V[i], i == int(V.size()) -1 ? 3 : 2); printsuc(suc); } template<class T, size_t N> void print_single(const array<T, N>& V, int suc = 1) { FORN(i, N) print_single(V[i], i == int(N) - 1 ? 3 : 2); printsuc(suc); } template<class T> void print(const T& t) { print_single(t); } template<class T, class ...Args> void print(const T& t ,const Args&... args) { print_single(t, 2); print(args...); } // Alias function that calls print template<class ...Args> void output(const Args&... args) { print(args...); } typedef long long int LL; typedef __int128 i128; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; typedef pair<LL, int> PLI; typedef pair<double, double> PDD; typedef vector<int> VI; typedef vector<LL> VL; typedef vector<PLL> VPL; typedef vector<PII> VP; typedef vector<double> VD; typedef vector<vector<int>> VVI; typedef vector<vector<LL>> VVL; typedef vector<string> VS; typedef long double ld; typedef unsigned long long ULL; //#define MAXN 1000 //#define MAXN2 MAXN*MAXN mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // mt19937 rng(10); // shuffle(V.begin(), V.end(), rng); // int u = rng(); // int u = uniform_int_distribution<long long>(L, R)(rng); // double u = uniform_real_distribution<float>(L, R)(rng); // URDL // int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1}; // int dc[] = {0, 1, 0, -1, 1, -1, 1, -1}; //const LL MOD = 1000000007; const LL MOD = 998244353; // const LL INF = 2000000000000000001LL; //2e18 + 1 int glbl = 0; void UpdateGlobal(PII cnt, int u) { if (cnt.first) glbl += u * (cnt.first + cnt.second); } struct UnionFindUndo { vector< int > data; VP cnt_color; stack< pair< int, int > > history; stack<array<int, 3>> history_cnt_color; UnionFindUndo() {} void resize(int sz) { data.assign(sz, -1); cnt_color.resize(sz, {0, 0}); } bool unite(int x, int y) { x = find(x), y = find(y); if(data[x] > data[y]) swap(x, y); history.emplace(x, data[x]); history.emplace(y, data[y]); if(x == y) { return (false); } history_cnt_color.emplace(array<int, 3>({y, cnt_color[y].first, cnt_color[y].second})); history_cnt_color.emplace(array<int, 3>({x, cnt_color[x].first, cnt_color[x].second})); UpdateGlobal(cnt_color[x], -1); UpdateGlobal(cnt_color[y], -1); cnt_color[x].first += cnt_color[y].first; cnt_color[x].second += cnt_color[y].second; UpdateGlobal(cnt_color[x], +1); data[x] += data[y]; data[y] = x; return (true); } void color(int id, int new_color) { int par = find(id); history_cnt_color.emplace(array<int, 3>({par, cnt_color[par].first, cnt_color[par].second})); UpdateGlobal(cnt_color[par], -1); if (new_color == 1) cnt_color[par].first++; else if (new_color == 2) cnt_color[par].second++; UpdateGlobal(cnt_color[par], +1); } int find(int k) { if(data[k] < 0) return (k); return (find(data[k])); } int size(int k) { return (-data[find(k)]); } void undo_unite() { auto p1 = history.top(); data[p1.first] = p1.second; history.pop(); auto p2 = history.top(); data[p2.first] = p2.second; history.pop(); if (p1.first == p2.first) return; auto a = history_cnt_color.top(); UpdateGlobal(cnt_color[a[0]], -1); cnt_color[a[0]] = {a[1], a[2]}; UpdateGlobal(cnt_color[a[0]], +1); history_cnt_color.pop(); a = history_cnt_color.top(); cnt_color[a[0]] = {a[1], a[2]}; UpdateGlobal(cnt_color[a[0]], +1); history_cnt_color.pop(); } void undo_color() { auto a = history_cnt_color.top(); UpdateGlobal(cnt_color[a[0]], -1); cnt_color[a[0]] = {a[1], a[2]}; UpdateGlobal(cnt_color[a[0]], +1); history_cnt_color.pop(); } }; struct pair_hash { std::size_t operator () (const std::pair<int, int> &p) const { return p.first * 200001LL + p.second; } }; unordered_map<PII, int, pair_hash> cell_ids; // first = time. // second = state. // 0 = inactive, 1 = it has two opposite free // 2 = it does not have opposite free, 3 = belongs to 2x2. PII state[150005]; int ids[5][5], filled[5][5]; int new_state[5][5]; struct Event { int type = 0; int t1, t2; int u, v; void print() { dbg(type, t1, t2, u, v); } }; vector<Event> events; void canonical_form(int& id1, int& id2) { if (id1 > id2) swap(id1, id2); } unordered_map<PII, int, pair_hash> appear; void add_edge(int id1, int id2, int eventT) { canonical_form(id1, id2); appear[{id1, id2}] = eventT; } void remove_edge(int id1, int id2, int eventT) { canonical_form(id1, id2); events.push_back({1, appear[{id1, id2}], eventT - 1, id1, id2}); appear.erase({id1, id2}); } void change_state(int id, int new_state, int eventT) { events.push_back({2, state[id].first, eventT - 1, id, state[id].second}); state[id] = {eventT, new_state}; } void process_remaining(int eventT) { for (auto& p : appear) { events.push_back({1, p.second, eventT, p.first.first, p.first.second}); } appear.clear(); for (int id = 0; id < cell_ids.size(); id++) { events.push_back({2, state[id].first, eventT, id, state[id].second}); state[id] = {-1, -1}; } } VVI st; void st_add(int at, int l, int r, int t1, int t2, int event_id) { if (t1 <= l && r <= t2) { st[at].push_back(event_id); return; } if (t2 < l || t1 > r) return; int m = (l + r) / 2; st_add(2 * at, l, m, t1, t2, event_id); st_add(2 * at + 1, m + 1, r, t1, t2, event_id); } UnionFindUndo uf; int N; void build_st() { st.resize(4 * (N + 1) + 1); FORN(i, SZ(events)) { auto& e = events[i]; st_add(1, 0, N, e.t1, e.t2, i); } uf.resize(cell_ids.size()); } void run(int at, int l, int r, const function<void(int)>& f) { for (int i : st[at]) { const auto& event = events[i]; if (event.type == 1) { uf.unite(event.u, event.v); } else { uf.color(event.u, event.v); } } if (l == r) { f(l); } else { int m = (l + r) / 2; run(at * 2, l, m, f); run(at * 2 + 1, m + 1, r, f); } for (int i = SZ(st[at]) - 1; i >= 0; i--) { const auto& event = events[st[at][i]]; if (event.type == 1) { uf.undo_unite(); } else { uf.undo_color(); } } } void flip(int eventT, PII& p) { for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { int r = p.first + i - 2; int c = p.second + j - 2; auto it = cell_ids.find({r, c}); if (it == cell_ids.end()) { ids[i][j] = -1; filled[i][j] = 0; new_state[i][j] = 0; } else { ids[i][j] = it->second; filled[i][j] = (state[ids[i][j]].second > 0); new_state[i][j] = 4; // tbd } } } filled[2][2] ^= 1; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (filled[i][j] + filled[i + 1][j] + filled[i][j + 1] + filled[i + 1][j + 1] == 4) { new_state[i][j] = new_state[i + 1][j] = new_state[i][j + 1] = new_state[i + 1][j + 1] = 3; } } } for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { if (i == 0 || j == 0 || i == 4 || j == 4) { new_state[i][j] = (ids[i][j] != -1 ? state[ids[i][j]].second : 0); } if (filled[i][j] == 0 || new_state[i][j] == 3) continue; if (!filled[i - 1][j] && !filled[i + 1][j]) new_state[i][j] = 1; else if (!filled[i][j - 1] && !filled[i][j + 1]) new_state[i][j] = 1; else new_state[i][j] = 2; } } auto check_edge = [](int st1, int st2) -> int { return IN(st1, 1, 2) && IN(st2, 1, 2); }; FORN(i, 5) FORN(j, 5) { if (ids[i][j] < 0) continue; if (new_state[i][j] == 4) new_state[i][j] = 0; int ni = i, nj = j + 1; if (nj < 5 && ids[ni][nj] >= 0) { int past = check_edge(state[ids[i][j]].second, state[ids[ni][nj]].second); int future = check_edge(new_state[i][j], new_state[ni][nj]); if (!past && future) add_edge(ids[i][j], ids[ni][nj], eventT); if (past && !future) remove_edge(ids[i][j], ids[ni][nj], eventT); } ni = i + 1, nj = j; if (ni < 5 && ids[ni][nj] >= 0) { int past = check_edge(state[ids[i][j]].second, state[ids[ni][nj]].second); int future = check_edge(new_state[i][j], new_state[ni][nj]); if (!past && future) add_edge(ids[i][j], ids[ni][nj], eventT); if (past && !future) remove_edge(ids[i][j], ids[ni][nj], eventT); } if (state[ids[i][j]].second != new_state[i][j] && IN(i, 1, 3) && IN(j, 1, 3)) { change_state(ids[i][j], new_state[i][j], eventT); } } } void solve(int ks) { int n, m, init, q; input(n, m, init, q); VP inits(init); FORN(i, init) input(inits[i].first, inits[i].second); VP queries(q); FORN(i, q) input(queries[i].first, queries[i].second); { int cell_id = 0; for (auto& p : inits) if (!cell_ids.contains(p)) cell_ids[p] = cell_id++; for (auto& p : queries) if (!cell_ids.contains(p)) cell_ids[p] = cell_id++; for (int i = 0; i < cell_id; i++) state[i] = {0, 0}; } VI collect_ans; int eventT = 0; for (auto& p : inits) { eventT++; flip(eventT, p); } collect_ans.push_back(eventT); for (auto& p : queries) { eventT++; flip(eventT, p); collect_ans.push_back(eventT); } process_remaining(eventT); N = eventT; // for (auto e : events) e.print(); build_st(); int at = 0; VI ans; run(1, 0, eventT, [&](int t) { if (at < collect_ans.size() && collect_ans[at] == t) { ans.push_back(glbl); at++; } }); for (int a : ans) output(a); } void gen() { } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); std::cout << std::setprecision(10); double start_time = clock(); #ifdef LOCAL freopen("C:\\Home\\Contest\\sample.in", "r", stdin); // freopen("C:\Home\Contest\0.out", "w", stdout); #endif gen(); if (0) { int T; input(T); for (int ks = 1; ks <= T; ks++) { solve(ks); if (ks % 100 == 0) dbg(ks, " done"); // double time_elapsed = (clock() - start_time) / CLOCKS_PER_SEC; // dbg(ks, time_elapsed); } } else { solve(1); } double TimeElapsed = (clock() - start_time) / CLOCKS_PER_SEC; dbg(TimeElapsed); return 0; }
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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 | //#pragma GCC optimize("Ofast") //#pragma GCC optimize ("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma warning(disable:4786) #pragma warning(disable:4996) #include <bits/stdc++.h> using namespace std; #define MEM(a, b) memset(a, (b), sizeof(a)) #define CLR(a) memset(a, 0, sizeof(a)) #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define ABS(X) ( (X) > 0 ? (X) : ( -(X) ) ) #define S(X) ( (X) * (X) ) #define SZ(V) (int )V.size() #define FORN(i, n) for(int i = 0; i < n; i++) #define FORAB(i, a, b) for(int i = a; i <= b; i++) #define ALL(V) V.begin(), V.end() #define IN(A, B, C) ((B) <= (A) && (A) <= (C)) #define AIN(A, B, C) assert(IN(A, B, C)) string to_string(string s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto& x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } void dbg_out() { cerr << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << to_string(H); dbg_out(T...); } #ifdef DBG_PRINT #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) #else #define dbg(...) 0 #endif template<class... T> void input(T&... a) { (cin >> ... >> a); } void printsuc(int suc){ if (suc == 1) cout << "\n"; if (suc == 2) cout << " "; } template<class t> void print_single(t x, int suc = 1){ cout << x; printsuc(suc); } template<class t, class u> void print_single(const pair<t, u>& p, int suc = 1) { print_single(p.first, 2); print_single(p.second, suc); } template<class T> void print_single(const vector<T>& V, int suc = 1) { FORN(i, V.size()) print_single(V[i], i == int(V.size()) -1 ? 3 : 2); printsuc(suc); } template<class T, size_t N> void print_single(const array<T, N>& V, int suc = 1) { FORN(i, N) print_single(V[i], i == int(N) - 1 ? 3 : 2); printsuc(suc); } template<class T> void print(const T& t) { print_single(t); } template<class T, class ...Args> void print(const T& t ,const Args&... args) { print_single(t, 2); print(args...); } // Alias function that calls print template<class ...Args> void output(const Args&... args) { print(args...); } typedef long long int LL; typedef __int128 i128; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; typedef pair<LL, int> PLI; typedef pair<double, double> PDD; typedef vector<int> VI; typedef vector<LL> VL; typedef vector<PLL> VPL; typedef vector<PII> VP; typedef vector<double> VD; typedef vector<vector<int>> VVI; typedef vector<vector<LL>> VVL; typedef vector<string> VS; typedef long double ld; typedef unsigned long long ULL; //#define MAXN 1000 //#define MAXN2 MAXN*MAXN mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // mt19937 rng(10); // shuffle(V.begin(), V.end(), rng); // int u = rng(); // int u = uniform_int_distribution<long long>(L, R)(rng); // double u = uniform_real_distribution<float>(L, R)(rng); // URDL // int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1}; // int dc[] = {0, 1, 0, -1, 1, -1, 1, -1}; //const LL MOD = 1000000007; const LL MOD = 998244353; // const LL INF = 2000000000000000001LL; //2e18 + 1 int glbl = 0; void UpdateGlobal(PII cnt, int u) { if (cnt.first) glbl += u * (cnt.first + cnt.second); } struct UnionFindUndo { vector< int > data; VP cnt_color; stack< pair< int, int > > history; stack<array<int, 3>> history_cnt_color; UnionFindUndo() {} void resize(int sz) { data.assign(sz, -1); cnt_color.resize(sz, {0, 0}); } bool unite(int x, int y) { x = find(x), y = find(y); if(data[x] > data[y]) swap(x, y); history.emplace(x, data[x]); history.emplace(y, data[y]); if(x == y) { return (false); } history_cnt_color.emplace(array<int, 3>({y, cnt_color[y].first, cnt_color[y].second})); history_cnt_color.emplace(array<int, 3>({x, cnt_color[x].first, cnt_color[x].second})); UpdateGlobal(cnt_color[x], -1); UpdateGlobal(cnt_color[y], -1); cnt_color[x].first += cnt_color[y].first; cnt_color[x].second += cnt_color[y].second; UpdateGlobal(cnt_color[x], +1); data[x] += data[y]; data[y] = x; return (true); } void color(int id, int new_color) { int par = find(id); history_cnt_color.emplace(array<int, 3>({par, cnt_color[par].first, cnt_color[par].second})); UpdateGlobal(cnt_color[par], -1); if (new_color == 1) cnt_color[par].first++; else if (new_color == 2) cnt_color[par].second++; UpdateGlobal(cnt_color[par], +1); } int find(int k) { if(data[k] < 0) return (k); return (find(data[k])); } int size(int k) { return (-data[find(k)]); } void undo_unite() { auto p1 = history.top(); data[p1.first] = p1.second; history.pop(); auto p2 = history.top(); data[p2.first] = p2.second; history.pop(); if (p1.first == p2.first) return; auto a = history_cnt_color.top(); UpdateGlobal(cnt_color[a[0]], -1); cnt_color[a[0]] = {a[1], a[2]}; UpdateGlobal(cnt_color[a[0]], +1); history_cnt_color.pop(); a = history_cnt_color.top(); cnt_color[a[0]] = {a[1], a[2]}; UpdateGlobal(cnt_color[a[0]], +1); history_cnt_color.pop(); } void undo_color() { auto a = history_cnt_color.top(); UpdateGlobal(cnt_color[a[0]], -1); cnt_color[a[0]] = {a[1], a[2]}; UpdateGlobal(cnt_color[a[0]], +1); history_cnt_color.pop(); } }; struct pair_hash { std::size_t operator () (const std::pair<int, int> &p) const { return p.first * 200001LL + p.second; } }; unordered_map<PII, int, pair_hash> cell_ids; // first = time. // second = state. // 0 = inactive, 1 = it has two opposite free // 2 = it does not have opposite free, 3 = belongs to 2x2. PII state[150005]; int ids[5][5], filled[5][5]; int new_state[5][5]; struct Event { int type = 0; int t1, t2; int u, v; void print() { dbg(type, t1, t2, u, v); } }; vector<Event> events; void canonical_form(int& id1, int& id2) { if (id1 > id2) swap(id1, id2); } unordered_map<PII, int, pair_hash> appear; void add_edge(int id1, int id2, int eventT) { canonical_form(id1, id2); appear[{id1, id2}] = eventT; } void remove_edge(int id1, int id2, int eventT) { canonical_form(id1, id2); events.push_back({1, appear[{id1, id2}], eventT - 1, id1, id2}); appear.erase({id1, id2}); } void change_state(int id, int new_state, int eventT) { events.push_back({2, state[id].first, eventT - 1, id, state[id].second}); state[id] = {eventT, new_state}; } void process_remaining(int eventT) { for (auto& p : appear) { events.push_back({1, p.second, eventT, p.first.first, p.first.second}); } appear.clear(); for (int id = 0; id < cell_ids.size(); id++) { events.push_back({2, state[id].first, eventT, id, state[id].second}); state[id] = {-1, -1}; } } VVI st; void st_add(int at, int l, int r, int t1, int t2, int event_id) { if (t1 <= l && r <= t2) { st[at].push_back(event_id); return; } if (t2 < l || t1 > r) return; int m = (l + r) / 2; st_add(2 * at, l, m, t1, t2, event_id); st_add(2 * at + 1, m + 1, r, t1, t2, event_id); } UnionFindUndo uf; int N; void build_st() { st.resize(4 * (N + 1) + 1); FORN(i, SZ(events)) { auto& e = events[i]; st_add(1, 0, N, e.t1, e.t2, i); } uf.resize(cell_ids.size()); } void run(int at, int l, int r, const function<void(int)>& f) { for (int i : st[at]) { const auto& event = events[i]; if (event.type == 1) { uf.unite(event.u, event.v); } else { uf.color(event.u, event.v); } } if (l == r) { f(l); } else { int m = (l + r) / 2; run(at * 2, l, m, f); run(at * 2 + 1, m + 1, r, f); } for (int i = SZ(st[at]) - 1; i >= 0; i--) { const auto& event = events[st[at][i]]; if (event.type == 1) { uf.undo_unite(); } else { uf.undo_color(); } } } void flip(int eventT, PII& p) { for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { int r = p.first + i - 2; int c = p.second + j - 2; auto it = cell_ids.find({r, c}); if (it == cell_ids.end()) { ids[i][j] = -1; filled[i][j] = 0; new_state[i][j] = 0; } else { ids[i][j] = it->second; filled[i][j] = (state[ids[i][j]].second > 0); new_state[i][j] = 4; // tbd } } } filled[2][2] ^= 1; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (filled[i][j] + filled[i + 1][j] + filled[i][j + 1] + filled[i + 1][j + 1] == 4) { new_state[i][j] = new_state[i + 1][j] = new_state[i][j + 1] = new_state[i + 1][j + 1] = 3; } } } for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { if (i == 0 || j == 0 || i == 4 || j == 4) { new_state[i][j] = (ids[i][j] != -1 ? state[ids[i][j]].second : 0); } if (filled[i][j] == 0 || new_state[i][j] == 3) continue; if (!filled[i - 1][j] && !filled[i + 1][j]) new_state[i][j] = 1; else if (!filled[i][j - 1] && !filled[i][j + 1]) new_state[i][j] = 1; else new_state[i][j] = 2; } } auto check_edge = [](int st1, int st2) -> int { return IN(st1, 1, 2) && IN(st2, 1, 2); }; FORN(i, 5) FORN(j, 5) { if (ids[i][j] < 0) continue; if (new_state[i][j] == 4) new_state[i][j] = 0; int ni = i, nj = j + 1; if (nj < 5 && ids[ni][nj] >= 0) { int past = check_edge(state[ids[i][j]].second, state[ids[ni][nj]].second); int future = check_edge(new_state[i][j], new_state[ni][nj]); if (!past && future) add_edge(ids[i][j], ids[ni][nj], eventT); if (past && !future) remove_edge(ids[i][j], ids[ni][nj], eventT); } ni = i + 1, nj = j; if (ni < 5 && ids[ni][nj] >= 0) { int past = check_edge(state[ids[i][j]].second, state[ids[ni][nj]].second); int future = check_edge(new_state[i][j], new_state[ni][nj]); if (!past && future) add_edge(ids[i][j], ids[ni][nj], eventT); if (past && !future) remove_edge(ids[i][j], ids[ni][nj], eventT); } if (state[ids[i][j]].second != new_state[i][j] && IN(i, 1, 3) && IN(j, 1, 3)) { change_state(ids[i][j], new_state[i][j], eventT); } } } void solve(int ks) { int n, m, init, q; input(n, m, init, q); VP inits(init); FORN(i, init) input(inits[i].first, inits[i].second); VP queries(q); FORN(i, q) input(queries[i].first, queries[i].second); { int cell_id = 0; for (auto& p : inits) if (!cell_ids.contains(p)) cell_ids[p] = cell_id++; for (auto& p : queries) if (!cell_ids.contains(p)) cell_ids[p] = cell_id++; for (int i = 0; i < cell_id; i++) state[i] = {0, 0}; } VI collect_ans; int eventT = 0; for (auto& p : inits) { eventT++; flip(eventT, p); } collect_ans.push_back(eventT); for (auto& p : queries) { eventT++; flip(eventT, p); collect_ans.push_back(eventT); } process_remaining(eventT); N = eventT; // for (auto e : events) e.print(); build_st(); int at = 0; VI ans; run(1, 0, eventT, [&](int t) { if (at < collect_ans.size() && collect_ans[at] == t) { ans.push_back(glbl); at++; } }); for (int a : ans) output(a); } void gen() { } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); std::cout << std::setprecision(10); double start_time = clock(); #ifdef LOCAL freopen("C:\\Home\\Contest\\sample.in", "r", stdin); // freopen("C:\Home\Contest\0.out", "w", stdout); #endif gen(); if (0) { int T; input(T); for (int ks = 1; ks <= T; ks++) { solve(ks); if (ks % 100 == 0) dbg(ks, " done"); // double time_elapsed = (clock() - start_time) / CLOCKS_PER_SEC; // dbg(ks, time_elapsed); } } else { solve(1); } double TimeElapsed = (clock() - start_time) / CLOCKS_PER_SEC; dbg(TimeElapsed); return 0; } |