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
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#ifdef LOCAL
#include "../debug/debug.h"
#else
#define debug(...)
#define debugArr(...)
#endif

using namespace std;
using namespace __gnu_pbds;

using ll = long long;
using db = long double;
using pi = pair<int,int>;
using pl = pair<ll,ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vb = vector<bool>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vvi = vector<vi>;

#define mp make_pair
#define eb emplace_back
#define pb push_back
#define x first
#define y second
#define sz(x) int((x).size())
#define bg(x) begin(x)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(), (x).rend() 
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define ft front()
#define bk back()
#define rsz resize
#define ins insert
#define each(a,x) for(auto&a:x)

template<class T> bool ckmin(T& a, T b) { return b<a?a=b,1:0; }
template<class T> bool ckmax(T& a, T b) { return b>a?a=b,1:0; }
template<class T> int lwb(vector<T>& a, const T& b) { return int(lower_bound(all(a),b)-bg(a)); }
template<class T> int upb(vector<T>& a, const T& b) { return int(upper_bound(all(a),b)-bg(a)); }
template<class T> void remdup(vector<T>& v) { sort(all(v)); v.erase(unique(all(v)),end(v)); }

constexpr int pct(int x) { return __builtin_popcount(x); }
constexpr int bitlog(int x) { return x == 0 ? 0 : 31 - __builtin_clz(x); }
constexpr int pct(ll x) { return __builtin_popcountll(x); }
constexpr int bitlog(ll x) { return x == 0 ? 0 : 63 - __builtin_clzll(x); }

constexpr ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); }  // divide a by b rounded up
constexpr ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }  // divide a by b rounded down
constexpr int cdiv(int a, int b) { return a / b + ((a ^ b) > 0 && a % b); }  // divide a by b rounded down
constexpr int fdiv(int a, int b) { return a / b - ((a ^ b) < 0 && a % b); }  // divide a by b rounded down


// UP, LEFT, DOWN, RIGHT
const pi MOVES[] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};

#ifdef LOCAL
mt19937_64 rng(0xabadbeef);
#else
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif

ll randr(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rng); }

const int N = 2e5+32;
const int K = 75032;
unordered_set<int> _grid[N];
unordered_map<int, int> cached_type[N];
unordered_map<int, int> cached_color[N];
int n, m, k, q;
int sx[K], sy[K];
int qx[K], qy[K];
int ans;

namespace Grid {

inline bool taken(int i, int j) { 
    return _grid[i].find(j) != end(_grid[i]); 
}

inline void set(int i, int j) { _grid[i].insert(j); }

inline void unset(int i, int j) { _grid[i].erase(j); }

inline bool flip(int i, int j) {
    auto [it, inserted] = _grid[i].insert(j);
    if (!inserted) {
        _grid[i].erase(it);
        return false;
    }
    return true;
}

};

const int NONE = 0;

// .x.
// .o.
// ...
const int UP = 0;

// ...
// xo.
// ...
const int LEFT = 1;

// ...
// .o.
// .x.
const int DOWN = 2;

// ...
// .ox
// ...
const int RIGHT = 3;

// .x.
// xo.
// ...
const int LU = 1;

// .x.
// .ox
// ...
const int RU = 2;

// ...
// xo.
// .x.
const int LD = 3;

// ...
// .ox
// .x.
const int RD = 4;

// LD i RU z lewa dołu w prawo góra -> "POS"
// LU i RD z lewa góry w prawo dół  -> "NEG"

const int LEAF          = 1;
const int LEAF_2        = 2;
const int STABLE        = 3;

struct Entry {
    int lx, ly, rx, ry;
    bool removed;
    
    inline int len() const {
        return abs(rx - lx) + abs(ry - ly) + 1;
    }
};

struct CMP_POS {
    constexpr inline bool cmp(const pi& a, const pi& b) const {
        return a.y == b.y ? a.x > b.x : a.y < b.y;
    }
    constexpr inline bool operator()(const Entry& a, const Entry& b) const {
        return cmp({a.lx, a.ly}, {b.lx, b.ly});
    }
};

struct CMP_NEG {
    constexpr inline bool cmp(const pi& a, const pi& b) const {
        return a.y == b.y ? a.x < b.x : a.y < b.y; 
    }
    constexpr inline bool operator()(const Entry& a, const Entry& b) const {
        return cmp({a.lx, a.ly}, {b.lx, b.ly});
    }
};

