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
#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) begin(x), end(x)
#define sz(x) int((x).size())
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

#ifdef LOCAL
auto operator<<(auto& o, auto x) -> decltype(x.first, o);
auto operator<<(auto& o, auto x) -> decltype(x.end(), o) {
  o << "{";
  for (int i = 0; auto y : x) o << ", " + !i++ * 2 << y;
  return o << "}"; }
auto operator<<(auto& o, auto x) -> decltype(x.first, o) {
  return o << "(" << x.first << ", " << x.second << ")"; }
void __print(auto... x) { ((cerr << x << " "), ...) << endl; }
#define debug(x...) __print("[" #x "]:", x)
#else
#define debug(...) 2137
#endif

const int inf = 1e9;
int ans = 0;

struct seg {
  int diag, l, r;
  auto operator<=>(const seg&) const = default;
};
ostream& operator<<(ostream& o, const seg& a) {
  return o << "(" << a.diag << ", " << a.l << ", " << a.r << ")";
}

struct DS {
  int n, m;
  set<seg> st;
  set<pair<int, int>> grid;
  DS(int n = 0, int m = 0) : n(n), m(m) {}
  void insert(int i, int j) {
    int my_d = i + j;
    for(int d : array<int, 2>{my_d, my_d + 1}) {
      if(d < 0 || d >= n + m) continue;
      int ind = get_ind(d, {i, j});
      pair<int, int> prv = at(d, ind - 1);
      pair<int, int> nxt = at(d, ind + 1);
      int lt = ind, rt = ind;
      if(grid.contains(prv)) {
        auto it = st.lower_bound({d, ind, ind});
        if(it == st.begin()) {
          lt = ind - 1;
        } else {
          it--;
          if(it->diag == d && it->r == ind - 1) {
            lt = it->l;
            pair<int, int> other = {i + (d == my_d), j - (d != my_d)};
            if(grid.contains(other)) {
              if(ind - lt - 4 > 0) {
                if(grid.contains(at(d, lt - 1))) {
                  ans -= ind - lt - 4;
                }
              }
              lt = ind - 1;
            } else {
              st.erase(it);
            }
          } else {
            lt = ind - 1;
          }
        }
      }
      if(grid.contains(nxt)) {
        auto it = st.upper_bound({d, ind, ind});
        if(it != st.end() && it->diag == d && it->l == ind + 1) {
          rt = it->r;
          pair<int, int> other = {i - (d != my_d), j + (d == my_d)};
          if(grid.contains(other)) {
            if(rt - ind - 4 > 0) {
              if(grid.contains(at(d, rt + 1))) {
                ans -= rt - ind - 4;
              }
            }
            rt = ind + 1;
          } else {
            st.erase(it);
          }
        } else {
          rt = ind + 1;
        }
      }
      if(lt < ind && ind < rt) {
        pair<int, int> other = {i + 1, j + 1};
        if(d == my_d) {
          other.first -= 2;
          other.second -= 2;
        }
        if(!grid.contains(other)) {
          ind = rt;
        }
      }
      if(lt < ind) {
        st.insert({d, lt, ind});
        if(ind - lt - 3 > 0) {
          if(grid.contains(at(d, lt - 1)) && grid.contains(at(d, ind + 1))) {
            ans -= ind - lt - 3;
          }
        }
      }
      if(ind < rt) {
        st.insert({d, ind, rt});
        if(rt - ind - 3 > 0) {
          if(grid.contains(at(d, ind - 1)) && grid.contains(at(d, rt + 1))) {
            ans -= rt - ind - 3;
          }
        }
      }
    }
    for(int d : array<int, 2>{my_d - 1, my_d + 2}) {
      if(d < 0 || d >= n + m) continue;
      pair<int, int> other = {i + 1, j + 1};
      if(d == my_d - 1) {
        other.first -= 2;
        other.second -= 2;
      }
      int ind = get_ind(d, other);
      auto it = st.lower_bound({d, ind, ind});
      if(it != st.begin()) {
        it--;
        if(it->diag == d && it->r > ind) {
          int lt = it->l;
          int rt = it->r;
          st.erase(it);
          bool block_lt = grid.contains(at(d, lt - 1));
          bool block_rt = grid.contains(at(d, rt + 1));
          if(block_lt && block_rt) {
            ans += max(rt - lt - 3, 0);
          }
          st.insert({d, lt, ind});
          if(block_lt) {
            ans -= max(ind - lt - 3, 0);
          }
          st.insert({d, ind, rt});
          if(block_rt) {
            ans -= max(rt - ind - 3, 0);
          }
        }
      }
    }
    grid.insert({i, j});
  }
  void erase(int i, int j) {
    int my_d = i + j;
    for(int d : array<int, 2>{my_d, my_d + 1}) {
      if(d < 0 || d >= n + m) continue;
      int ind = get_ind(d, {i, j});
      pair<int, int> prv = at(d, ind - 1);
      pair<int, int> nxt = at(d, ind + 1);
      int lt = ind, rt = ind;
      if(grid.contains(prv)) {
        auto it = st.lower_bound({d, ind - 1, ind - 1});
        if(it != st.begin()) {
          it--;
          if(it->diag == d && it->r == ind - 1) {
            lt = it->l;
            if(ind - lt - 4 > 0) {
              if(grid.contains(at(d, lt - 1))) {
                ans += ind - lt - 4;
              }
            }
          }
        }
      }
      if(grid.contains(nxt)) {
        auto it = st.lower_bound({d, ind + 1, ind + 1});
        if(it != st.end() && it->diag == d && it->l == ind + 1) {
          rt = it->r;
          if(rt - ind - 4 > 0) {
            if(grid.contains(at(d, rt + 1))) {
              ans += rt - ind - 4;
            }
          }
        }
      }
      lt = ind, rt = ind;
      bool two = 0;
      auto it = st.lower_bound({d, ind, ind});
      if(it != st.end()) {
        if(it->diag == d && it->l == ind) {
          rt = it->r;
          two = 1;
        }
      }
      if(it != st.begin()) {
        it--;
        if(it->diag == d && it->r >= ind) {
          lt = it->l;
          if(!two) rt = it->r;
        }
      }
      if(lt < ind - 1) st.insert({d, lt, ind - 1});
      if(ind + 1 < rt) st.insert({d, ind + 1, rt});
      if(!two) ind = rt;
      st.erase({d, lt, ind});
      if(ind - lt - 3 > 0) {
        if(grid.contains(at(d, lt - 1)) && grid.contains(at(d, ind + 1))) {
          ans += ind - lt - 3;
        }
      }
      st.erase({d, ind, rt});
      if(rt - ind - 3 > 0) {
        if(grid.contains(at(d, ind - 1)) && grid.contains(at(d, rt + 1))) {
          ans += rt - ind - 3;
        }
      }
    }
    for(int d : array<int, 2>{my_d - 1, my_d + 2}) {
      if(d < 0 || d >= n + m) continue;
      pair<int, int> other = {i + 1, j + 1};
      if(d == my_d - 1) {
        other.first -= 2;
        other.second -= 2;
      }
      int ind = get_ind(d, other);
      auto it = st.lower_bound({d, ind, ind});
      if(it != st.end() && it->diag == d && it->l == ind) {
        int rt = it->r;
        if(it != st.begin()) {
          auto it2 = it;
          it--;
          if(it->diag == d && it->r == ind) {
            int lt = it->l;
            st.erase(it);
            st.erase(it2);
            st.insert({d, lt, rt});
            bool block_lt = grid.contains(at(d, lt - 1));
            bool block_rt = grid.contains(at(d, rt + 1));
            if(block_lt) {
              ans += max(ind - lt - 3, 0);
            }
            if(block_rt) {
              ans += max(rt - ind - 3, 0);
            }
            if(block_lt && block_rt) {
              ans -= max(rt - lt - 3, 0);
            }
          }
        }
      }
    }
    grid.erase({i, j});
  }
  inline pair<int, int> at(int d, int ind) {
    if(ind == -1) return {d, -1};
    int y = ind / 2;
    int x = d - y;
    if(ind & 1) x--;
    return {x, y};
  }
  inline int get_ind(int d, pair<int, int> p) {
    int ind = 2 * p.second;
    if((d & 1) != ((p.first + p.second) & 1)) ind++;
    return ind;
  }
};
DS lt, rt;

