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

const int MAXN = 150000 + 5;
unordered_map <unsigned long long, int> mp;
array <int, 4> g[MAXN];
bool unlock[MAXN], on[MAXN], redo = false;
int idx = 1;

unsigned long long get_key(int a, int b) {
    return 200003 * a + b;
}

bool is_isol(int v) {
    return ((unlock[g[v][0]] && unlock[g[v][2]]) || (unlock[g[v][1]] && unlock[g[v][3]]));
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    unlock[0] = true;

    int n, m, k, q;
    cin >> n >> m >> k >> q;
    for (int i = 0; i < k; i++) {
        int a, b;
        cin >> a >> b;
        auto key = get_key(a, b);
        mp[key] = idx;
        unlock[idx] = false;
        on[idx] = true;
        g[idx] = {0, 0, 0, 0};
        if (mp.count(get_key(a - 1, b))) {
            int x = mp[get_key(a - 1, b)];
            g[idx][3] = x;
            g[x][1] = idx;
        }
        if (mp.count(get_key(a + 1, b))) {
            int x = mp[get_key(a + 1, b)];
            g[idx][1] = x;
            g[x][3] = idx;
        }
        if (mp.count(get_key(a, b - 1))) {
            int x = mp[get_key(a, b - 1)];
            g[idx][0] = x;
            g[x][2] = idx;
        }
        if (mp.count(get_key(a, b + 1))) {
            int x = mp[get_key(a, b + 1)];
            g[idx][2] = x;
            g[x][0] = idx;
        }
        idx++;
    }

    queue <int> Q;
    for (int i = 1; i < idx; i++) {
        if (is_isol(i)) {
            unlock[i] = true;
            Q.push(i);
        }
    }

    int ans = 0;
    while (!Q.empty()) {
        int cur = Q.front();
        Q.pop();
        ans++;

        for (auto e : g[cur]) if (e != 0) {
            if (!unlock[e] && is_isol(e)) {
                Q.push(e);
                unlock[e] = true;
            }
        }
    }
    cout << ans << '\n';

    while (q--) {
        int a, b;
        cin >> a >> b;
        int v;
        if (mp.count(get_key(a, b)) == 0) {
            v = idx;
            mp[get_key(a, b)] = idx;
            unlock[idx] = false;
            on[idx] = false;
            g[idx] = {0, 0, 0, 0};
            if (mp.count(get_key(a - 1, b))) {
                int x = mp[get_key(a - 1, b)];
                g[idx][3] = x;
                g[x][1] = idx;
            }
            if (mp.count(get_key(a + 1, b))) {
                int x = mp[get_key(a + 1, b)];
                g[idx][1] = x;
                g[x][3] = idx;
            }
            if (mp.count(get_key(a, b - 1))) {
                int x = mp[get_key(a, b - 1)];
                g[idx][0] = x;
                g[x][2] = idx;
            }
            if (mp.count(get_key(a, b + 1))) {
                int x = mp[get_key(a, b + 1)];
                g[idx][2] = x;
                g[x][0] = idx;
            }
            idx++;
        } else v = mp[get_key(a, b)];

        if (on[v]) {
            on[v] = false;
            if (unlock[v]) ans--;
            for (int i = 0; i < 4; i++) {
                int e = g[v][i];
                if (e == 0) continue;
                g[e][(i + 2) % 4] = 0;
                g[v][i] = 0;
                if (!unlock[e] && is_isol(e)) {
                    Q.push(e);
                    unlock[e] = true;
                } 
            }

            while (!Q.empty()) {
                int cur = Q.front();
                Q.pop();
                ans++;

                for (auto e : g[cur]) if (e != 0) {
                    if (!unlock[e] && is_isol(e)) {
                        Q.push(e);
                        unlock[e] = true;
                    }
                }
            }
        } else {
            on[v] = true;
            if (mp.count(get_key(a - 1, b))) {
                int x = mp[get_key(a - 1, b)];
                g[v][3] = x;
                g[x][1] = v;
            }
            if (mp.count(get_key(a + 1, b))) {
                int x = mp[get_key(a + 1, b)];
                g[v][1] = x;
                g[x][3] = v;
            }
            if (mp.count(get_key(a, b - 1))) {
                int x = mp[get_key(a, b - 1)];
                g[v][0] = x;
                g[x][2] = v;
            }
            if (mp.count(get_key(a, b + 1))) {
                int x = mp[get_key(a, b + 1)];
                g[v][2] = x;
                g[x][0] = v;
            }
            
            for (int i = 1; i < idx; i++) unlock[i] = false;
            for (int i = 1; i < idx; i++) if (on[i] && is_isol(i)) {
                unlock[i] = true;
                Q.push(i);
            }
            ans = 0;
            while (!Q.empty()) {
                int cur = Q.front();
                Q.pop();
                ans++;

                for (auto e : g[cur]) if (e != 0) {
                    if (!unlock[e] && is_isol(e)) {
                        Q.push(e);
                        unlock[e] = true;
                    }
                }
            }
        }
        cout << ans << '\n';
    }
}