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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include "bits/stdc++.h"
using namespace std;
#define rep(i,a,b) for(int i=(a); i<(b); ++i)
#define all(x) x.begin(),x.end()
#define sz(x) int(x.size())
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;

struct pt{
    int X, Y;
    bool operator<(const pt b) const {
        if (X==b.X) return Y<b.Y;
        return X<b.X;
    };
};

const int N = 2e5+10;

void read(pt& p){
    int x,y; cin >> x >> y;
    p = {x,y};
}

pair<int,int> toChain(pt p){
    return {p.X+p.Y,p.X+p.Y+1};
}

int toIndex(pt p){
    return p.X-p.Y;
}

struct stuk{
    int l, r; // closed
    bool operator<(const stuk& b) const {
        return l < b.l;
    }bool operator>(const stuk& b) const {
        return l > b.l;
    }
};

set<pt> allNodes;
set<pt> blokNodes;

set<int> badChain[N*2];
set<stuk> allChains[N*2];

int badChainCount = 0;

stuk combine(int id, stuk A, stuk B){
    stuk nw = {A.l, B.r};
    allChains[id].insert(nw);
    return nw;
}

// A smaller than B
stuk mergeChains(int id, stuk A, stuk B){
    allChains[id].erase(A);
    allChains[id].erase(B);

    /*
        find first bad in A. This is --(upperbound A.r)
        if it fits in A
    */
    auto it = badChain[id].upper_bound(A.r);
    if (it == begin(badChain[id])) return combine(id,A,B); // no removals
    --it;
    if (*it < A.l) return combine(id,A,B); // no bad in chain

    /*
        find first bad in B. This is lowerbound(B.l)
    */
    auto itr = badChain[id].lower_bound(B.l);
    if (itr == end(badChain[id])) return combine(id,A,B);
    if (*itr > B.r) return combine(id,A,B);

    int newbad = (*itr - *it - 1);
    badChainCount += newbad;
    return combine(id,A,B);
}

// sets A to be the right chain, if it exists, 
// and returns whether a chain contains this node
bool findChain(stuk& A, int id, int pos){
    stuk dummy = {pos,pos};
    auto it = allChains[id].upper_bound(dummy);
    if (it != begin(allChains[id])){
        A = *(--it);
        return A.r >= pos;
    }
    return false;
}

void cutChain(int id, stuk A, int pos){
    // remove bad counts
    if (badChain[id].count(pos)){
        badChainCount--; // remove node itself

        auto it = badChain[id].find(pos);
        if (it != begin(badChain[id])){
            auto lit = it; --lit;
            if (*lit >= A.l){
                int rembad = pos - (*lit) - 1;
                badChainCount -= rembad;
            }
        }{
            auto rit = it; ++rit;
            if (rit != end(badChain[id])){
                if (*rit <= A.r){
                    int rembad = (*rit) - pos - 1;
                    badChainCount -= rembad;
                }
            }
        }
        // remove bad node from list
        badChain[id].erase(pos);
    }else{
        auto rit = badChain[id].upper_bound(pos);
        if (rit != begin(badChain[id]) and rit != end(badChain[id])){
            auto lit = rit; --lit;
            if (*lit >= A.l and *rit <= A.r){
                int rembad = (*rit) - (*lit) - 1;
                badChainCount -= rembad;
            }
        }
    }

    // make new chains
    allChains[id].erase(A);
    stuk L = {A.l, pos-1};
    stuk R = {pos+1,A.r};

    if (L.l <= L.r){
        allChains[id].insert(L);
    }
    if (R.l <= R.r){
        allChains[id].insert(R);
    }
}

void addNode(pt p, bool bad){
    auto [id1, id2] = toChain(p);
    auto pos = toIndex(p);

    if (bad) {
        badChainCount += 2; // accounts for both things
        badChain[id1].insert(pos);
        badChain[id2].insert(pos);
        blokNodes.insert(p);
    }

    for(auto id : {id1, id2}){
        stuk me = {pos,pos};
        allChains[id].insert(me);

        stuk L;
        if (findChain(L, id, pos-1)){
            me = mergeChains(id,L,me);
        }

        stuk R;
        if (findChain(R, id, pos+1)){
            me = mergeChains(id,me,R);
        }
    }
}

void remNode(pt p){
    auto [id1, id2] = toChain(p);
    auto pos = toIndex(p);
    blokNodes.erase(p); // just to be sure

    for(auto id : {id1, id2}){
        stuk A;
        if (findChain(A,id,pos)){
            cutChain(id,A,pos);
        }
    }
}