map<pair<int, int>, int> st_sq;
int n, m;
void add(int x, int y) {
  ans++;
  lt.insert(x, y);
  rt.insert(x, m - 1 - y);
  array<array<int, 3>, 3> a;
  a[1][1] = 1;
  for(int i = -1; i <= 1; i++) {
    for(int j = -1; j <= 1; j++) {
      if(!i && !j) continue;
      a[i + 1][j + 1] = lt.grid.contains({x + i, y + j});
    }
  }
  for(int i = 0; i < 2; i++) {
    for(int j = 0; j < 2; j++) {
      int s = 0;
      for(int c = 0; c < 2; c++) {
        for(int d = 0; d < 2; d++) {
          s += a[i + c][j + d];
        }
      }
      if(s == 4) {
        for(int c = 0; c < 2; c++) {
          for(int d = 0; d < 2; d++) {
            pair<int, int> p = {x - 1 + i + c, y - 1 + j + d};
            if(!st_sq[p]) ans--;
            st_sq[p]++;
          }
        }
      }
    }
  }
}

void rem(int x, int y) {
  ans--;
  lt.erase(x, y);
  rt.erase(x, m - 1 - y);
  array<array<int, 3>, 3> a;
  a[1][1] = 1;
  for(int i = -1; i <= 1; i++) {
    for(int j = -1; j <= 1; j++) {
      if(!i && !j) continue;
      a[i + 1][j + 1] = lt.grid.contains({x + i, y + j});
    }
  }
  for(int i = 0; i < 2; i++) {
    for(int j = 0; j < 2; j++) {
      int s = 0;
      for(int c = 0; c < 2; c++) {
        for(int d = 0; d < 2; d++) {
          s += a[i + c][j + d];
        }
      }
      if(s == 4) {
        for(int c = 0; c < 2; c++) {
          for(int d = 0; d < 2; d++) {
            pair<int, int> p = {x - 1 + i + c, y - 1 + j + d};
            st_sq[p]--;
            if(!st_sq[p]) {
              st_sq.erase(p);
              ans++;
            }
          }
        }
      }
    }
  }
}

int main() {
  cin.tie(0)->sync_with_stdio(0);

  int k, q;
  cin >> n >> m >> k >> q;
  set<pair<int, int>> st;
  lt = DS(n, m);
  rt = DS(n, m);
  for(int i = 0; i < k; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    st.insert({x, y});
    add(x, y);
  }
  cout << ans << '\n';
  for(int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    if(st.contains({x, y})) {
      st.erase({x, y});
      rem(x, y);
    } else {
      st.insert({x, y});
      add(x, y);
    }
    cout << ans << '\n';
  }
}