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
#include <bits/stdc++.h>
using namespace std;
#define rep(a, b) for (int a = 0; a < (b); a++)
#define rep1(a, b) for (int a = 1; a <= (b); a++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int MOD = 1e9 + 7;

const int XD = 21372137;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

#define LOCAL false

/*
jak dodaje:
$. znajdź wszystkich ziomków w kwadracie 3x3 którzy stają się cyklem
0. dodaj ziomkow z ($) do map przekątnych z cyklami oraz dla kazdego +1 do locked jezeli jeszcze nie byl zablokowany
1. dodaj siebie do przekątnych
2. dla każdego z ($) szukaj, czy zamyka jakiś segment (dokładając drugie zamknięcie do niezamkniętego sementu) (we wszystkie 4 strony)
3. jak ziomek którego dodaje nie jest cyklem, to sprawdź dla obu przekątnych czy zamyka jakiś segment (łącząc 2 niezamknięte segmenty)

jak usuwam:
$. znajdź wszystkich ziomków w kwadracie 3x3 którzy przestają być cyklem
3'. jak ziomek którego usuwam nie jest cyklem, to sprawdź dla obu przekątnych czy otwiera jakiś segment (odcinając zamknięty segment)
2'. dla kazdego z ($) szukaj, czy otwiera jakiś segment (wywalająć jedno z zamknięć) (we wszystkie 4 strony)
1'. usun siebie z przekątnych
0'. usun ziomkow z ($) z map przekątnych z cyklami oraz dla kazdego -1 do locked juz nie jest zablokowany
*/

const int LIM = 2e5 + 7;
int R, C, k, q;
set<pii> pts;
int locked = 0;
 
ordered_set diag[5*LIM];
ordered_set diagcyc[5*LIM];

int findpre(ordered_set& st, int idx) {
    auto it = st.lower_bound(idx);
    return (it != st.begin() ? *(--it) : XD);
}
int findnxt(ordered_set& st, int idx) {
    auto it = st.lower_bound(idx+1);
    return (it != st.end() ? *it : XD);
}
bool bruh = false;
bool connected(ordered_set& st, int l, int r) {
    int lidx = st.order_of_key(l);
    int ridx = st.order_of_key(r+1)-1;
    // if (bruh) {
    //     cout << l << " " << r << "??\n";
    //     cout << lidx << ' ' << ridx << "????\n";
    //     for (int val: st) cout << val << " ";
    //     cout << " iksde\n";
    // }
    return r-l == ridx-lidx;
}

const int UL = 1;
const int UR = 2;
const int DL = 4;
const int DR = 3;

bool bitmap[2][5][5];
bool iscycle[2][5][5];
bool cool[5][5];
vector<int> tags[2][5][5];
void genbitmaps(int r, int c) {
    for (int dr = -2; dr <= 2; dr++) for (int dc = -2; dc <= 2; dc++) {
        bitmap[0][dr+2][dc+2] = pts.count({r+dr, c+dc});
        bitmap[1][dr+2][dc+2] = bitmap[0][dr+2][dc+2];
    }
    bitmap[1][2][2] ^= 1;

    rep(x, 2) rep(r, 5) rep(c, 5) iscycle[x][r][c] = false;
    rep(x, 2) rep(r, 5) rep(c, 5) tags[x][r][c].clear();
    rep(x, 2) rep(r, 4) rep(c, 4) {
        if (bitmap[x][r][c]&&bitmap[x][r+1][c]&&bitmap[x][r][c+1]&&bitmap[x][r+1][c+1]) {
            rep(dr, 2) rep(dc, 2) iscycle[x][r+dr][c+dc] = true;
            if (1 <= r && r <= 2 && 1 <= c && c <= 2) {
                tags[x][r][c].push_back(UL);
                tags[x][r][c+1].push_back(UR);
                tags[x][r+1][c].push_back(DL);
                tags[x][r+1][c+1].push_back(DR);
            }
        }
    }
    rep(r, 5) rep(c, 5) cool[r][c] = iscycle[0][r][c]^iscycle[1][r][c];
}

vector<pii> diagdata(int r, int c) {
    return { {2*(r+c-1), 2*r-1}, {2*(r+c), 2*r}, {2*(LIM+r-c-1)+1, 2*r-1}, {2*(LIM+r-c)+1, 2*r} };
}

bool islocked(int r, int c) {
    for (auto [di, idx]: diagdata(r, c)) {
        int pre = findpre(diagcyc[di], idx);
        int nxt = findnxt(diagcyc[di], idx);
        if (pre != XD && nxt != XD && connected(diag[di], pre, nxt)) return true;
    }
    return false;
}


