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
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

#define JOIN_(X, Y) X##Y
#define JOIN(X, Y) JOIN_(X, Y)
#define TMP JOIN(tmp, __LINE__)
#define PB push_back
#define SZ(x) int((x).size())
#define REP(i, n) for (int i = 0, TMP = (n); i < TMP; ++i)
#define FOR(i, a, b) for (int i = (a), TMP = (b); i <= TMP; ++i)
#define FORD(i, a, b) for (int i = (a), TMP = (b); i >= TMP; --i)

#ifdef DEBUG
#define DEB(x) (cerr << x)
#else
#define DEB(x)
#endif

// typedef long long ll;
// typedef vector<int> vi;
typedef pair<int, int> pii;
// typedef unsigned int uint;
typedef unordered_map<int, int> umap;

// const int INF = 1e9 + 9;

template <typename S, typename T> ostream &operator<<(ostream &os, const pair<S, T> &p) {
    os << "(" << p.first << ", " << p.second << ")";
    return os;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "[";
    for (int i = 0; i < SZ(vec); ++i) {
        os << vec[i];
        if (i != SZ(vec) - 1) {
            os << ", ";
        }
    }
    os << "]";
    return os;
}

vector<pii> DIAGS = {{-1, -1}, {-1, +1}, {+1, -1}, {+1, +1}};
vector<pii> AXIS = {{+1, -1}, {+1, +1}};

umap t[200005];

bool inline safe(int x, int y, int max_x, int max_y) {
    return x > 0 and y > 0 and x <= max_x and y <= max_y;
}

void mark_stuck(int x, int y) {
    if ((t[x].count(y) + t[x + 1].count(y) + t[x].count(y + 1) + t[x + 1].count(y + 1)) == 4) {
        t[x][y] = 1;
    } else {
        t[x][y] = 0;
    }
}

int count_stuck(int x, int y) {
    // DEB("count_stuck(" << x << ", " << y << ")\n");
    // DEB("  t[x][y]=" << t[x][y] << "\n");
    if (t[x][y] == 0) {
        return 0;
    }
    int result = 1;
    int down = t[x + 1].at(y);
    int right = t[x].at(y + 1);
    int has_up_right = t[x - 1].count(y + 1);
    int down_right = t[x + 1].at(y + 1);
    if (down == 0) {
        ++result;
    }
    if (right == 0 and (has_up_right == 0 or t[x - 1].at(y + 1) == 0)) {
        ++result;
    }
    if (down == 0 and right == 0 and down_right == 0) {
        ++result;
    }
    return result;
}

void dump(int max_x, int max_y) {
    DEB("dump:\n");
    FOR(i, 1, max_x) {
        FOR(j, 1, max_y) {
            if (t[i].count(j)) {
                DEB(t[i][j]);
            } else {
                DEB("-");
            }
        }
        DEB("\n");
    }
}

bool stores_stuck(int x, int y) { return t[x].count(y) and t[x][y]; }

bool is_stuck(int x, int y) {
    return stores_stuck(x, y) or stores_stuck(x - 1, y) or stores_stuck(x, y - 1) or
           stores_stuck(x - 1, y - 1);
}

bool is_corner(int x, int y, int dx, int dy, int max_x, int max_y) {
    // DEB("is_corner: " << x << ", " << y << " d: " << dx << ", " << dy << "\n");
    // if (x + dx < 1 or y + dy < 1) {
    if (not safe(x, y, max_x, max_y) or not safe(x + dx, y + dy, max_x, max_y)) {
        return false;
    }
    return (is_stuck(x, y) and not is_stuck(x + dx, y) and not is_stuck(x, y + dy));
}

bool is_blocked(int x, int y) {
    return (t[x].count(y - 1) or t[x].count(y + 1)) and (t[x - 1].count(y) or t[x + 1].count(y));
}