using SetPos = set<Entry, CMP_POS>;
using SetNeg = set<Entry, CMP_NEG>;

SetPos set_pos[2 * N];
SetNeg set_neg[2 * N];

constexpr bool is_pos(int type) {
    switch (type){
        case LU:
        case RD:
            return true;
        case LD:
        case RU:
            return false;
        default:
            return false;
    }
}

constexpr int get_idx(int x, int y, int type) {
    switch (type) {
        case LU:
            return x + y;
        case RD:
            return x + y + 1;
        case LD:
            return N - x + y;
        case RU:
            return N - x + y + 1;
        default:
            return -1;
    }
}

array<bool, 4> get_nei(int x, int y) {
    array<bool, 4> res;
    rep(i,0,4) {
        auto [dx, dy] = MOVES[i];
        int nx = x + dx;
        int ny = y + dy;
        res[i] = Grid::taken(nx, ny);
    }
    
    return res;
}

bool is_leaf(int x, int y) {
    if (!Grid::taken(x + 1, y) && !Grid::taken(x - 1, y)) return true;
    if (!Grid::taken(x, y - 1) && !Grid::taken(x, y + 1)) return true;
    return false;
}

array<bool, 4> get_non_leaf_nei(int x, int y) {
    auto nei = get_nei(x, y);
    
    rep(i,0,4) {
        auto [dx, dy] = MOVES[i];
        int nx = x + dx;
        int ny = y + dy;
        if (nei[i] && is_leaf(nx, ny)) {
            nei[i] = false;
        }
    }

    return nei;
}

int get_type(int x, int y) {
    auto nei = get_non_leaf_nei(x, y);
    int cnt = accumulate(all(nei), 0);
    
    if (cnt != 2) {
        return NONE;
    }

    if (nei[UP]) {
        if (nei[LEFT]) {
            return LU;
        }
        
        if (nei[RIGHT]) {
            return RU;
        }

        return NONE;
    }

    if (nei[DOWN]) {
        if (nei[LEFT]) {
            return LD;
        }

        if (nei[RIGHT]) {
            return RD;
        }
        
        return NONE;
    }

    return NONE;
}

bool is_leaf_2(int x, int y) {
    auto nei = get_non_leaf_nei(x, y);

    if (!nei[UP] && !nei[DOWN]) return true;
    if (!nei[LEFT] && !nei[RIGHT]) return true;
    return false;
}

bool connected_to_leaf_2(int x, int y) {
    for (auto [dx, dy] : MOVES) {
        int nx = x + dx;
        int ny = y + dy;
        if (!Grid::taken(nx, ny)) continue;
        if (cached_color[nx][ny] == LEAF_2) {
            return true;
        }
    }
    return false;
}

// a < b
inline bool mergable(const Entry& a, const Entry& b) {
    return abs(a.rx - b.lx) + abs(a.ry - b.ly) == 1;
}

inline bool connected_to_leaf_2(const Entry& entry) {
    return connected_to_leaf_2(entry.lx, entry.ly) || connected_to_leaf_2(entry.rx, entry.ry);
}

template<class T>
void add_impl(T& s, int x, int y) {
    Entry entry{x, y, x, y, false};

    auto it = s.lower_bound(entry);
    if (it != s.end() && mergable(entry, *it)) {
        entry.rx = it->rx;
        entry.ry = it->ry;
        if (it->removed) {
            ans -= it->len();
        }
        it = s.erase(it);
    }

    if (it != s.begin() && mergable(*(--it), entry)) {
        entry.lx = it->lx;
        entry.ly = it->ly;
        if (it->removed) {
            ans -= it->len();
        }
        s.erase(it);
    }

    if (connected_to_leaf_2(entry)) {
        entry.removed = true;
        ans += entry.len();
    }

    s.insert(entry);
}

pi get_prv(int x, int y, int type) {
    switch (type) {
        case LU:
            return {x, y - 1};
        case RD:
            return {x + 1, y};
        case LD:
            return {x, y - 1};
        case RU:
            return {x - 1, y};
        default:
            return {-1, -1};
    }
}

pi get_nxt(int x, int y, int type) {
    switch (type) {
        case LU:
            return {x - 1, y};
        case RD:
            return {x, y + 1};
        case LD:
            return {x + 1, y};
        case RU:
            return {x, y + 1};
        default:
            return {-1, -1};
    }
}

