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

using llong = long long;

int n, m;
constexpr int dx[4] = { 0, 0, -1, 1 };
constexpr int dy[4] = { -1, 1, 0, 0 };

inline llong idx(int x, int y) {
    return 1LL * (x - 1) * m + (y - 1);
}

inline pair<int,int> coord(llong v) {
    return { int(v / m) + 1, int(v % m) + 1 };
}

inline bool removable(llong v, const unordered_set<llong>& state) {
    auto [x, y] = coord(v);
    bool left = (y > 1) && (state.find(idx(x, y - 1)) != state.end());
    bool right = (y < m) && (state.find(idx(x, y + 1)) != state.end());
    bool up = (x > 1) && (state.find(idx(x - 1, y)) != state.end());
    bool down = (x < n) && (state.find(idx(x + 1, y)) != state.end());
    return ((!left && !right) || (!up && !down));
}

int simulate(unordered_set<llong> state) {
    vector<llong> q;
    q.reserve(state.size());
    for (llong v : state) {
        if (removable(v, state)) q.push_back(v);
    }
    
    int count = 0;
    for (int i = 0; i < q.size(); i++) {
        llong v = q[i];
        if (state.find(v) == state.end()) continue;
        state.erase(v);
        count++;
        auto [x, y] = coord(v);
        
        if (y > 1) {
            llong nv = idx(x, y - 1);
            if (state.find(nv) != state.end() && removable(nv, state)) q.push_back(nv);
        }
        if (y < m) {
            llong nv = idx(x, y + 1);
            if (state.find(nv) != state.end() && removable(nv, state)) q.push_back(nv);
        }
        if (x > 1) {
            llong nv = idx(x - 1, y);
            if (state.find(nv) != state.end() && removable(nv, state)) q.push_back(nv);
        }
        if (x < n) {
            llong nv = idx(x + 1, y);
            if (state.find(nv) != state.end() && removable(nv, state)) q.push_back(nv);
        }
    }
    return count;
}

void solve() {
    int k, q;
    cin >> n >> m >> k >> q;
    
    unordered_set<llong> state;
    state.reserve(k + q);
    
    int x, y;
    for (int i = 0; i < k; i++){
        cin >> x >> y;
        state.insert(idx(x, y));
    }
    
    cout << simulate(state) << "\n";
    
    for (int i = 0; i < q; i++) {
        cin >> x >> y;
        llong v = idx(x, y);
        if (state.find(v) != state.end()) state.erase(v);
        else state.insert(v);
        cout << simulate(state) << "\n";
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    solve();
    return 0;
}