int count_arms_from(int corner_x, int corner_y, int max_x, int max_y) {
    int result = 0;
    for (const auto &[dx, dy] : DIAGS) {
        // DEB("FROM before corner: " << corner_x << ", " << corner_y << " d: " << dx << ", " << dy
        //    << "\n");
        if (is_corner(corner_x, corner_y, dx, dy, max_x, max_y)) {
            // DEB("FROM corner: " << corner_x << ", " << corner_y << " d: " << dx << ", " << dy
            // << "\n");
            int x = corner_x + dx;
            int y = corner_y + dy;
            bool has_arm = false;
            int arm_length = 0;
            while (safe(x, y, max_x, max_y)) {
                // DEB("  loop x,y=" << x << ", " << y << "\n");
                if (is_stuck(x, y)) {
                    // DEB("  has_arm\n");
                    if (is_corner(x, y, -dx, -dy, max_x, max_y)) {
                        if (t[x - dx].count(y) or t[x].count(y - dy)) {
                            has_arm = true;
                            ++arm_length;
                            // DEB("  ++arm_length\n");
                        } else {
                            // DEB("  not connected corner\n");
                        }
                    } else {
                        has_arm = true;
                        // DEB("  no corner, no arm...\n");
                    }
                    break;
                }
                if (not t[x].count(y)) {
                    break;
                }
                if (not is_blocked(x, y)) {
                    break;
                }
                arm_length += 2;
                x += dx;
                y += dy;
            }
            if (has_arm) {
                result += arm_length;
            }
        }
    }
    return result;
}

int count_all_arms(int max_x, int max_y) {
    int result = 0;
    FOR(corner_x, 1, max_x) {
        for (const auto &[corner_y, cnt] : t[corner_x]) {
            result += count_arms_from(corner_x, corner_y, max_x, max_y);
        }
    }
    return result / 2;
}

vector<pii> find_distant_corners(int source_x, int source_y, int max_x, int max_y) {
    vector<pii> results;
    DEB("fdd source=" << source_x << "," << source_y << "\n");
    for (const auto &[dx, dy] : DIAGS) {
        DEB("fdd d=" << dx << "," << dy << "\n");
        int x = source_x + dx;
        int y = source_y + dy;
        // bool skip_first_corner = true;
        while (safe(x, y, max_x, max_y)) {
            DEB("fdd xy=" << x << "," << y << "\n");
            if (not t[x].count(y)) {
                break;
            }
            // if (is_corner(x, y, dx, dy, max_x, max_y)) {
            //     // Bedzie obsluzony w dalszej czesci count_arms_in_query().
            //     break;
            // }
            if (is_corner(x, y, -dx, -dy, max_x, max_y)) {
                // if (not skip_first_corner) {
                results.emplace_back(x, y);
                // }
                break;
            }
            if (is_corner(x + dx, y, -dx, -dy, max_x, max_y)) {
                results.emplace_back(x + dx, y);
                break;
            }
            if (is_corner(x, y + dy, -dx, -dy, max_x, max_y)) {
                results.emplace_back(x, y + dy);
                break;
            }
            x += dx;
            y += dy;
            // skip_first_corner = false;
        }
    }

    return results;
}

int count_arms_in_query(int source_x, int source_y, int max_x, int max_y) {
    int result = 0;

    DEB("count_arms_in_query before loop\n");
    FOR(x, max(1, source_x - 1), min(max_x, source_x + 1)) {
        FOR(y, max(1, source_y - 1), min(max_y, source_y + 1)) {
            // continue;
            // if (source_x == x or source_y == y) {
            DEB("  loop: xy=" << x << ", " << y << "\n");
            int part = count_arms_from(x, y, max_x, max_y);
            DEB("  part=" << part << "\n");
            result += part;
            // }
        }
    }
    DEB("count_arms_in_query after loop\n");

    if (result) {
        return result;
    }

    DEB("count_arms_in_query: source=" << source_x << ", " << source_y << "\n");
    DEB("before find_distant_corners\n");
    vector<pii> distant_corners = find_distant_corners(source_x, source_y, max_x, max_y);
    DEB("distant_corners=" << distant_corners << "\n");
    for (const auto &[x, y] : distant_corners) {
        result += count_arms_from(x, y, max_x, max_y);
    }

    DEB("count_arms_in_query from dist corners result /= 2: " << result << " -> " << result / 2
                                                              << "\n");
    result /= 2;

    return result;
}

