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
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;

using ll = long long;
using ld = long double;

//#define int ll
#define sz(x) ((int)(x).size())
#define reduce muiemuiemuie
using pii = pair<int,int>;
using tii = tuple<int,int,int>;

int dirx[4] = {1, -1, 0, 0};
int diry[4] = {0, 0, 1, -1};

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return x ^ FIXED_RANDOM;
    }
    
    size_t operator()(pair<int, int> x) const {
        return (*this)((ll)x.first * 1e9 + x.second);
    }
};

signed main() { // U R D L; x creste in jos, y creste in dreapta
   cin.tie(0) -> sync_with_stdio(0);
   int H, W, n, q;
   cin >> H >> W >> n >> q;
   
   unordered_map<pii, char, custom_hash> cells, actually;
   vector<pii> inqueue;
   auto p = [&](int a, int b) { return pii{a, b}; };
   
   
   auto insert = [&](int x, int y) {
      cells[p(x, y)];
      if(cells.count(p(x - 1, y))) {
         cells[p(x - 1, y)] |= 1 << 2;
         cells[p(x, y)] |= 1 << 0;
      }
      if(cells.count(p(x + 1, y))) {
         cells[p(x + 1, y)] |= 1 << 0;
         cells[p(x, y)] |= 1 << 2;
      }
      if(cells.count(p(x, y - 1))) {
         cells[p(x, y - 1)] |= 1 << 1;
         cells[p(x, y)] |= 1 << 3;
      }
      if(cells.count(p(x, y + 1))) {
         cells[p(x, y + 1)] |= 1 << 3;
         cells[p(x, y)] |= 1 << 1;
      }
   };
   auto erase = [&](int x, int y) {
      cells.erase(p(x, y));
      
      if(cells.count(p(x - 1, y)))
         cells[p(x - 1, y)] &= ~(1 << 2);
      if(cells.count(p(x + 1, y)))
         cells[p(x + 1, y)] &= ~(1 << 0);
      if(cells.count(p(x, y - 1)))
         cells[p(x, y - 1)] &= ~(1 << 1);
      if(cells.count(p(x, y + 1)))
         cells[p(x, y + 1)] &= ~(1 << 3);
      for(int h = 0; h < 4; h++) {
         if(!cells.count(p(x + dirx[h], y + diry[h]))) continue;
         auto b = cells[p(x + dirx[h], y + diry[h])];
         if(((b & (0b1010)) == 0) || ((b & (0b0101)) == 0)) inqueue.emplace_back(p(x + dirx[h], y + diry[h]));
      }
   };
   
   for(int i = 0, x, y; i < n; i++) {
      cin >> x >> y;
      insert(x, y);
      actually[p(x, y)];
   }
   int reduced = 0;
   auto reduce = [&]() {
      while(!inqueue.empty()) {
         auto x = inqueue.back();
         inqueue.pop_back();
         if(!cells.count(x)) continue;
         erase(x.first, x.second);
         reduced++;
         //cerr << x.first << ' ' << x.second << '\t';;
      }
      return;
   };
   for(auto &[a, b] : cells) 
      if(((b & (0b1010)) == 0) || ((b & (0b0101)) == 0)) inqueue.emplace_back(a);
   
   reduce();
   cout << reduced << '\n';
      
   for(int i = 0, x, y; i < q; i++) {
      cin >> x >> y;
      if(actually.count(p(x, y))) {
         if(cells.count(p(x, y)))
            erase(x, y);
         else reduced--;
         actually.erase(p(x, y));
      }
      else {
         actually[p(x, y)];
         reduced = 0;
         cells.clear();
         for(auto &[x, b] : actually) 
            insert(x.first, x.second);
         for(auto &[a, b] : cells) 
            if(((b & (0b1010)) == 0) || ((b & (0b0101)) == 0)) inqueue.emplace_back(a);
      }
      reduce();
      cout << reduced << '\n';
   }
   return 0;
}

/**
      Binecuvanteaza Doamne Ukraina.
--
*/