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
#include <cstdio>
#include <cstdlib>
#include <cstdint>

#include <vector>
#include <set>

using lli = long long int;
using field_t = uint8_t;

int count_takeoffable(std::set<std::pair<int, int>> fields) {
    std::vector<std::pair<int, int>> fields_to_process{fields.begin(), fields.end()};

    int took_off = 0;

    while (!fields_to_process.empty()) {
        auto [x, y] = fields_to_process.back();
        fields_to_process.pop_back();

        auto it = fields.find({x, y});
        if (it == fields.end()) {
            continue;
        }

        if ((!fields.contains({x - 1, y}) && !fields.contains({x + 1, y})) || (!fields.contains({x, y - 1}) && !fields.contains({x, y + 1}))) {
            fields.erase(it);
            took_off++;

            auto check_nb = [&] (int dx, int dy) {
                if (fields.contains({x + dx, y + dy})) {
                    fields_to_process.push_back({x + dx, y + dy});
                }
            };
            check_nb(-1, -1);
            check_nb(-1,  0);
            check_nb(-1,  1);
            check_nb( 0,  1);
            check_nb( 1,  1);
            check_nb( 1,  0);
            check_nb( 1, -1);
            check_nb( 0, -1);
        }
    }

    return took_off;
}

int main() {
    int n, m, k, q;
    scanf("%d %d %d %d\n", &n, &m, &k, &q);

    std::set<std::pair<int, int>> fields;

    for (int i = 0; i < k; i++) {
        int x, y;
        scanf("%d %d\n", &x, &y);
        fields.insert({x, y});
    }

    printf("%d\n", count_takeoffable(fields));

    for (int i = 0; i < q; i++) {
        int x, y;
        scanf("%d %d\n", &x, &y);
        auto it = fields.find({x, y});
        if (it != fields.end()) {
            fields.erase(it);
        } else {
            fields.insert({x, y});
        }

        printf("%d\n", count_takeoffable(fields));
    }

    return 0;
}