void add(int r, int c) {
    // $.
    genbitmaps(r, c);
    vector<pair<pii, int>> addcycles;
    rep1(dr, 3) rep1(dc, 3) if (cool[dr][dc]) {
        int r2 = r+dr-2;
        int c2 = c+dc-2;
        if (!islocked(r2, c2)) locked++;
        if (tags[1][dr][dc].size() == 1) addcycles.push_back(pair{pair{r2, c2}, tags[1][dr][dc][0]});
    }
    
    
    for (auto [di, idx]: diagdata(r, c)) {
        diag[di].insert(idx);
    }
    
    // 3'.
    if (!iscycle[1][2][2]) {
        for (auto [di, idx]: diagdata(r, c)) {
            int pre = findpre(diagcyc[di], idx);
            int nxt = findnxt(diagcyc[di], idx);
            if (pre != XD && nxt != XD && connected(diag[di], pre, nxt)) locked += nxt-pre-1;
        }
    }
    
    // 2. 
    for (auto& [pt, tg]: addcycles) {
        auto& [r2, c2] = pt;
        for (auto [di, idx]: diagdata(r2, c2)) {
            if (di%2 != tg%2) continue;
            int pre = findpre(diagcyc[di], idx);
            int nxt = findnxt(diagcyc[di], idx);
            
            if ((r2 != r || c2 != c) && pre != XD && nxt != XD && connected(diag[di], pre, nxt)) continue;
            
            // if (r == 4 && c == 3 && r2 == 3 && c2 == 2 && di == 400017) {
            //     cout << di << "\n";
            //     cout << idx << " " << pre << " " << connected(diag[di], pre, idx-1)<< "??\n";
            //     bruh = true;
            //     connected(diag[di], pre, idx-1);
            //     bruh = false;
            //     for (int val: diag[di]) cout << val << " ";
            //     cout << " basd\n";
            // }
            if ((tg==UL || tg==UR) && pre != XD && connected(diag[di], pre, idx-1)) locked += idx-pre-1;
            if ((tg==DL || tg==DR) && nxt != XD && connected(diag[di], idx+1, nxt)) locked += nxt-idx-1;
        }
    }
    for (auto [pt, tg]: addcycles) for (auto [di, idx]: diagdata(pt.first, pt.second)) diagcyc[di].insert(idx);

    
    // 0.
    // cout << locked << "??\n";
}
void rem(int r, int c) {
    // $.
    genbitmaps(r, c);
    vector<pair<pii, int>> remcycles;
    rep1(dr, 3) rep1(dc, 3) if (cool[dr][dc]) {
        remcycles.push_back(pair{pair{r+dr-2, c+dc-2}, tags[0][dr][dc][0]});
        for (auto [di, idx]: diagdata(r+dr-2, c+dc-2)) diagcyc[di].erase(idx);
    }
    
    // 3'.
    if (!iscycle[0][2][2]) {
        for (auto [di, idx]: diagdata(r, c)) {
            int pre = findpre(diagcyc[di], idx);
            int nxt = findnxt(diagcyc[di], idx);
            if (pre != XD && nxt != XD && connected(diag[di], pre, nxt)) locked -= nxt-pre-1;
        }
    }
    
    
    // 2. 
    for (auto& [pt, tg]: remcycles) {
        auto& [r2, c2] = pt;
        for (auto [di, idx]: diagdata(r2, c2)) {
            if (di%2 != tg%2) continue;
            int pre = findpre(diagcyc[di], idx);
            int nxt = findnxt(diagcyc[di], idx);

            int dr = r2-r+2;
            int dc = c2-c+2;
            
            if ((r2 != r || c2 != c) && pre != XD && nxt != XD && connected(diag[di], pre, nxt)) continue;
            
            if (((tg==UL && !cool[dr][dc-1] && !cool[dr-1][dc]) || (tg==UR && !cool[dr][dc+1] && !cool[dr-1][dc])) && pre != XD && connected(diag[di], pre, idx-1)) locked -= idx-pre-1;
            if (((tg==DL && !cool[dr][dc-1] && !cool[dr+1][dc]) || (tg==DR && !cool[dr][dc+1] && !cool[dr+1][dc])) && nxt != XD && connected(diag[di], idx+1, nxt)) locked -= nxt-idx-1;
        }
    }
    // if (r == 1 && c == 2) cout << locked << "???\n";
    
    // 1.
    for (auto [di, idx]: diagdata(r, c)) diag[di].erase(idx);
    
    // 0.
    for (auto [pt, tg]: remcycles) if (!islocked(pt.first, pt.second)) locked--; // przyspieszyc?

    // cout << locked << "??\n";
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    if (LOCAL) {
        ignore=freopen("io/in", "r", stdin);
        ignore=freopen("io/out", "w", stdout);
    }

    cin >> R >> C >> k >> q;
    int r, c;
    rep(i, k) {
        cin >> r >> c;
        add(r, c);
        pts.insert({r, c});
    }
    cout << (int)pts.size() - locked << "\n";
    rep(i, q) {
        cin >> r >> c;
        if (pts.count({r, c})) {
            rem(r, c);
            pts.erase({r, c});
        } else {
            add(r, c);
            pts.insert({r, c});
        }
        cout << (int)pts.size() - locked << "\n";
        
        // if (i == 59445) {
        //     for (auto [x, y]: pts) cout << x << " " << y << "\n";
        //     cout << r << " " << c << "!!\n";
        // }
    }

    return 0;
}