template<class T>
void remove_impl(T& s, int x, int y, int type) {
    Entry entry{x, y, x, y, false};
    
    auto it = s.upper_bound(entry);
    it--;

    if (it->removed) {
        ans -= it->len();
    }

    entry = *it;
    s.erase(it);

    if (entry.lx != x || entry.ly != y) {
        auto [px, py] = get_prv(x, y, type);
        Entry lhs{entry.lx, entry.ly, px, py, false};
        if (connected_to_leaf_2(lhs)) {
            lhs.removed = true;
            ans += lhs.len();
        }
        s.ins(lhs);
    }

    if (entry.rx != x || entry.ry != y) {
        auto [nx, ny] = get_nxt(x, y, type);
        Entry rhs{nx, ny, entry.rx, entry.ry, false};
        if (connected_to_leaf_2(rhs)) {
            rhs.removed = true;
            ans += rhs.len();
        }
       s.ins(rhs);
    }
}

void add_to_diagonal(int x, int y, int type) {
    int idx = get_idx(x, y, type);

    if (is_pos(type)) {
        add_impl(set_pos[idx], x, y);
    }
    else {
        add_impl(set_neg[idx], x, y);
    }
}

void remove_from_diagonal(int x, int y, int type) {
    int idx = get_idx(x, y, type);

    if (is_pos(type)) {
        remove_impl(set_pos[idx], x, y, type);
    }
    else {
        remove_impl(set_neg[idx], x, y, type);
    }
}

// najpierw trzeba LEAF, potem LEAF_2, inaczej się źle zrobi
void reset(int x, int y) {
    int old_type = cached_type[x][y];
    if (old_type != NONE) {
        cached_type[x][y] = NONE;
        remove_from_diagonal(x, y, old_type);
    }

    int old_color = cached_color[x][y];
    if (old_color == LEAF || old_color == LEAF_2) {
        ans--;
    }

    if (is_leaf(x, y)) {
        cached_color[x][y] = LEAF;
        ans++;
        return;
    }

    if (is_leaf_2(x, y)) {
        cached_color[x][y] = LEAF_2;
        ans++;
        return;
    }

    // Nie jestem liściem pierwszego ani drugiego rzędu
    // Nie jestem również na ładnej diagonali
    cached_color[x][y] = STABLE;
}

// (x, y) should be already removed/added
void recurrent_reset(int x, int y) {
    for (auto [dx, dy] : MOVES) {
        int nx = x + dx;
        int ny = y + dy;
        if (Grid::taken(nx, ny)) {
            reset(nx, ny);
        }
    }

    // głupi for, na razie dla testów powinien działać
    rep(dx,-2,3) {
        rep(dy,-2,3) {
            if (dx == 0 && dy == 0) continue;
            int nx = x + dx;
            int ny = y + dy;

            if (Grid::taken(nx, ny)) {
                reset(nx, ny);
            }
        }
    }
}

void flip_and_reset(int x, int y) {
    bool added = Grid::flip(x, y);

    if (added) {
        reset(x, y);
    }
    else {
        int old_type = cached_type[x][y];
        if (old_type != NONE) {
            cached_type[x][y] = NONE;
            remove_from_diagonal(x, y, old_type);
        }

        if (cached_color[x][y] == LEAF || cached_color[x][y] == LEAF_2) {
            cached_color[x][y] = NONE;
            ans--;
        }
    }

    recurrent_reset(x, y);

    // jeśli byłem styczny z liśćmi to fałszywie mogłem stwierdzić
    // że jestem liściem drugiego rzędu
    if (added) {
        reset(x, y);
    }

    rep(dx,-2,3) {
        rep(dy,-2,3) {
            int nx = x + dx;
            int ny = y + dy;

            if (Grid::taken(nx, ny)) {
                int type = get_type(nx, ny);
                cached_type[nx][ny] = type;

                if (type != NONE) {
                    add_to_diagonal(nx, ny, type);
                }
            }
        }
    }
}

void solve() {
    rep(i,0,k) {
        flip_and_reset(sx[i], sy[i]);
    }

    cout << ans << '\n';

    rep(i,0,q) {
        flip_and_reset(qx[i], qy[i]);
        cout << ans << '\n';
    }
}

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

    cin >> n >> m >> k >> q;

    mt19937 gen(2137);

    rep(i,0,k) {
        cin >> sx[i] >> sy[i];
        sx[i] += 5;
        sy[i] += 5;
    }
    rep(i,0,q) {
        cin >> qx[i] >> qy[i];
        qx[i] += 5;
        qy[i] += 5;
    }

    solve();

    return 0;
}