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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
#include "bits/stdc++.h"
using namespace std;

#define rep(i, b, e) for (int i = (b); i <= (e); i++)
#define per(i, b, e) for (int i = (e); i >= (b); i--)
#define FOR(i, b, e) rep(i, b, (e)-1)
#define SZ(x) int(x.size())
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define st first
#define nd second
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;

auto& operator<<(auto& o, pair<auto, auto> p) {
  return o << "(" << p.st << ", " << p.nd << ")";
}
auto operator<<(auto& o, auto x) -> decltype(end(x), o) {
  o << "{";
  int i = 0;
  for (auto e : x)
    o << ", " + 2 * !i++ << e;
  return o << "}";
}
#ifdef LOCAL
#define deb(x...) \
  // cerr << "[" #x "]: ", [](auto... $) { \
    ((// cerr << $ << "; "), ...) << endl; \
  }(x)
#else
#define deb(...)
#endif

struct node {
  node* f;     // Father of the node
  node* c[2];  // Children of the node
  pii val;
  int cnt, sum;
  int size;  // Size of the subtree of the node
  bool wliczam;

  node(pii val_)
      : f(nullptr), val(val_), cnt(0), sum(0), size(1), wliczam(false) {
    c[0] = nullptr;
    c[1] = nullptr;
  }

  // Re-caculate the size.
  void update() {
    size = 1;
    sum = cnt;
    for (int t = 0; t < 2; ++t) {
      if (c[t]) {
        size += c[t]->size;
        sum += c[t]->sum;
        wliczam |= c[t]->wliczam;
        c[t]->wliczam = false;
      }
    }
  }
};

void rotate(node* n) {
  // v: is n the left child or the right child?
  int v = n->f->c[0] == n;
  node *p = n->f, *m = n->c[v];
  if (p->f)
    p->f->c[p->f->c[1] == p] = n;
  n->f = p->f, n->c[v] = p;
  p->f = n, p->c[v ^ 1] = m;
  if (m)
    m->f = p;
  // Update the sizes.
  p->update(), n->update();
}

void splay(node* n) {
  while (n->f != nullptr) {
    node *m = n->f, *l = m->f;
    if (l == nullptr)
      rotate(n);
    else if ((l->c[0] == m) == (m->c[0] == n))
      rotate(m), rotate(n);
    else
      rotate(n), rotate(n);
  }
}

node* find(node* root, pii val) {
  // // // cerr << "Wale finda " << val << "\n";
  node* crawl = root;
  node* prev = root;
  while (crawl && crawl->val != val) {
    // deb(crawl, crawl->val, crawl->c[0], crawl->c[1]);
    if (val < crawl->val) {
      prev = crawl;
      crawl = crawl->c[0];
    } else {
      prev = crawl;
      crawl = crawl->c[1];
    }
  }

  // // // cerr << "Find udany " << crawl << "\n";

  if (crawl) {
    splay(crawl);
  } else {
    splay(prev);
  }

  return crawl;
}

void dump(node* n) {
  if (!n)
    return;
  deb(n, n->c[0], n->c[1], n->cnt, n->sum, n->val, n->wliczam);
  if (n->c[0]) {
    // assert(n->c[0]->val < n->val);
  }
  if (n->c[1]) {
    // assert(n->c[1]->val > n->val);
  }
  // dump(n->c[0]);
  // dump(n->c[1]);
}

// Insert node n to tree with root root
node* insert(node* root, node* n) {
  // // // cerr << "Inserting " << n->val << "\n";
  node* crawl = root;
  while (crawl && crawl->val != n->val) {
    // // // cerr << "Im in " << crawl->val << "\n";
    if (n->val < crawl->val) {
      // // // cerr << "Going left\n";
      if (crawl->c[0]) {
        // // // cerr << "There is a left child, crawling\n";
        crawl = crawl->c[0];
      } else {
        // // // cerr << "No left child, stopping\n";
        crawl->c[0] = n;
        n->f = crawl;
        break;
      }
    } else {
      if (crawl->c[1]) {
        crawl = crawl->c[1];
      } else {
        crawl->c[1] = n;
        n->f = crawl;
        break;
      }
    }
  }

  if (crawl == n) {
    // // // cerr << "crawl == n\n";
    splay(crawl);
    return crawl;
  }

  // // // cerr << "Splaying " << n->val << "\n";

  splay(n);
  return n;
}

node* minimum(node* root) {
  while (root && root->c[0])
    root = root->c[0];

  if (root)
    splay(root);
  return root;
}

node* maximum(node* root) {
  while (root && root->c[1])
    root = root->c[1];

  if (root)
    splay(root);
  return root;
}

// assumes all values in r1 are smaller than values in r2
node* join(node* r1, node* r2) {
  // assert(r1);
  // assert(r2);
  splay(r1);
  splay(r2);
  // // cerr << "Robie join " << r1->val << ", " << r2->val << "\n";
  // dump(r1);
  // dump(r2);

  if (r1->val > r2->val)
    swap(r1, r2);

  r1 = maximum(r1);
  splay(r2);

  // assert(r1->c[1] == nullptr);

  r1->c[1] = r2;
  r2->f = r1;

  r1->update();

  return r1;
}

node* erase(node* root, pii p) {
  node* n = find(root, p);
  splay(n);
  if (n->c[0]) {
    n->c[0]->f = nullptr;
    n->c[0]->wliczam |= n->wliczam;
  }
  if (n->c[1]) {
    n->c[1]->f = nullptr;
    n->c[1]->wliczam |= n->wliczam;
  }

  if (n->c[0] && n->c[1]) {
    root = join(n->c[0], n->c[1]);
  } else if (!n->c[0]) {
    root = n->c[1];
  } else {
    root = n->c[0];
  }

  delete n;
  return root;
}

// bool contains(node* root, pii val) {
//   return find(root, val) != nullptr;
// }

map<pair<int, int>, node*> nodes;
set<pair<int, int>> sklejacz;
set<pair<int, int>> kwadrat;

const int dx[] = {0, 1, 1, 1, 0, -1, -1, -1, 0};
const int dy[] = {1, 1, 0, -1, -1, -1, 0, 1, 0};

const int d4x[] = {0, 0, 1, -1};
const int d4y[] = {1, -1, 0, 0};

vector<pii> get_all(node* root, vector<pii>& res) {
  if (root) {
    // deb(root, root->val, root->c[0], root->c[1], root->f);
    get_all(root->c[0], res);
    res.push_back(root->val);
    get_all(root->c[1], res);
  }
  return res;
}

void remove_all(node* root) {
  if (!root)
    return;
  remove_all(root->c[0]);
  remove_all(root->c[1]);
  delete root;
}

node* change_cnt(node* n, int cnt) {
  splay(n);
  n->cnt = cnt;
  n->update();
  return n;
}

int ans = 0;

void sprawdz_i_moze_odlicz(node* n) {
  splay(n);
  if (n->wliczam && n->cnt >= 2) {}
}

int size(node* n) {
  splay(n);
  return n->size;
}

void odlicz(node* n) {
  splay(n);
  // cerr << "Odejmuje w odliczam " << n->val << " " << n->wliczam << " " << n->sum
  // << " " << n->size << "\n";
  if (!n->wliczam) {
    // cerr << "A jednak nie odliczam...\n";
    return;
  }
  ans -= size(n);
  n->wliczam = false;
}

bool contains(node* n, pii p) {
  splay(n);
  node* crawl = find(n, p);
  return crawl != nullptr;
}

void remove_and_split(node* n) {
  splay(n);

  // cerr << "Odejmuje w remove_and_split do ans dla " << n->val << " " << 1
  // << "\n";
  if (!n->wliczam) {
    // cerr << "Its just a prank bro\n";
  } else {
    ans--;
  }
  deb("Usuwam", n->val, n->wliczam, n->size, n->cnt);

  /* Jeszcze dodac ifa ze jak sie usuwa goscia i nad nim i na lewo nad nim sa goscie to trzeba inaczej splitnac xdddd) */

  if (n->c[0]) {
    n->c[0]->f = nullptr;
    n->c[0]->wliczam |= n->wliczam;
  }

  if (n->c[1]) {
    n->c[1]->f = nullptr;
    n->c[1]->wliczam |= n->wliczam;
  }

  if (n->c[0]) {
    // gosc pod usuwanym
    node* nikczemnik = find(n->c[0], {n->val.first, n->val.second - 1});
    if (nikczemnik &&
        !(n->c[1] && contains(n->c[1], {n->val.first + 1, n->val.second}))) {
      // // cerr << "Jest z dolu nikczemnik!!! " << nikczemnik->val << "\n";
      // dump(nikczemnik);
      // gosc na lewo od usuwanego
      node* pot = find(nikczemnik, {n->val.first - 1, n->val.second});
      if (pot) {
        // // cerr << "Wypieprzam go z tego seta " << pot->val << "\n";
        // dump(pot);

        splay(nikczemnik);

        nikczemnik->wliczam |= n->wliczam;
        nikczemnik->c[0]->f = nullptr;
        nikczemnik->c[0]->wliczam |= nikczemnik->wliczam;
        nikczemnik->c[0]->update();

        nikczemnik->c[0] = nullptr;
        nikczemnik->update();
      }
      if (n->c[1]) {
        // // cerr << "Dodaje nikczemnika do drugiego seta " << n->c[1]->val << "\n";
        splay(n->c[1]);
        insert(n->c[1], nikczemnik);
        // dump(n->c[1]);
      }
      // // cerr << "NA KONCU STAN TO\n";
      if (pot)
        splay(pot);
      // dump(pot);
      // // cerr << "nik:\n";
      if (nikczemnik)
        splay(nikczemnik);
      // dump(nikczemnik);
    }
  }

  if (n->c[1]) {
    // gosc nad usuwanym
    node* nikczemnik = find(n->c[1], {n->val.first, n->val.second + 1});
    if (nikczemnik &&
        !(n->c[0] && contains(n->c[0], {n->val.first - 1, n->val.second}))) {
      // // cerr << "Jest z gory nikczemnik!!! " << nikczemnik->val << "\n";
      // dump(nikczemnik);
      // gosc na parwo od usuwanego
      node* pot = find(nikczemnik, {n->val.first + 1, n->val.second});
      if (pot) {
        // usun nikczemnika, powinien byc w drugim secie
        // // cerr << "Wypicprzam go z tego seta " << pot->val << "\n";
        // dump(pot);

        splay(nikczemnik);

        nikczemnik->wliczam |= n->wliczam;
        nikczemnik->c[1]->f = nullptr;
        nikczemnik->c[1]->wliczam |= nikczemnik->wliczam;
        nikczemnik->c[1]->update();

        nikczemnik->c[1] = nullptr;
        nikczemnik->update();
      }
      if (n->c[0]) {
        // // cerr << "Dodaje nikczemnikagc do drugiego seta " << n->c[0]->val
        // << "\n";
        splay(n->c[0]);
        insert(n->c[0], nikczemnik);
        // dump(n->c[0]);
      }
      // // cerr << "NA KONCU STAN TO\n";
      if (pot)
        splay(pot);
      // dump(pot);
      // // cerr << "nik:\n";
      if (nikczemnik)
        splay(nikczemnik);
      // dump(nikczemnik);
    }
  }

  // // cerr << "// Dumpuje lewego\n";
  if (n->c[0])
    splay(n->c[0]);
  // dump(n->c[0]);
  // // cerr << "// Dumpuje prawego\n";
  if (n->c[1])
    splay(n->c[1]);
  // dump(n->c[1]);

  n->c[0] = nullptr;
  n->c[1] = nullptr;
  n->cnt = 0;
  n->wliczam = false;

  n->update();
}

bool czy_zajete(int x, int y) {
  return nodes.count({x, y});
}

bool czy_jestem_kwadratem(int x, int y) {
  for (int i = 0; i < 8; i += 2) {
    bool jestem = true;
    for (int k = 0; k < 3; k++) {
      if (!czy_zajete(x + dx[(i + k) % 8], y + dy[(i + k) % 8])) {
        jestem = false;
        break;
      }
    }
    if (jestem) {
      return true;
    }
  }

  return false;
}

bool czy_jestem_sklejaczem(int x, int y) {
  if (czy_zajete(x + 1, y) && czy_zajete(x - 1, y))
    return true;
  if (czy_zajete(x, y + 1) && czy_zajete(x, y - 1))
    return true;
  return false;
}

void dump_state() {
  // deb(nodes);
  deb(kwadrat);
  deb(sklejacz);
  deb(ans);
  for (auto [_, node] : nodes) {
    deb(node->val, node->f == nullptr, node->cnt, node->sum, node->size,
        node->wliczam);
    splay(node);
    if (node->wliczam && !sklejacz.count(node->val)) {
      // assert(node->cnt < 2);
      // assert(node->sum < 2);
    }
    if (node->size == 1 && node->cnt != node->sum) {
      // assert(false);
    }
    if (node->f != nullptr) {
      // assert(!node->wliczam);
    }
  }
}

bool are_on_one_path(node* n1, node* n2) {
  // // cerr << "Czy juz byly polaczone?\n";
  splay(n1);
  deb(n1, n2);
  // dump(n1);
  return find(n1, n2->val) != nullptr;
}

bool czy_wliczam(node* n) {
  splay(n);
  return n->wliczam;
}

void wlicz(node* n) {
  splay(n);
  if (n->wliczam)
    return;

  // cerr << "Dodaje z wlicz do ans dla " << n->val << "\n";
  ans += size(n);
  n->wliczam = true;
}

// assuming they are not merged already
void merge_paths(node* n1, node* n2) {
  // cerr << "Merguje " << n1->val << " " << n2->val << "\n";
  splay(n1);
  splay(n2);

  if (czy_wliczam(n1)) {
    // cerr << "Merg1 Odejmuje do ans dla " << n1->val << " " << size(n1) << "\n";
    ans -= size(n1);
    n1->wliczam = false;
  }
  if (czy_wliczam(n2)) {
    // cerr << "Merg2 Odejmuje do ans dla " << n2->val << " " << size(n2) << "\n";
    ans -= size(n2);
    n2->wliczam = false;
  }

  if (size(n1) == 1) {
    splay(n2);
    insert(n2, n1);
    return;
  }
  if (size(n2) == 1) {
    splay(n1);
    insert(n1, n2);
    return;
  }

  node* n1_min = minimum(n1);
  node* n2_min = minimum(n2);

  if (contains(n2, {n1_min->val.first, n1_min->val.second - 1})) {
    // // // cerr << "Gupia sprawa, trzeba na okolo, wywalam " << n1_min->val << "\n";
    splay(n1_min);
    // assert(size(n1_min) > 1);
    // dump(n1_min);

    n1_min->c[1]->f = nullptr;
    join(n1_min->c[1], n2);
    n1_min->c[1] = nullptr;
    n1_min->update();

    splay(n2);
    insert(n2, n1_min);
    return;
  } else if (contains(n1, {n2_min->val.first, n2_min->val.second - 1})) {
    // // // cerr << "Gupia sprawa, trzeba na okolo DWA, wywalam " << n2_min->val
    //      << "\n";
    splay(n2_min);
    // assert(n2_min->c[0] == nullptr);
    n2_min->c[1]->f = nullptr;

    join(n2_min->c[1], n1);

    n2_min->c[1] = nullptr;
    n2_min->update();

    splay(n1);
    insert(n1, n2_min);
    return;
  }

  if (n1_min->val > n2_min->val) {
    swap(n1, n2);
  }

  join(n1, n2);
}

void add_cell(int x, int y) {
  // cerr << ">>>>>>>>>>>>>>>>>>>> Dodaje komorke " << x << " " << y << "\n";

  vector<pii> nowe_kwadraty;
  for (int k = 0; k < 9; k++) {
    int xp = x + dx[k];
    int yp = y + dy[k];
    if (nodes.count({xp, yp}) && !kwadrat.count({xp, yp}) &&
        czy_jestem_kwadratem(xp, yp)) {
      // // cerr << "Komorka " << xp << ", " << yp
      // << " jest czescia kwadratu i wczesniej nie byla!\n";
      kwadrat.insert({xp, yp});
      sklejacz.erase({xp, yp});
      nowe_kwadraty.push_back({xp, yp});

      node* n = nodes[{xp, yp}];
      if (size(n) > 1) {
        // // cerr << "Trzeba ja rozdzielic!\n";
        remove_and_split(n);
      } else if (n->wliczam) {
        // cerr << "Odejmuje do ans dla " << n->val << " bo to nowy kwadrat\n";
        n->wliczam = 0;
        ans--;
      }
      n->cnt = 0;
      n->sum = 0;
    }
  }
  set<pair<int, int>> nowy_sklej;

  for (int k = 0; k < 9; k++) {
    int xp = x + dx[k];
    int yp = y + dy[k];
    if (nodes.count({xp, yp}) && !kwadrat.count({xp, yp}) &&
        czy_jestem_sklejaczem(xp, yp)) {
      // cerr << "Komorka " << xp << ", " << yp << " jest sklejaczem.\n";

      node* n = nodes[{xp, yp}];
      if (size(n) > 1) {
        remove_and_split(n);
      }
      if (!n->wliczam) {
        n->wliczam = true;
        // // cerr << "Dodaje do ans dla " << n->val << "\n";
        ans++;
      }

      n->sum = 0;
      n->cnt = 0;

      nowy_sklej.insert({xp, yp});
      for (int j = 0; j < 4; j++) {
        int xpp = xp + d4x[j];
        int ypp = yp + d4y[j];
        if (nodes.count({xpp, ypp}) && czy_jestem_kwadratem(xpp, ypp)) {
          // cerr << "Musze dodac cnt bo " << xpp << " " << ypp
          // << " jest kwadratem\n";
          n->cnt++;
          n->sum++;
        }
      }
      sklejacz.insert({xp, yp});
    }
  }

  for (auto [x1, y1] : nowe_kwadraty) {
    for (int k = 0; k < 4; k++) {
      int xp = x1 + d4x[k];
      int yp = y1 + d4y[k];

      if (nodes.count({xp, yp}) && !kwadrat.count({xp, yp}) &&
          !nowy_sklej.count({xp, yp})) {
        node* n = nodes[{xp, yp}];
        splay(n);
        n->cnt++;
        n->sum++;

        if (n->sum == 2 && !sklejacz.count({xp, yp})) {
          odlicz(n);
        }
      }
    }
  }

  for (int k = 0; k < 9; k++) {
    int xp = x + dx[k];
    int yp = y + dy[k];

    if (nodes.count({xp, yp}) && !kwadrat.count({xp, yp}) &&
        !sklejacz.count({xp, yp})) {
      // cerr << "Komorka " << xp << ", " << yp << " jest czescia sciezki.\n";

      node* n = nodes[{xp, yp}];
      splay(n);
      if (n->cnt >= 2) {
        continue;
      }

      n->cnt = 0;
      for (int j = 0; j < 4; j++) {
        int xpp = xp + d4x[j];
        int ypp = yp + d4y[j];
        if (!nodes.count({xpp, ypp}))
          continue;
        if (kwadrat.count({xpp, ypp})) {
          change_cnt(n, n->cnt + 1);
        } else if (!sklejacz.count({xpp, ypp})) {
          // cerr << "Komorka " << xp << ", " << yp << " jest czescia sciezki z "
          // << xpp << " " << ypp << "\n";
          node* n2 = nodes[{xpp, ypp}];
          if (!are_on_one_path(n, n2)) {
            merge_paths(n, n2);
          }
        }
      }

      splay(n);
      if (n->sum < 2) {
        wlicz(n);
      } else {
        odlicz(n);
      }
    }
  }

  // // dump_state();
}

bool sciezkowy(int x, int y) {
  return nodes.count({x, y}) && !sklejacz.count({x, y}) &&
         !kwadrat.count({x, y});
}

void remove_cell(int x, int y) {
  // cerr << ">>>>>>>>>>>>>>>>>>>>>>>> usuwam komorke " << x << ", " << y << "\n";
  node* n_glowny = nodes[{x, y}];
  nodes.erase({x, y});

  /* Jesli bylem elementem sciezki to usuniecie mnie odblokowuje obie sciezki */
  if (!sklejacz.count({x, y}) && !kwadrat.count({x, y})) {
    // cerr << "Bylem szara sciezka\n";
    remove_and_split(n_glowny);
    for (int i = 0; i < 4; i++) {
      int xp = x + d4x[i];
      int yp = y + d4y[i];
      if (sciezkowy(xp, yp)) {
        node* np = nodes[{xp, yp}];
        splay(np);
        wlicz(np);
      }
    }
  }

  // jesli bylem czescia kwadrata to przejdz sie po sasiednich sciezkach i im powiedz ze juz jest gicior blancior
  if (kwadrat.count({x, y})) {
    // cerr << "Za zycia bylem kwadratem\n";
    for (int k = 0; k < 4; k++) {
      int xp = x + d4x[k];
      int yp = y + d4y[k];
      if (sciezkowy(xp, yp)) {
        node* n = nodes[{xp, yp}];
        // cerr << "Pan " << xp << ", " << yp << "jest sciezkowy\n";
        // cerr << "Nie wliczalem go, wiec teraz go wlicze!\n";
        splay(n);
        n->cnt--;
        n->sum--;
        wlicz(n);
      } else if (sklejacz.count({xp, yp})) {
        node* n = nodes[{xp, yp}];
        splay(n);
        n->cnt--;
        // assert(n->cnt >= 0);
        n->sum--;
      }
    }
    kwadrat.erase({x, y});
  }

  if (sklejacz.count({x, y})) {
    // jak bylem sklejaczem to sie w sumie wiele nie dzieje
    // cerr << "Za zycia bylem sklejaczem\n";
    odlicz(n_glowny);
    sklejacz.erase({x, y});
  }

  set<pair<int, int>> stare_kwadraty;
  for (int i = 0; i < 8; i++) {
    int xp = x + dx[i];
    int yp = y + dy[i];
    if (!nodes.count({xp, yp}))
      continue;

    if (sklejacz.count({xp, yp})) {
      // cerr << "Kiedys bylem sklejaczem " << xp << " " << yp << "\n";
      node* np = nodes[{xp, yp}];

      // assert(!kwadrat.count({xp, yp}));
      // nadal jestem sklejaczem
      if (czy_jestem_sklejaczem(xp, yp)) {
        continue;
      }

      sklejacz.erase({xp, yp});
      // teraz jestem sciezka!!
      for (int k = 0; k < 4; k++) {
        int xpp = xp + d4x[k];
        int ypp = yp + d4y[k];
        if (sciezkowy(xpp, ypp)) {
          merge_paths(np, nodes[{xpp, ypp}]);
          splay(np);
          wlicz(np);
        }
      }
    }

    else if (kwadrat.count({xp, yp})) {
      if (czy_jestem_kwadratem(xp, yp)) {
        continue;
      }
      // cerr << "Ja tez kiedys bylem kwadratem ale juz nie " << xp << " " << yp
      // << "\n";
      kwadrat.erase({xp, yp});
      stare_kwadraty.insert({xp, yp});
      // do_wyjebania.push_back({xp, yp});

      // Juz nie jestem kwadratem. Moze zostalem sklejaczem?
      if (czy_jestem_sklejaczem(xp, yp)) {
        // cerr << "Zostalem sklejaczem!\n";

        sklejacz.insert({xp, yp});
        node* n = nodes[{xp, yp}];
        for (int k = 0; k < 4; k++) {
          int xpp = xp + d4x[k];
          int ypp = yp + d4y[k];
          if (nodes.count({xpp, ypp}) && czy_jestem_kwadratem(xpp, ypp)) {
            // cerr << "Somsiad kwadrat " << xpp << " " << ypp << "\n";
            n->cnt++;
            n->sum++;
          } else if (nodes.count({xpp, ypp}) &&
                     !stare_kwadraty.count({xpp, ypp}) &&
                     !kwadrat.count({xpp, ypp})) {
            // cerr << "Odejmuje cnt dla " << xpp << " " << ypp << "\n";
            node* np = nodes[{xpp, ypp}];
            splay(np);
            np->cnt--;
            // assert(np->cnt >= 0);
            np->sum--;
            wlicz(np);
          }
        }
        wlicz(n);
      } else {
        // nie jestem ani sklejaczem ani kwadratem, zatem jestem sciezka!
        // cerr << "Zostalem sciezka\n";
        node* n = nodes[{xp, yp}];
        for (int k = 0; k < 4; k++) {
          int xpp = xp + d4x[k];
          int ypp = yp + d4y[k];
          // moze teraz jeszcze nie kazdy sasiad jest uznany za sciezkowego ale zaraz bedzie i wtedy sie z nim polacze
          if (sciezkowy(xpp, ypp)) {
            node* np = nodes[{xpp, ypp}];
            splay(np);
            if (!stare_kwadraty.count({xpp, ypp})) {
              np->cnt--;
              // assert(np->cnt >= 0);
              np->sum--;
            }
            merge_paths(n, np);
          } else if (nodes.count({xpp, ypp}) &&
                     czy_jestem_kwadratem(xpp, ypp)) {
            splay(n);
            n->cnt++;
            n->sum++;
          } else if (sklejacz.count({xpp, ypp})) {
            node* np = nodes[{xpp, ypp}];
            if (!stare_kwadraty.count({xpp, ypp})) {
              // cerr << "Odejmuje sklejaczowi " << xpp << " " << ypp << "\n";
              splay(np);
              np->cnt--;
              np->sum--;
              // assert(np->cnt >= 0);
            }
          }
        }
        splay(n);
        // cerr << "No dobra to jaki mam cnt??? " << n->cnt << " " << n->cnt << " "
        // << n->wliczam << "\n";

        if (n->sum < 2) {
          wlicz(n);
        }
      }
    }
  }
  // for (auto [xp, yp] : do_wyjebania) {
  //   kwadrat.erase({xp, yp});
  // }

  delete n_glowny;
  // // dump_state();
}

int get_ans() {
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie();

  int n, m, k, q;
  cin >> n >> m >> k >> q;

  for (int i = 0; i < k; i++) {
    int x, y;
    cin >> x >> y;

    nodes[{x, y}] = new node({x, y});
    add_cell(x, y);
  }
  cout << get_ans() << "\n";

  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;

    if (!nodes.count({x, y})) {
      nodes[{x, y}] = new node({x, y});
      add_cell(x, y);
    } else {
      remove_cell(x, y);
    }
    cout << get_ans() << "\n";
  }

#ifdef LOCAL
  for (auto [_, node] : nodes) {
    delete node;
  }
#endif
}