#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <vector> #include <set> #include <map> #include <array> #include <random> #include <cmath> #include <chrono> #include <list> #include <ctime> #include <sstream> #include <queue> #include <climits> #include <stack> #include <valarray> #include <random> #include <bitset> #include <numeric> #include <iomanip> #include <cassert> using namespace std; typedef vector<int> vi; typedef pair<int,int> pii; typedef long long ll; #define rep(x, b, e) for(int x=(b); x<(e); ++x) #define trav(a, x) for(auto& a : x) #define ford(x, b, e) for(int x=((int)(b))-1; x>=(e); --x) #define all(c) c.begin(),c.end() #define sz(x) ((int)((x).size())) #define pb push_back #define eb emplace_back #define st first #define nd second #define mp(x,y) make_pair(x,y) typedef short int sint; template<typename T> bool ckmin(T& a, const T& b){return b<a?a=b,1:0;} template<typename T> bool ckmax(T& a, const T& b){return b>a?a=b,1:0;} template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string& s) { return '"' + s + '"'; } string to_string(char c) { return string(1, c); } string to_string(const char* s) { return to_string((string) s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } 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; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif // #include <atcoder/modint> // using namespace atcoder; // using mint = modint998244353; // modint1000000007; // typedef vector<mint> vmi; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // use rng() to get unsigned int // mt19937_64 for random long longs const int Q = 75002; int n, m, k, q; vector<pii> evs; int covered[Q]; int total[Q]; int contributed[2][Q]; int iteracja; set<pii> g; set<pii> pokryte; int ile; int contribution; map<int, set<tuple<int, int, int>>> diags; bool fullSquare(int x, int y) { rep(i, 0, 2) rep(j, 0, 2) { if (g.count({x + i, y + j}) == 0) return false; } return true; } bool inBounds(int x, int y) { if (x < 1 || x > n || y < 1 || y > m) return false; return true; } bool isOnSquare(int x, int y) { if (!inBounds(x, y)) return false; rep(i, 0, 2) rep(j, 0, 2) { if (fullSquare(x - i, y - j)) return true; } return false; } tuple<int, int, int> addToDiags(int x, int y) { int diag = x + y; auto &elems = diags[diag]; // debug("add to diags: ", x, y, elems); auto p = elems.lower_bound({ y, 0, 0 }); if (p == elems.end()) { // no one after } auto q = p; if (q != elems.begin()) { q = prev(q); } else { q = elems.end(); } // q ; p // both must be non-contributing, or too far int start = y, koniec = y; if (q != elems.end() && get<1>(*q) + 1 == start) { start = get<0>(*q); elems.erase(q); } if (p != elems.end() && get<0>(*p) == koniec + 1) { // erase p koniec = get<1>(*p); elems.erase(p); } tuple<int, int, int> result{start, koniec, diag}; elems.insert(result); return result; } tuple<int, int, int> removeDiag(int x, int y) { int diag = x + y; auto &elems = diags[diag]; // debug("remove diagonal impl", x, y, elems); auto p = prev(elems.upper_bound({y + 1, 0, 0})); auto res = *p; // this is what I removed and need to re-check for contributions elems.erase(p); if (get<0>(res) < y) { // need to push element on the left elems.insert({ get<0>(res), y - 1, diag }); } if (get<1>(res) > y) { elems.insert({ y + 1, get<1>(res), diag }); } return res; } // returns the full size of the contribution int checkContribution(pii pokryty, int kierunek, tuple<int, int, int> segment) { int myLength = get<1>(segment) - get<0>(segment) + 1; auto &coveredDiag = diags[ pokryty.st + pokryty.nd ]; pii whereToCheck; int diagLen = 0; if (kierunek < 2) { auto p = coveredDiag.lower_bound({ get<0>(segment), 0, 0 }); // going upper right if (p != coveredDiag.end() && get<0>(*p) == get<0>(segment) + 1 - kierunek) { debug(" coveredDiag: ", *p, coveredDiag); diagLen = get<1>(*p) - get<0>(*p) + 1; } if (!( myLength == diagLen || myLength == diagLen + 1 )) { return 0; } // nice! there is a change if (myLength == diagLen) { if (kierunek == 0) { whereToCheck = { pokryty.st - myLength - 1, pokryty.nd + myLength}; } else { // kierunek = 1 whereToCheck = { pokryty.st - myLength, pokryty.nd + myLength + 1 }; } } else { // myLength = diagLen + 1 whereToCheck = { pokryty.st - myLength, pokryty.nd + myLength }; } } else { if (kierunek == 2) { auto p = coveredDiag.lower_bound({ get<1>(segment), 0, 0 }); if (p != coveredDiag.begin() && get<1>(*prev(p)) + 1 == get<1>(segment)) { --p; diagLen = get<1>(*p) - get<0>(*p) + 1; } } else { auto p = coveredDiag.lower_bound({ get<1>(segment) + 1, 0, 0 }); if (p != coveredDiag.begin() && get<1>(*prev(p)) == get<1>(segment)) { --p; diagLen = get<1>(*p) - get<0>(*p) + 1; } } debug(myLength, diagLen, pokryty); if (!( myLength == diagLen || myLength == diagLen + 1 )) { return 0; } // nice! there is a change if (myLength == diagLen) { if (kierunek == 2) { whereToCheck = { pokryty.st + myLength + 1, pokryty.nd - myLength}; } else { // kierunek = 3 whereToCheck = { pokryty.st + myLength, pokryty.nd - myLength - 1 }; } } else { // my longer // myLength = diagLen + 1 whereToCheck = { pokryty.st + myLength, pokryty.nd - myLength }; } } if (pokryte.count(whereToCheck) == 0) { return 0; } // yes contribution!!!! debug(" contributions impl", pokryty, kierunek, segment, myLength, diagLen, whereToCheck); return myLength + diagLen; } int checkOnlyDiagonal(tuple<int, int, int> segment) { debug("check only diagonal!", segment); // check above, check below // above int diag = get<2>(segment); int myLength = get<1>(segment) - get<0>(segment) + 1; auto p = diags[ diag - 1 ].lower_bound({ get<0>(segment) - 1, 0, 0 }); if (p != diags[diag - 1].end())debug(" above", *p); if ( p != diags[ diag - 1].end() && get<0>(*p) + 1 == get<0>(segment) && get<1>(*p) == get<1>(segment)) { // ching ching return 2 * myLength + 1; } p = diags[ diag + 1 ].lower_bound({ get<0>(segment), 0, 0 }); if ( p != diags[diag + 1].end() && get<0>(*p) == get<0>(segment) && get<1>(*p) - 1 == get<1>(segment) ) { return 2 * myLength + 1; } return 0; } int countContributions(tuple<int, int, int> segment) { int res = 0; int x1 = get<2>(segment) - get<0>(segment); int x2 = get<2>(segment) - get<1>(segment); int y1 = get<0>(segment); int y2 = get<1>(segment); vector<pii> items = { { x1 + 1, y1 }, { x1, y1 - 1 }, { x2 - 1, y2 }, { x2, y2 + 1 }, }; rep(dir, 0, sz(items)) { if (!pokryte.count(items[dir])) continue; res = checkContribution(items[dir], dir, segment); if (res) break; } if (res == 0 && pokryte.count({ x1 + 1, y1 -1 }) && pokryte.count({ x2 - 1, y2 + 1 })) { res = checkOnlyDiagonal(segment); } if (res) { debug(" have contributions for segment", segment, res); } return res; } int countContributions(pii xy) { int res = 0; int diag = xy.st + xy.nd; auto p = diags[diag - 1].lower_bound({xy.nd, 0, 0}); if (p != diags[diag - 1].end() && get<0>(*p) == xy.nd) { res += checkContribution(xy, 0, *p); } p = diags[diag + 1].lower_bound({ xy.nd + 1, 0, 0 }); if (p != diags[diag + 1].end() && get<0>(*p) == xy.nd + 1) { res += checkContribution(xy, 1, *p); } p = diags[diag + 1].upper_bound({ xy.nd + 1, 0, 0 }); if (p != diags[diag + 1].begin() && get<1>(*prev(p)) == xy.nd ) { res += checkContribution(xy, 2, *prev(p)); } debug("diags above: ", diags[diag - 1], xy.nd); p = diags[diag - 1].upper_bound({ xy.nd, 0, 0 }); if (p != diags[diag - 1].begin()) debug(*prev(p)); if (p != diags[diag - 1].begin() && get<1>(*prev(p)) == xy.nd - 1) { debug("checking contrubution"); res += checkContribution(xy, 3, *prev(p)); } if (res != 0) { debug(" have contribution for covered", xy, res); } return res; } void usunNiepokryte(int x, int y) { debug(" usun niepokryte", x, y); auto moja = removeDiag(x, y); // debug(" the removed diagonal: ", moja); contribution -= countContributions(moja); } void dodajNiepokryte(int x, int y) { debug(" Dodaj nie pokryte", x, y); auto moja = addToDiags(x, y); debug(" the added diagonal", moja); contribution += countContributions(moja); } void usunPokryte(int x, int y) { // subtract contributions debug(" usun pokryte", x, y); contribution -= countContributions({ x, y }); pokryte.erase({ x, y }); } void dodajPokryte(int x, int y) { debug(" dodaj pokryte", x, y); pokryte.insert({ x, y }); // add contributions contribution += countContributions({ x, y }); } void addBlock(int x, int y) { debug(" Add block: ", x, y); g.insert({x, y}); ++ile; vector<pii> nowo_pokryte; rep(i, -1, 2) { rep(j, -1, 2) { if (!inBounds(x + i, y + j)) continue; if (pokryte.count({x + i, y + j})) continue; if (isOnSquare(x + i, y + j)) { nowo_pokryte.eb(x + i, y + j); } } } debug(nowo_pokryte); if (sz(nowo_pokryte)) { for (auto [i, j] : nowo_pokryte) { if (i == x && j == y) { continue; } usunNiepokryte(i, j); } for (auto [i, j]: nowo_pokryte) { dodajPokryte(i, j); } } else { dodajNiepokryte(x, y); } } void removeBlock(int x, int y) { debug(" Remove block", x, y); g.erase({x, y}); --ile; vector<pii> nowo_niepokryte; rep(i, -1, 2) { rep(j, -1, 2) { if (!inBounds(x + i, y + j)) continue; if (pokryte.count({x + i, y + j}) == 0) continue; if (!isOnSquare(x + i, y + j)) { nowo_niepokryte.eb(x + i, y + j); } } } debug(" ", nowo_niepokryte); if (sz(nowo_niepokryte)) { // including myself for (auto [i, j] : nowo_niepokryte) { usunPokryte(i, j); } for (auto [i, j] : nowo_niepokryte) { if (i == x && j == y) continue; dodajNiepokryte(i, j); } } else { usunNiepokryte(x, y); } } void handle(int x, int y) { if (g.count({x, y})) { removeBlock(x, y); } else { addBlock(x, y); } } void rozwiaz() { g.clear(); diags.clear(); pokryte.clear(); ile = 0; contribution = 0; rep(i, 0, k) { addBlock(evs[i].st, evs[i].nd); } // debug("dodalem podstawe"); for (int test = 0; test < q; ++test) { auto [x, y] = evs[k + test]; handle(x, y); total[test] = ile; covered[test] = sz(pokryte); contributed[iteracja][test] = contribution; } } void solve() { cin >> n >> m >> k >> q; evs.resize(k + q); rep(i, 0, k + q) { cin >> evs[i].st >> evs[i].nd; } --k; ++q; debug("read input"); for (iteracja = 0; iteracja < 2; ++iteracja) { rozwiaz(); rep(i, 0, k + q) { evs[i] = {evs[i].nd, n - evs[i].st + 1}; } // debug(evs); swap(n, m); debug("skonczylem iteracje"); } rep(i, 0, q) { cout << (total[i] - covered[i] - contributed[0][i] - contributed[1][i]) << endl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t; // cin >> t; t = 1; while (t--) { solve(); } }
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 | #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <vector> #include <set> #include <map> #include <array> #include <random> #include <cmath> #include <chrono> #include <list> #include <ctime> #include <sstream> #include <queue> #include <climits> #include <stack> #include <valarray> #include <random> #include <bitset> #include <numeric> #include <iomanip> #include <cassert> using namespace std; typedef vector<int> vi; typedef pair<int,int> pii; typedef long long ll; #define rep(x, b, e) for(int x=(b); x<(e); ++x) #define trav(a, x) for(auto& a : x) #define ford(x, b, e) for(int x=((int)(b))-1; x>=(e); --x) #define all(c) c.begin(),c.end() #define sz(x) ((int)((x).size())) #define pb push_back #define eb emplace_back #define st first #define nd second #define mp(x,y) make_pair(x,y) typedef short int sint; template<typename T> bool ckmin(T& a, const T& b){return b<a?a=b,1:0;} template<typename T> bool ckmax(T& a, const T& b){return b>a?a=b,1:0;} template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string& s) { return '"' + s + '"'; } string to_string(char c) { return string(1, c); } string to_string(const char* s) { return to_string((string) s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } 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; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif // #include <atcoder/modint> // using namespace atcoder; // using mint = modint998244353; // modint1000000007; // typedef vector<mint> vmi; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // use rng() to get unsigned int // mt19937_64 for random long longs const int Q = 75002; int n, m, k, q; vector<pii> evs; int covered[Q]; int total[Q]; int contributed[2][Q]; int iteracja; set<pii> g; set<pii> pokryte; int ile; int contribution; map<int, set<tuple<int, int, int>>> diags; bool fullSquare(int x, int y) { rep(i, 0, 2) rep(j, 0, 2) { if (g.count({x + i, y + j}) == 0) return false; } return true; } bool inBounds(int x, int y) { if (x < 1 || x > n || y < 1 || y > m) return false; return true; } bool isOnSquare(int x, int y) { if (!inBounds(x, y)) return false; rep(i, 0, 2) rep(j, 0, 2) { if (fullSquare(x - i, y - j)) return true; } return false; } tuple<int, int, int> addToDiags(int x, int y) { int diag = x + y; auto &elems = diags[diag]; // debug("add to diags: ", x, y, elems); auto p = elems.lower_bound({ y, 0, 0 }); if (p == elems.end()) { // no one after } auto q = p; if (q != elems.begin()) { q = prev(q); } else { q = elems.end(); } // q ; p // both must be non-contributing, or too far int start = y, koniec = y; if (q != elems.end() && get<1>(*q) + 1 == start) { start = get<0>(*q); elems.erase(q); } if (p != elems.end() && get<0>(*p) == koniec + 1) { // erase p koniec = get<1>(*p); elems.erase(p); } tuple<int, int, int> result{start, koniec, diag}; elems.insert(result); return result; } tuple<int, int, int> removeDiag(int x, int y) { int diag = x + y; auto &elems = diags[diag]; // debug("remove diagonal impl", x, y, elems); auto p = prev(elems.upper_bound({y + 1, 0, 0})); auto res = *p; // this is what I removed and need to re-check for contributions elems.erase(p); if (get<0>(res) < y) { // need to push element on the left elems.insert({ get<0>(res), y - 1, diag }); } if (get<1>(res) > y) { elems.insert({ y + 1, get<1>(res), diag }); } return res; } // returns the full size of the contribution int checkContribution(pii pokryty, int kierunek, tuple<int, int, int> segment) { int myLength = get<1>(segment) - get<0>(segment) + 1; auto &coveredDiag = diags[ pokryty.st + pokryty.nd ]; pii whereToCheck; int diagLen = 0; if (kierunek < 2) { auto p = coveredDiag.lower_bound({ get<0>(segment), 0, 0 }); // going upper right if (p != coveredDiag.end() && get<0>(*p) == get<0>(segment) + 1 - kierunek) { debug(" coveredDiag: ", *p, coveredDiag); diagLen = get<1>(*p) - get<0>(*p) + 1; } if (!( myLength == diagLen || myLength == diagLen + 1 )) { return 0; } // nice! there is a change if (myLength == diagLen) { if (kierunek == 0) { whereToCheck = { pokryty.st - myLength - 1, pokryty.nd + myLength}; } else { // kierunek = 1 whereToCheck = { pokryty.st - myLength, pokryty.nd + myLength + 1 }; } } else { // myLength = diagLen + 1 whereToCheck = { pokryty.st - myLength, pokryty.nd + myLength }; } } else { if (kierunek == 2) { auto p = coveredDiag.lower_bound({ get<1>(segment), 0, 0 }); if (p != coveredDiag.begin() && get<1>(*prev(p)) + 1 == get<1>(segment)) { --p; diagLen = get<1>(*p) - get<0>(*p) + 1; } } else { auto p = coveredDiag.lower_bound({ get<1>(segment) + 1, 0, 0 }); if (p != coveredDiag.begin() && get<1>(*prev(p)) == get<1>(segment)) { --p; diagLen = get<1>(*p) - get<0>(*p) + 1; } } debug(myLength, diagLen, pokryty); if (!( myLength == diagLen || myLength == diagLen + 1 )) { return 0; } // nice! there is a change if (myLength == diagLen) { if (kierunek == 2) { whereToCheck = { pokryty.st + myLength + 1, pokryty.nd - myLength}; } else { // kierunek = 3 whereToCheck = { pokryty.st + myLength, pokryty.nd - myLength - 1 }; } } else { // my longer // myLength = diagLen + 1 whereToCheck = { pokryty.st + myLength, pokryty.nd - myLength }; } } if (pokryte.count(whereToCheck) == 0) { return 0; } // yes contribution!!!! debug(" contributions impl", pokryty, kierunek, segment, myLength, diagLen, whereToCheck); return myLength + diagLen; } int checkOnlyDiagonal(tuple<int, int, int> segment) { debug("check only diagonal!", segment); // check above, check below // above int diag = get<2>(segment); int myLength = get<1>(segment) - get<0>(segment) + 1; auto p = diags[ diag - 1 ].lower_bound({ get<0>(segment) - 1, 0, 0 }); if (p != diags[diag - 1].end())debug(" above", *p); if ( p != diags[ diag - 1].end() && get<0>(*p) + 1 == get<0>(segment) && get<1>(*p) == get<1>(segment)) { // ching ching return 2 * myLength + 1; } p = diags[ diag + 1 ].lower_bound({ get<0>(segment), 0, 0 }); if ( p != diags[diag + 1].end() && get<0>(*p) == get<0>(segment) && get<1>(*p) - 1 == get<1>(segment) ) { return 2 * myLength + 1; } return 0; } int countContributions(tuple<int, int, int> segment) { int res = 0; int x1 = get<2>(segment) - get<0>(segment); int x2 = get<2>(segment) - get<1>(segment); int y1 = get<0>(segment); int y2 = get<1>(segment); vector<pii> items = { { x1 + 1, y1 }, { x1, y1 - 1 }, { x2 - 1, y2 }, { x2, y2 + 1 }, }; rep(dir, 0, sz(items)) { if (!pokryte.count(items[dir])) continue; res = checkContribution(items[dir], dir, segment); if (res) break; } if (res == 0 && pokryte.count({ x1 + 1, y1 -1 }) && pokryte.count({ x2 - 1, y2 + 1 })) { res = checkOnlyDiagonal(segment); } if (res) { debug(" have contributions for segment", segment, res); } return res; } int countContributions(pii xy) { int res = 0; int diag = xy.st + xy.nd; auto p = diags[diag - 1].lower_bound({xy.nd, 0, 0}); if (p != diags[diag - 1].end() && get<0>(*p) == xy.nd) { res += checkContribution(xy, 0, *p); } p = diags[diag + 1].lower_bound({ xy.nd + 1, 0, 0 }); if (p != diags[diag + 1].end() && get<0>(*p) == xy.nd + 1) { res += checkContribution(xy, 1, *p); } p = diags[diag + 1].upper_bound({ xy.nd + 1, 0, 0 }); if (p != diags[diag + 1].begin() && get<1>(*prev(p)) == xy.nd ) { res += checkContribution(xy, 2, *prev(p)); } debug("diags above: ", diags[diag - 1], xy.nd); p = diags[diag - 1].upper_bound({ xy.nd, 0, 0 }); if (p != diags[diag - 1].begin()) debug(*prev(p)); if (p != diags[diag - 1].begin() && get<1>(*prev(p)) == xy.nd - 1) { debug("checking contrubution"); res += checkContribution(xy, 3, *prev(p)); } if (res != 0) { debug(" have contribution for covered", xy, res); } return res; } void usunNiepokryte(int x, int y) { debug(" usun niepokryte", x, y); auto moja = removeDiag(x, y); // debug(" the removed diagonal: ", moja); contribution -= countContributions(moja); } void dodajNiepokryte(int x, int y) { debug(" Dodaj nie pokryte", x, y); auto moja = addToDiags(x, y); debug(" the added diagonal", moja); contribution += countContributions(moja); } void usunPokryte(int x, int y) { // subtract contributions debug(" usun pokryte", x, y); contribution -= countContributions({ x, y }); pokryte.erase({ x, y }); } void dodajPokryte(int x, int y) { debug(" dodaj pokryte", x, y); pokryte.insert({ x, y }); // add contributions contribution += countContributions({ x, y }); } void addBlock(int x, int y) { debug(" Add block: ", x, y); g.insert({x, y}); ++ile; vector<pii> nowo_pokryte; rep(i, -1, 2) { rep(j, -1, 2) { if (!inBounds(x + i, y + j)) continue; if (pokryte.count({x + i, y + j})) continue; if (isOnSquare(x + i, y + j)) { nowo_pokryte.eb(x + i, y + j); } } } debug(nowo_pokryte); if (sz(nowo_pokryte)) { for (auto [i, j] : nowo_pokryte) { if (i == x && j == y) { continue; } usunNiepokryte(i, j); } for (auto [i, j]: nowo_pokryte) { dodajPokryte(i, j); } } else { dodajNiepokryte(x, y); } } void removeBlock(int x, int y) { debug(" Remove block", x, y); g.erase({x, y}); --ile; vector<pii> nowo_niepokryte; rep(i, -1, 2) { rep(j, -1, 2) { if (!inBounds(x + i, y + j)) continue; if (pokryte.count({x + i, y + j}) == 0) continue; if (!isOnSquare(x + i, y + j)) { nowo_niepokryte.eb(x + i, y + j); } } } debug(" ", nowo_niepokryte); if (sz(nowo_niepokryte)) { // including myself for (auto [i, j] : nowo_niepokryte) { usunPokryte(i, j); } for (auto [i, j] : nowo_niepokryte) { if (i == x && j == y) continue; dodajNiepokryte(i, j); } } else { usunNiepokryte(x, y); } } void handle(int x, int y) { if (g.count({x, y})) { removeBlock(x, y); } else { addBlock(x, y); } } void rozwiaz() { g.clear(); diags.clear(); pokryte.clear(); ile = 0; contribution = 0; rep(i, 0, k) { addBlock(evs[i].st, evs[i].nd); } // debug("dodalem podstawe"); for (int test = 0; test < q; ++test) { auto [x, y] = evs[k + test]; handle(x, y); total[test] = ile; covered[test] = sz(pokryte); contributed[iteracja][test] = contribution; } } void solve() { cin >> n >> m >> k >> q; evs.resize(k + q); rep(i, 0, k + q) { cin >> evs[i].st >> evs[i].nd; } --k; ++q; debug("read input"); for (iteracja = 0; iteracja < 2; ++iteracja) { rozwiaz(); rep(i, 0, k + q) { evs[i] = {evs[i].nd, n - evs[i].st + 1}; } // debug(evs); swap(n, m); debug("skonczylem iteracje"); } rep(i, 0, q) { cout << (total[i] - covered[i] - contributed[0][i] - contributed[1][i]) << endl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t; // cin >> t; t = 1; while (t--) { solve(); } } |