void makeNodeBad(pt p){
    remNode(p);
    addNode(p, true);
}

void makeNodeGood(pt p){
    remNode(p);
    addNode(p, false);
}

void ADD(pt p){
    array<array<bool,3>,3> newbad = {array<bool,3>{0,0,0},{0,0,0},{0,0,0}};
    auto isthere = newbad;
    // mark existing nodes
    isthere[1][1] = 1; // mark new node as existing
    rep(dx,-1,2) rep(dy,-1,2) if (dx!=0 or dy!=0){
        pt at = {p.X+dx, p.Y+dy};
        if (allNodes.count(at)) isthere[dx+1][dy+1] = true;
    }
    // mark new bad
    rep(i,0,2) rep(j,0,2){
        bool good = 1;
        rep(di,0,2) rep(dj,0,2) if (!isthere[i+di][j+dj]){
            good = 0;
            break;
        }
        if (good){
            rep(di,0,2) rep(dj,0,2){
                newbad[i+di][j+dj] = true;
            }
        }
    }
    // mark previous bad
    rep(dx,-1,2) rep(dy,-1,2) if (dx!=0 or dy != 0){
        if (newbad[dx+1][dy+1]){
            pt at = {dx + p.X, dy + p.Y};
            if (blokNodes.count(at)){
                // was already bad before
                newbad[dx+1][dy+1] = false;
            }
        }
    }

    // insert node
    addNode(p, newbad[1][1]);

    // flip all surroundings
    rep(dx,-1,2) rep(dy,-1,2) if (dx != 0 or dy != 0){
        if (newbad[dx+1][dy+1]){
            makeNodeBad({dx + p.X, dy + p.Y});
        }
    }   
}

void DEL(pt p){
    array<array<bool,5>,5> isthere, isblok;
    rep(i,0,5) rep(j,0,5) {
        isthere[i][j] = false;
        isblok[i][j] = false;
    }

    // mark existing nodes
    rep(dx,-2,3) rep(dy,-2,3) if (dx!=0 or dy!=0){
        pt at = {p.X+dx, p.Y+dy};
        if (allNodes.count(at)) isthere[dx+2][dy+2] = true;
    }

    // mark bad blocks
    rep(i,0,4) rep(j,0,4){
        bool good = true;
        rep(di,0,2) rep(dj,0,2){
            if (!isthere[i+di][j+dj]){
                good = false;
                break;
            }
        }
        if (good){
            rep(di,0,2) rep(dj,0,2){
                isblok[i+di][j+dj] = true;
            }
        }
    }

    // remove node
    remNode(p);

    // toggle rim if needed
    rep(dx,-1,2) rep(dy,-1,2) if (dx!=0 or dy!=0){
        if (!isblok[dx+2][dy+2]){
            pt at = {p.X + dx, p.Y + dy};
            if (blokNodes.count(at)){
                makeNodeGood(at);
            }
        }
    }
}

void toggle(pt p){
    if (allNodes.count(p)){
        allNodes.erase(p);
        DEL(p);
    }else{
        allNodes.insert(p);
        ADD(p);
    }
}

int main(){
    cin.tie(NULL),cin.sync_with_stdio(false);
    
    int n,m,k,q; cin >> n >> m >> k >> q;
    --k, ++q; // see zeroth query as last update on k

    vector<pt> opk(k), opq(q);
    for(auto& p : opk) read(p);
    for(auto& p : opq) read(p);

    vi total(q), numbad(q), numblok(q);

    // do first pass
    for(auto p : opk) toggle(p);
    rep(i,0,q){
        auto p = opq[i];
        toggle(p);
        total[i] = sz(allNodes);
        numbad[i] = badChainCount;
        numblok[i] = sz(blokNodes);
    }


    // reset 
    rep(i,0,N*2) {
        badChain[i].clear();
        allChains[i].clear();
    }
    allNodes.clear();
    blokNodes.clear();
    badChainCount = 0;

    // flip nodes
    for(auto& p : opk) p = {n+1-p.X, p.Y};
    for(auto& p : opq) p = {n+1-p.X, p.Y};

    // do second pass
    for(auto p : opk) toggle(p);
    rep(i,0,q){
        auto p = opq[i];
        toggle(p);
        numbad[i] += badChainCount;
    }

    rep(i,0,q){
        int tot = total[i];
        int bad = numbad[i] - numblok[i]*3;
        cout << tot-bad << '\n';
    }
}