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

#define all(a) begin(a), end(a)
using ll = long long;
using PII = pair<int, int>;

int solve(set<PII> a) {
    auto check = [&](PII p) {
        auto [x, y] = p;
        if (!a.count({x - 1, y}) && !a.count({x + 1, y}))
            return true;

        if (!a.count({x, y - 1}) && !a.count({x, y + 1}))
            return true;

        return false;
    };

    queue<PII> q;
    auto push_rem = [&](PII p) {
        if (a.count(p) && check(p)) {
            q.push(p);
            a.erase(p);
        }
    };

    vector<PII> b;
    for (auto p : a)
        if (check(p)) {
            b.push_back(p);
            q.push(p);
        }

    for (auto i : b)
        a.erase(i);

    int result = 0;
    while (!q.empty()) {
        auto [x, y] = q.front();
        q.pop();
        result++;

        push_rem({x - 1, y});
        push_rem({x + 1, y});
        push_rem({x, y - 1});
        push_rem({x, y + 1});
    }

    return result;
}

void solve() {
    int n, m, k, q;
    cin >> n >> m >> k >> q;

    vector<PII> a(k);
    for (auto &[i, j] : a) {
        cin >> i >> j;
        --i;
        --j;
    }

    set<PII> s(all(a));
    cout << solve(s) << "\n";
    while (q--) {
        int i, j;
        cin >> i >> j;
        --i;
        --j;

        auto it = s.find({i, j});
        if (it != s.end()) {
            s.erase(it);
        } else {
            s.insert({i, j});
        }
        cout << solve(s) << "\n";
    }
}

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

    int tests = 1;
    // cin >> tests;

    while (tests--)
        solve();
}