void inline one() {
    int max_x, max_y, initial_number, queries;
    cin >> max_x >> max_y >> initial_number >> queries;
    REP(_, initial_number) {
        int x, y;
        cin >> x >> y;
        t[x][y] = 0;
        // DEB("x=" << x << " y=" << y << "\n");
        // DEB("tst=" << t[x].count(y) << t[x + 1].count(y) << t[x].count(y + 1)
        //    << t[x + 1].count(y + 1) << "\n");
    }

    // DEB("matrix:\n");
    // FOR(x, 1, max_x) {
    //     DEB("x=" << x << ": ");
    //     for (const auto &[y, cnt] : t[x]) {
    //         DEB("y=" << y << " (" << cnt << ") ");
    //     }
    //     DEB("\n");
    // }

    int total = initial_number;
    int total_stuck = 0;
    FOR(x, 1, max_x) {
        for (const auto &[y, cnt] : t[x]) {
            mark_stuck(x, y);
        }
    }

    // DEB("marked:\n");
    // FOR(x, 1, max_x) {
    //     DEB("x=" << x << ": ");
    //     for (const auto &[y, cnt] : t[x]) {
    //         DEB("y=" << y << " (" << cnt << ") ");
    //     }
    //     DEB("\n");
    // }

    FOR(x, 1, max_x) {
        for (const auto &[y, cnt] : t[x]) {
            int stuck = count_stuck(x, y);
            t[x][y] = stuck;
            total_stuck += stuck;
        }
    }

    int total_arms = count_all_arms(max_x, max_y);
    DEB("total_arms=" << total_arms << "\n");

    cout << (total - total_stuck - total_arms) << "\n";
    REP(q, queries) {
        int x, y;
        cin >> x >> y;
        DEB("=========================\n");
        DEB("query line=" << (q + 2) << " xy=" << x << ", " << y << "\n");

        DEB("before prev_arms count_arms_in_query\n");
        int prev_arms = count_arms_in_query(x, y, max_x, max_y);

        int prev_stuck = 0;
        if (t[x].count(y)) {
            DEB("  remove: " << x << ", " << y << "\n");
            int stuck = t[x][y];
            // total_stuck -= stuck;
            prev_stuck += stuck;
            --total;
            t[x].erase(y);
        } else {
            DEB("  add: " << x << ", " << y << "\n");
            t[x][y] = 0;
            ++total;
        }

        dump(max_x, max_y);

        DEB("before mark_stuck\n");
        FOR(i, max(1, x - 2), min(max_x, x + 2)) {
            FOR(j, max(1, y - 2), min(max_y, y + 2)) {
                if (t[i].count(j)) {
                    int prev = t[i][j];
                    prev_stuck += prev;
                    mark_stuck(i, j);
                }
            }
        }
        DEB("after mark_stuck\n");
        int next_stuck = 0;

        // DEB("tab:\n");
        // FOR(i, x - 1, x + 1) {
        //     FOR(j, y - 1, y + 1) {
        //         if (t[i].count(j)) {
        //             DEB(t[i][j]);
        //         } else {
        //             DEB("-");
        //         }
        //     }
        //     DEB("\n");
        // }
        DEB("before count_stuck\n");
        FOR(i, max(1, x - 2), min(max_x, x + 2)) {
            FOR(j, max(1, y - 2), min(max_y, y + 2)) {
                if (t[i].count(j)) {
                    int stuck = count_stuck(i, j);
                    t[i][j] = stuck;
                    next_stuck += stuck;
                }
            }
        }
        DEB("after count_stuck\n");
        total_stuck += (next_stuck - prev_stuck);

        DEB("before next_arms count_arms_in_query\n");
        int next_arms = count_arms_in_query(x, y, max_x, max_y);

        DEB("prev_arms=" << prev_arms << "\n");
        DEB("next_arms=" << next_arms << "\n");
        total_arms += (next_arms - prev_arms);

        DEB("prev_stuck=" << prev_stuck << "\n");
        DEB("next_stuck=" << next_stuck << "\n");
        DEB("total=" << total << "\n");
        DEB("total_stuck=" << total_stuck << "\n");

        cout << (total - total_stuck - total_arms) << "\n";
        DEB("\n\n");

#ifdef DEBUG
        if (q + 2 >= 21979) {
            exit(1);
        }
#endif
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    // int z; cin >> z; while(z--)
    one();
}