#ifndef LOC
#define NDEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
#define fwd(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) fwd(i, 0, n)
#define all(X) X.begin(), X.end()
#define sz(X) int(ssize(X))
#define pb push_back
#define eb emplace_back
#define st first
#define nd second
using pii = pair<int, int>; using vi = vector<int>;
using ll = long long; using ld = long double;
#ifdef LOC
auto SS = signal(6, [](int) { *(int *)0 = 0; });
#define DTP(x, y) auto operator<<(auto &o, auto a) -> decltype(y, o) { o << "("; x; return o << ")"; }
auto operator<<(auto &o, auto a) -> decltype(all(a), o);
DTP(o << a.st << ", " << a.nd, a.nd);
DTP(for (auto i : a) o << i << ", ", all(a));
#define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", [](auto... arg_) { (( cerr << arg_ << ", " ), ...) << '\n'; }(x)
#else
#define deb(...) 0
#endif
pii operator+(pii a, pii b) {
return {a.st + b.st, a.nd + b.nd};
}
namespace brute {
template <class T>
int solve(map<pii, T> mapka) {
int ans = 0, here = 1;
while (here) {
here = 0;
for (auto it = mapka.begin(); it != mapka.end();) {
pii coord = it->first;
bool rem = !mapka.contains(coord + pii{0, 1}) && !mapka.contains(coord + pii{0, -1});
rem |= !mapka.contains(coord + pii{1, 0}) && !mapka.contains(coord + pii{-1, 0});
if (rem) {
here = 1;
++ans;
it = mapka.erase(it);
}
else {
++it;
}
}
}
return ans;
}
}
enum NodeType {
PATH, CYCLE, HIGH, DISCO
};
bool areDiagonal(pii a, pii b) {
return abs(a.st - b.st) == 1 && abs(a.nd - b.nd) == 1;
}
int dist(pii a, pii b) {
return abs(a.st - b.st) + abs(a.nd - b.nd);
}
bool opposite(pii a, pii b) {
int x = abs(a.st - b.st), y = abs(a.nd - b.nd);
if (x == 2 && y == 0) return true;
if (x == 0 && y == 2) return true;
return false;
}
vi filterNodes(array<array<int, 3>, 3> nodes) {
vi v;
for (auto &a : nodes) for (int b : a) if (b != -1)
v.pb(b);
return v;
}
struct Treap {
int ANS = 0;
struct Node {
// E[0] = left child, E[1] = right child
// weight = node random weight (for treap)
// size = subtree size, par = parent node
int E[2] = {-1, -1}, weight = rand();
int size = 1, par = -1;
bool flip = 0; // Is interval reversed?
bool isActiv = true;
vi neis;
bool partOfAns = false;
pii xy{};
int typ = DISCO;
int partOfSquare = 0;
int threeInARow = 0;
int threeInARowOverChild = 0;
// void debnode(int i) {
// // deb(i, E[0], E[1], size, par, flip, isActiv);
// deb(i,neis, partOfAns, xy, typ, partOfSquare, threeInARow, threeInARowOverChild);
// }
};
// void debTreap() {
// rep(i, sz(G)) if (G[i].isActiv) {
// G[i].debnode(i);
// }
// assertTreap();
// }
// void assertTreap() {
// rep(i, sz(G)) if (G[i].isActiv) {
// assert(mapka.find(G[i].xy)->second == i);
// }
// }
void disconnectPath(int x, int y) {
// deb("disco", x, y);
int rx = root(x);
excludeFromAns(rx);
assert(rx == root(y));
if (G[rx].typ == CYCLE) {
int idx = index(x);
int idy = index(y);
assert(abs(idx - idy) == 1 || (min(idx, idy) == 0 && max(idx, idy)+1 == G[rx].size));
rx = shiftLeft(rx, idx); // x na pozycji 0
if (index(y) == 1)
rx = reverse(rx, 1, G[rx].size);
assert(index(y) + 1 == G[rx].size);
G[rx].typ = PATH;
return;
}
int idx = index(x);
int idy = index(y);
if (idx > idy) {
swap(idx, idy);
swap(x, y);
}
int _1, _2;
split(rx, _1, _2, idx + 1);
G[_1].typ = PATH;
G[_2].typ = PATH;
}
int shiftLeft(int root, int by) { // [by)[_) --> [_)[by)
int a, b;
split(root, a, b, by);
return join(b, a);
}
void connectPath(int x, int y) {
// deb(x, y);
int rx = root(x), ry = root(y);
excludeFromAns(rx);
excludeFromAns(ry);
if (rx == ry) {
assert(G[rx].typ == PATH);
G[rx].typ = CYCLE;
return;
}
flipToFront(x);
flipToFront(y);
G[rx].flip ^= 1;
join(rx, ry);
}
void disconnect(int x) { // also calls excludeFromAns
// if (G[x].typ == DISCO) {
// deb(x);
// cerr << "---\n";
// debTreap();
// exit(-1);
// }
assert(G[x].typ != DISCO);
int rt = root(x);
excludeFromAns(rt);
if (G[rt].size == 1) {
G[x].typ = DISCO;
return;
}
assert(G[x].typ != HIGH);
for (int i : G[x].neis) {
if (G[i].typ == DISCO || sz(G[i].neis) >= 3)
continue;
disconnectPath(i, x);
}
assert(G[x].par == -1 && G[x].size == 1);
G[x].typ = DISCO;
}
void reconnect(int x) { // does not call recheckPartOfAns
assert(G[x].typ == DISCO);
if (sz(G[x].neis) >= 3) {
G[x].typ = HIGH;
return;
}
G[x].threeInARow = 0;
if (sz(G[x].neis) == 2) {
int a = G[x].neis[0], b = G[x].neis[1];
if (opposite(G[a].xy, G[b].xy))
G[x].threeInARow = 1;
}
update(x);
assert(G[x].par == -1 && G[x].size == 1);
G[x].typ = PATH;
for (int i : G[x].neis) {
if (G[i].typ == DISCO || sz(G[i].neis) >= 3)
continue;
// debTreap();
// deb("\n");
connectPath(i, x);
}
assert(G[x].typ != DISCO);
}
bool areDiagonallySeparatedPartOfSquares(const vi &v) {
if (sz(v) != 2) return false;
int a = v[0], b = v[1];
return areDiagonal(G[a].xy, G[b].xy) && G[a].partOfSquare && G[b].partOfSquare;
}
void flipToFront(int i) {
if (index(i) == 0) return;
G[root(i)].flip ^= true;
assert(index(i) == 0);
}
bool isEndOfPathBlocked(int x) { // x is end of path
if (sz(G[x].neis) <= 1)
return false;
assert(sz(G[x].neis) == 2);
int a = G[x].neis[0], b = G[x].neis[1];
if (G[a].typ != PATH) swap(a, b);
assert(G[a].typ == PATH);
assert(G[b].typ == HIGH);
return areDiagonal(G[a].xy, G[b].xy) && G[b].partOfSquare;
}
void recheckPartOfAns(int x) {
assert(x == root(x));
bool newPartOfAns = true;
if (G[x].typ == PATH) {
if (G[x].size == 1) {
if (areDiagonallySeparatedPartOfSquares(G[x].neis))
newPartOfAns = false;
}
else {
int bg = find(x, 0);
int en = find(x, G[x].size - 1);
if (isEndOfPathBlocked(bg) && isEndOfPathBlocked(en) && G[x].threeInARowOverChild == 0)
newPartOfAns = false;
}
}
else if (G[x].typ == CYCLE) {
if (G[x].partOfSquare) newPartOfAns = false;
}
else if (G[x].typ == HIGH) {
if (G[x].partOfSquare) newPartOfAns = false;
}
else assert(false);
if (G[x].partOfAns == newPartOfAns)
return;
G[x].partOfAns = newPartOfAns;
if (newPartOfAns) ANS += G[x].size;
else ANS -= G[x].size;
}
void excludeFromAns(int x) {
assert(x == root(x));
if (G[x].partOfAns) {
G[x].partOfAns = false;
ANS -= G[x].size;
}
}
vi rootifyList(vi v) {
for (int &i : v) i = root(i);
sort(all(v));
v.erase(unique(all(v)), v.end());
return v;
}
vi freeLista;
map<pii, int> mapka;
vector<Node> G;
Treap(int n = 0) : G(n) {} // makes n disjoint nodes
int make() {
if (sz(freeLista)) {
int i = freeLista.back();
freeLista.pop_back();
G[i] = Node{};
return i;
}
G.pb({}); return sz(G)-1;
}
void addSquare(int px, int py) {
auto neis = getTabliceNei({px, py});
int x = make();
// deb(px, py, x);
G[x].xy = {px, py};
mapka[G[x].xy] = x;
neis[1][1] = x;
vi nonzero = filterNodes(neis);
// deb(neis, nonzero, x);
for (int i : nonzero) {
if (i != x) disconnect(i);
}
rep(i, 2) rep(j, 2) {
int tu = 0;
rep(a, 2) rep(b, 2)
tu += neis[i+a][j+b] != -1;
if (tu != 4) continue;
rep(a, 2) rep(b, 2) {
int id = neis[i+a][j+b];
G[id].partOfSquare++;
}
}
rep(i, 3) rep(j, 3) {
int id = neis[i][j];
if (id == -1) continue;
if (dist(G[id].xy, G[x].xy) == 1) {
G[id].neis.pb(x);
G[x].neis.pb(id);
}
}
// deb("\n przed reconnectami");
// debTreap();
// deb("\n");
for (int i : nonzero) reconnect(i);
for (int i : rootifyList(extlist(nonzero))) recheckPartOfAns(i);
}
void removeSquare(int x) {
assert(G[x].isActiv);
// deb(x);
auto neis = getTabliceNei(G[x].xy);
vi nonzero = filterNodes(neis);
for (int i : nonzero) disconnect(i);
rep(i, 2) rep(j, 2) {
int tu = 0;
rep(a, 2) rep(b, 2)
tu += neis[i+a][j+b] != -1;
if (tu != 4) continue;
rep(a, 2) rep(b, 2) {
int id = neis[i+a][j+b];
G[id].partOfSquare--;
}
}
rep(i, 3) rep(j, 3) {
int id = neis[i][j];
if (id == -1) continue;
if (dist(G[id].xy, G[x].xy) == 1) {
erase(G[id].neis, x);
}
}
mapka.erase(G[x].xy);
G[x].isActiv = false;
freeLista.pb(x);
erase(nonzero, x);
for (int i : nonzero) reconnect(i);
for (int i : rootifyList(extlist(nonzero))) recheckPartOfAns(i);
}
vi extlist(vi v) {
int osz = sz(v);
rep(i, osz) for (int j : G[v[i]].neis)
v.pb(j);
sort(all(v));
v.erase(unique(all(v)), v.end());
return v;
}
array<array<int, 3>, 3> getTabliceNei(pii xy) {
auto [x, y] = xy;
array<array<int, 3>, 3> r{};
rep(i, 3) {
rep(j, 3) r[i][j] = -1;
auto it = mapka.lower_bound({x - 1 + i, y - 1});
while (it != mapka.end() && it->first <= pii{x - 1 + i, y + 1}) {
r[i][it->first.nd + 1 - y] = it->second;
++it;
}
}
return r;
}
int getNodeAt(pii xy) {
auto it = mapka.find(xy);
if (it == end(mapka)) return -1;
return it->second;
}
void flipSquare(int x, int y) {
int id = getNodeAt({x, y});
if (id == -1)
addSquare(x, y);
else
removeSquare(id);
#ifdef LOC
// debTreap();
// int corrANS = brute::solve(mapka);
// if (corrANS != ANS) {
// deb(ANS, corrANS);
// exit(-1);
// }
#endif
}
int size(int x) { // subtree size of node x
return (x >= 0 ? G[x].size : 0);
}
void push(int x) { // x can be -1 !!!
if (x >= 0 && G[x].flip) {
G[x].flip = 0;
swap(G[x].E[0], G[x].E[1]);
for(auto e : G[x].E) if (e>=0) G[e].flip ^= 1;
} // + any other lazy operations
}
void update(int x) { // pull data up (reverse of push)
if (x >= 0) {
int& s = G[x].size = 1;
G[x].par = -1;
G[x].threeInARowOverChild = G[x].threeInARow;
for (auto e : G[x].E) if (e >= 0) {
s += G[e].size;
G[e].par = x;
G[x].threeInARowOverChild += G[e].threeInARowOverChild;
}
} // + any other aggregates
}
// Split treap x into treaps l and r
// such that l contains first i elements
// and r the remaining ones.
// x, l, r can be -1; time: ~O(lg n)
void split(int x, int& l, int& r, int i) {
push(x); l = r = -1;
if (x < 0) return;
int key = size(G[x].E[0]);
if (i <= key) {
split(G[x].E[0], l, G[x].E[0], i);
r = x;
} else {
split(G[x].E[1], G[x].E[1], r, i-key-1);
l = x;
}
update(x);
}
// Join treaps l and r into one treap left to right
// l, r and returned value can be -1.
int join(int l, int r) { // time: ~O(lg n)
push(l); push(r);
if (l < 0 || r < 0) return max(l, r);
if (G[l].weight < G[r].weight) {
G[l].E[1] = join(G[l].E[1], r);
update(l);
return l;
}
G[r].E[0] = join(l, G[r].E[0]);
update(r);
return r;
}
// Find i-th node in treap x.
// Returns its key or -1 if not found.
// x can be -1; time: ~O(lg n)
int find(int x, int i) {
while (x >= 0) {
push(x);
int key = size(G[x].E[0]);
if (key == i) return x;
x = G[x].E[key < i];
if (key < i) i -= key+1;
}
return -1;
}
// Get key of treap containing node x
// (key of treap root). x can be -1.
int root(int x) { // time: ~O(lg n)
while (G[x].par >= 0) x = G[x].par;
return x;
}
// Get position of node x in its treap.
// x is assumed to NOT be -1; time: ~O(lg n)
int index(int x) {
int p, i = size(G[x].E[G[x].flip]);
while ((p = G[x].par) >= 0) {
if (G[p].E[1] == x) i+=size(G[p].E[0])+1;
if (G[p].flip) i = G[p].size-i-1;
x = p;
}
return i;
}
// Reverse interval [l;r) in treap x.
// Returns new key of treap; time: ~O(lg n)
int reverse(int x, int l, int r) {
int a, b, c;
split(x, b, c, r);
split(b, a, b, l);
if (b >= 0) G[b].flip ^= 1;
return join(join(a, b), c);
}
};
void solve() {
Treap tr;
int n, m, k, q;
cin >> n >> m >> k >> q;
rep(id, k + q) {
int x, y;
cin >> x >> y;
tr.flipSquare(x, y);
if (id >= k - 1)
cout << tr.ANS << '\n';
}
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cout << fixed << setprecision(10);
// mt19937 rnd;
// Treap tr;
// rep(_, 150000)
// tr.flipSquare(rnd() % 15, rnd() % 15);
int z = 1;
// cin >> z;
rep(_, z) solve();
cout << flush;
_Exit(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 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 | #ifndef LOC #define NDEBUG #endif #include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i++) #define rep(i, n) fwd(i, 0, n) #define all(X) X.begin(), X.end() #define sz(X) int(ssize(X)) #define pb push_back #define eb emplace_back #define st first #define nd second using pii = pair<int, int>; using vi = vector<int>; using ll = long long; using ld = long double; #ifdef LOC auto SS = signal(6, [](int) { *(int *)0 = 0; }); #define DTP(x, y) auto operator<<(auto &o, auto a) -> decltype(y, o) { o << "("; x; return o << ")"; } auto operator<<(auto &o, auto a) -> decltype(all(a), o); DTP(o << a.st << ", " << a.nd, a.nd); DTP(for (auto i : a) o << i << ", ", all(a)); #define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", [](auto... arg_) { (( cerr << arg_ << ", " ), ...) << '\n'; }(x) #else #define deb(...) 0 #endif pii operator+(pii a, pii b) { return {a.st + b.st, a.nd + b.nd}; } namespace brute { template <class T> int solve(map<pii, T> mapka) { int ans = 0, here = 1; while (here) { here = 0; for (auto it = mapka.begin(); it != mapka.end();) { pii coord = it->first; bool rem = !mapka.contains(coord + pii{0, 1}) && !mapka.contains(coord + pii{0, -1}); rem |= !mapka.contains(coord + pii{1, 0}) && !mapka.contains(coord + pii{-1, 0}); if (rem) { here = 1; ++ans; it = mapka.erase(it); } else { ++it; } } } return ans; } } enum NodeType { PATH, CYCLE, HIGH, DISCO }; bool areDiagonal(pii a, pii b) { return abs(a.st - b.st) == 1 && abs(a.nd - b.nd) == 1; } int dist(pii a, pii b) { return abs(a.st - b.st) + abs(a.nd - b.nd); } bool opposite(pii a, pii b) { int x = abs(a.st - b.st), y = abs(a.nd - b.nd); if (x == 2 && y == 0) return true; if (x == 0 && y == 2) return true; return false; } vi filterNodes(array<array<int, 3>, 3> nodes) { vi v; for (auto &a : nodes) for (int b : a) if (b != -1) v.pb(b); return v; } struct Treap { int ANS = 0; struct Node { // E[0] = left child, E[1] = right child // weight = node random weight (for treap) // size = subtree size, par = parent node int E[2] = {-1, -1}, weight = rand(); int size = 1, par = -1; bool flip = 0; // Is interval reversed? bool isActiv = true; vi neis; bool partOfAns = false; pii xy{}; int typ = DISCO; int partOfSquare = 0; int threeInARow = 0; int threeInARowOverChild = 0; // void debnode(int i) { // // deb(i, E[0], E[1], size, par, flip, isActiv); // deb(i,neis, partOfAns, xy, typ, partOfSquare, threeInARow, threeInARowOverChild); // } }; // void debTreap() { // rep(i, sz(G)) if (G[i].isActiv) { // G[i].debnode(i); // } // assertTreap(); // } // void assertTreap() { // rep(i, sz(G)) if (G[i].isActiv) { // assert(mapka.find(G[i].xy)->second == i); // } // } void disconnectPath(int x, int y) { // deb("disco", x, y); int rx = root(x); excludeFromAns(rx); assert(rx == root(y)); if (G[rx].typ == CYCLE) { int idx = index(x); int idy = index(y); assert(abs(idx - idy) == 1 || (min(idx, idy) == 0 && max(idx, idy)+1 == G[rx].size)); rx = shiftLeft(rx, idx); // x na pozycji 0 if (index(y) == 1) rx = reverse(rx, 1, G[rx].size); assert(index(y) + 1 == G[rx].size); G[rx].typ = PATH; return; } int idx = index(x); int idy = index(y); if (idx > idy) { swap(idx, idy); swap(x, y); } int _1, _2; split(rx, _1, _2, idx + 1); G[_1].typ = PATH; G[_2].typ = PATH; } int shiftLeft(int root, int by) { // [by)[_) --> [_)[by) int a, b; split(root, a, b, by); return join(b, a); } void connectPath(int x, int y) { // deb(x, y); int rx = root(x), ry = root(y); excludeFromAns(rx); excludeFromAns(ry); if (rx == ry) { assert(G[rx].typ == PATH); G[rx].typ = CYCLE; return; } flipToFront(x); flipToFront(y); G[rx].flip ^= 1; join(rx, ry); } void disconnect(int x) { // also calls excludeFromAns // if (G[x].typ == DISCO) { // deb(x); // cerr << "---\n"; // debTreap(); // exit(-1); // } assert(G[x].typ != DISCO); int rt = root(x); excludeFromAns(rt); if (G[rt].size == 1) { G[x].typ = DISCO; return; } assert(G[x].typ != HIGH); for (int i : G[x].neis) { if (G[i].typ == DISCO || sz(G[i].neis) >= 3) continue; disconnectPath(i, x); } assert(G[x].par == -1 && G[x].size == 1); G[x].typ = DISCO; } void reconnect(int x) { // does not call recheckPartOfAns assert(G[x].typ == DISCO); if (sz(G[x].neis) >= 3) { G[x].typ = HIGH; return; } G[x].threeInARow = 0; if (sz(G[x].neis) == 2) { int a = G[x].neis[0], b = G[x].neis[1]; if (opposite(G[a].xy, G[b].xy)) G[x].threeInARow = 1; } update(x); assert(G[x].par == -1 && G[x].size == 1); G[x].typ = PATH; for (int i : G[x].neis) { if (G[i].typ == DISCO || sz(G[i].neis) >= 3) continue; // debTreap(); // deb("\n"); connectPath(i, x); } assert(G[x].typ != DISCO); } bool areDiagonallySeparatedPartOfSquares(const vi &v) { if (sz(v) != 2) return false; int a = v[0], b = v[1]; return areDiagonal(G[a].xy, G[b].xy) && G[a].partOfSquare && G[b].partOfSquare; } void flipToFront(int i) { if (index(i) == 0) return; G[root(i)].flip ^= true; assert(index(i) == 0); } bool isEndOfPathBlocked(int x) { // x is end of path if (sz(G[x].neis) <= 1) return false; assert(sz(G[x].neis) == 2); int a = G[x].neis[0], b = G[x].neis[1]; if (G[a].typ != PATH) swap(a, b); assert(G[a].typ == PATH); assert(G[b].typ == HIGH); return areDiagonal(G[a].xy, G[b].xy) && G[b].partOfSquare; } void recheckPartOfAns(int x) { assert(x == root(x)); bool newPartOfAns = true; if (G[x].typ == PATH) { if (G[x].size == 1) { if (areDiagonallySeparatedPartOfSquares(G[x].neis)) newPartOfAns = false; } else { int bg = find(x, 0); int en = find(x, G[x].size - 1); if (isEndOfPathBlocked(bg) && isEndOfPathBlocked(en) && G[x].threeInARowOverChild == 0) newPartOfAns = false; } } else if (G[x].typ == CYCLE) { if (G[x].partOfSquare) newPartOfAns = false; } else if (G[x].typ == HIGH) { if (G[x].partOfSquare) newPartOfAns = false; } else assert(false); if (G[x].partOfAns == newPartOfAns) return; G[x].partOfAns = newPartOfAns; if (newPartOfAns) ANS += G[x].size; else ANS -= G[x].size; } void excludeFromAns(int x) { assert(x == root(x)); if (G[x].partOfAns) { G[x].partOfAns = false; ANS -= G[x].size; } } vi rootifyList(vi v) { for (int &i : v) i = root(i); sort(all(v)); v.erase(unique(all(v)), v.end()); return v; } vi freeLista; map<pii, int> mapka; vector<Node> G; Treap(int n = 0) : G(n) {} // makes n disjoint nodes int make() { if (sz(freeLista)) { int i = freeLista.back(); freeLista.pop_back(); G[i] = Node{}; return i; } G.pb({}); return sz(G)-1; } void addSquare(int px, int py) { auto neis = getTabliceNei({px, py}); int x = make(); // deb(px, py, x); G[x].xy = {px, py}; mapka[G[x].xy] = x; neis[1][1] = x; vi nonzero = filterNodes(neis); // deb(neis, nonzero, x); for (int i : nonzero) { if (i != x) disconnect(i); } rep(i, 2) rep(j, 2) { int tu = 0; rep(a, 2) rep(b, 2) tu += neis[i+a][j+b] != -1; if (tu != 4) continue; rep(a, 2) rep(b, 2) { int id = neis[i+a][j+b]; G[id].partOfSquare++; } } rep(i, 3) rep(j, 3) { int id = neis[i][j]; if (id == -1) continue; if (dist(G[id].xy, G[x].xy) == 1) { G[id].neis.pb(x); G[x].neis.pb(id); } } // deb("\n przed reconnectami"); // debTreap(); // deb("\n"); for (int i : nonzero) reconnect(i); for (int i : rootifyList(extlist(nonzero))) recheckPartOfAns(i); } void removeSquare(int x) { assert(G[x].isActiv); // deb(x); auto neis = getTabliceNei(G[x].xy); vi nonzero = filterNodes(neis); for (int i : nonzero) disconnect(i); rep(i, 2) rep(j, 2) { int tu = 0; rep(a, 2) rep(b, 2) tu += neis[i+a][j+b] != -1; if (tu != 4) continue; rep(a, 2) rep(b, 2) { int id = neis[i+a][j+b]; G[id].partOfSquare--; } } rep(i, 3) rep(j, 3) { int id = neis[i][j]; if (id == -1) continue; if (dist(G[id].xy, G[x].xy) == 1) { erase(G[id].neis, x); } } mapka.erase(G[x].xy); G[x].isActiv = false; freeLista.pb(x); erase(nonzero, x); for (int i : nonzero) reconnect(i); for (int i : rootifyList(extlist(nonzero))) recheckPartOfAns(i); } vi extlist(vi v) { int osz = sz(v); rep(i, osz) for (int j : G[v[i]].neis) v.pb(j); sort(all(v)); v.erase(unique(all(v)), v.end()); return v; } array<array<int, 3>, 3> getTabliceNei(pii xy) { auto [x, y] = xy; array<array<int, 3>, 3> r{}; rep(i, 3) { rep(j, 3) r[i][j] = -1; auto it = mapka.lower_bound({x - 1 + i, y - 1}); while (it != mapka.end() && it->first <= pii{x - 1 + i, y + 1}) { r[i][it->first.nd + 1 - y] = it->second; ++it; } } return r; } int getNodeAt(pii xy) { auto it = mapka.find(xy); if (it == end(mapka)) return -1; return it->second; } void flipSquare(int x, int y) { int id = getNodeAt({x, y}); if (id == -1) addSquare(x, y); else removeSquare(id); #ifdef LOC // debTreap(); // int corrANS = brute::solve(mapka); // if (corrANS != ANS) { // deb(ANS, corrANS); // exit(-1); // } #endif } int size(int x) { // subtree size of node x return (x >= 0 ? G[x].size : 0); } void push(int x) { // x can be -1 !!! if (x >= 0 && G[x].flip) { G[x].flip = 0; swap(G[x].E[0], G[x].E[1]); for(auto e : G[x].E) if (e>=0) G[e].flip ^= 1; } // + any other lazy operations } void update(int x) { // pull data up (reverse of push) if (x >= 0) { int& s = G[x].size = 1; G[x].par = -1; G[x].threeInARowOverChild = G[x].threeInARow; for (auto e : G[x].E) if (e >= 0) { s += G[e].size; G[e].par = x; G[x].threeInARowOverChild += G[e].threeInARowOverChild; } } // + any other aggregates } // Split treap x into treaps l and r // such that l contains first i elements // and r the remaining ones. // x, l, r can be -1; time: ~O(lg n) void split(int x, int& l, int& r, int i) { push(x); l = r = -1; if (x < 0) return; int key = size(G[x].E[0]); if (i <= key) { split(G[x].E[0], l, G[x].E[0], i); r = x; } else { split(G[x].E[1], G[x].E[1], r, i-key-1); l = x; } update(x); } // Join treaps l and r into one treap left to right // l, r and returned value can be -1. int join(int l, int r) { // time: ~O(lg n) push(l); push(r); if (l < 0 || r < 0) return max(l, r); if (G[l].weight < G[r].weight) { G[l].E[1] = join(G[l].E[1], r); update(l); return l; } G[r].E[0] = join(l, G[r].E[0]); update(r); return r; } // Find i-th node in treap x. // Returns its key or -1 if not found. // x can be -1; time: ~O(lg n) int find(int x, int i) { while (x >= 0) { push(x); int key = size(G[x].E[0]); if (key == i) return x; x = G[x].E[key < i]; if (key < i) i -= key+1; } return -1; } // Get key of treap containing node x // (key of treap root). x can be -1. int root(int x) { // time: ~O(lg n) while (G[x].par >= 0) x = G[x].par; return x; } // Get position of node x in its treap. // x is assumed to NOT be -1; time: ~O(lg n) int index(int x) { int p, i = size(G[x].E[G[x].flip]); while ((p = G[x].par) >= 0) { if (G[p].E[1] == x) i+=size(G[p].E[0])+1; if (G[p].flip) i = G[p].size-i-1; x = p; } return i; } // Reverse interval [l;r) in treap x. // Returns new key of treap; time: ~O(lg n) int reverse(int x, int l, int r) { int a, b, c; split(x, b, c, r); split(b, a, b, l); if (b >= 0) G[b].flip ^= 1; return join(join(a, b), c); } }; void solve() { Treap tr; int n, m, k, q; cin >> n >> m >> k >> q; rep(id, k + q) { int x, y; cin >> x >> y; tr.flipSquare(x, y); if (id >= k - 1) cout << tr.ANS << '\n'; } } int32_t main() { cin.tie(0)->sync_with_stdio(0); cout << fixed << setprecision(10); // mt19937 rnd; // Treap tr; // rep(_, 150000) // tr.flipSquare(rnd() % 15, rnd() % 15); int z = 1; // cin >> z; rep(_, z) solve(); cout << flush; _Exit(0); } |
English