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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include <bits/stdc++.h>
using ll = long long;
using sz = size_t;
using namespace std;

// make the code less c++-readable:
template<class T> using v = vector<T>; 
template<class T> using vv = v<v<T>>; 
using vi = v<int>; using vll = v<ll>; using vvi = vv<int>; using vvll = vv<ll>;

// hai loading utilities
#define $T template<class T>
#define $Ts template<class... T>
$T T Load() { T v; cin >> v; return v; }
$T auto Loads(int n) { v<T> v; v.reserve(n); while(n--) v.emplace_back(Load<T>()); return v; }
$T auto Loads() { return Loads<T>(Load<int>()); }
template<class T, int N> auto Loada() { array<T, N> a; for (T& v: a) v = Load<T>(); return a; }
$Ts auto Cols(int rows) { tuple<v<T>...> t; while(rows--) [&]<sz... I>(index_sequence<I...>){(std::get<I>(t).push_back(Load<T>()), ...);}(index_sequence_for<T...>{}); return t; }
//$Ts auto Rows(int rows) { v<tuple<T...>> v; while(rows--) { v.emplace_back(Load<T>()...); } return v; } bugged :(
struct _aIV { $T operator vector<T>() { return Loads<T>(n); } sz n; };
struct _aI { $T operator T() { return Load<T>(); } _aIV operator()(sz n) { return {n}; } }; static inline _aI $;  /* int N = $;  vi Y = $(N); */
#define MAKE_LOADER(T, alias) \
  T alias() { return Load<T>(); }   /* int x = Int(); */\
  auto alias##s() { return Loads<T>(); }   /* vector<> xs = Ints(); */\
  auto alias##s(int n) { return Loads<T>(n); }   /* vector<> xs = Ints(7); */\
  template<int N> auto alias##a() { return Loada<T, N>(); }   /* array<> xs = Inta<7>();  */\
// line intentionally left blank
MAKE_LOADER(int, Int)
MAKE_LOADER(long long, LL)
MAKE_LOADER(char, Char)
MAKE_LOADER(string, String)
// kthxbye


enum class Wężotyp { Górujący, Opadający };

struct Wężolina {
    Wężotyp typ;
    int linia;
    
    bool operator==(const Wężolina&) const = default;
};


struct H {
    ll operator()(const pair<int, int>& p) const {
        return ll(p.first) << 32 | p.second;
    }
    ll operator()(const Wężolina& w) const {
        return w.linia * 2 + int(w.typ);
    }
};

struct Wąż {
    Wężolina wężolina;
    int początek, koniec; // wskazuje na końce (nie segmenty)
    
    bool operator<(const Wąż& w) const {
        return tuple(int(wężolina.typ), wężolina.linia, początek, koniec) < tuple(int(w.wężolina.typ), w.wężolina.linia, w.początek, w.koniec);
    };
};

void test() {
    const int N = $;
    const int M = $;
    const int K = $;
    const int Q = $;
    
    unordered_set<pair<int, int>, H> plansza;
    unordered_map<Wężolina, set<int>, H> końce;
    
    int zabieralne = 0;
    
    auto ZabieralnePole = [&](int x, int y) {
        // Zał.: jest tu klocek i to nie segment węża!
        
        const bool ul = plansza.contains({x-1, y-1});
        const bool u = plansza.contains({x, y-1});
        const bool ur = plansza.contains({x+1, y-1});
        const bool l = plansza.contains({x-1, y});
        const bool r = plansza.contains({x+1, y});
        const bool dl = plansza.contains({x-1, y+1});
        const bool d = plansza.contains({x, y+1});
        const bool dr = plansza.contains({x+1, y+1});
        
        if (
            (ul && u && l) || (dl && d && l) || (ur && u && r) || (dr && d && r)
            ) return false;
            return true;
    };
    
    auto JakaWężolinaSegmentu = [&](int x, int y) -> optional<tuple<Wężolina, int>> {
        assert(plansza.contains({x, y}));
        // notacja kierunków graficzna
        bool up = plansza.contains({x, y-1});
        bool down = plansza.contains({x, y+1});
        bool left = plansza.contains({x-1, y});
        bool right = plansza.contains({x+1, y});
        
        bool ul = plansza.contains({x-1, y-1});
        bool ur = plansza.contains({x+1, y-1});
        bool dl = plansza.contains({x-1, y+1});
        bool dr = plansza.contains({x+1, y+1});
        
        using T = tuple<Wężolina, int>;
        
        if (left && up && !right && !down && !ul) return T{{.typ = Wężotyp::Górujący, .linia = x+y-1}, x-y};
        if (!left && !up && right && down && !dr) return T{{.typ = Wężotyp::Górujący, .linia = x+y}, x-y};
        if (left && down && !right && !up && !dl) return T{{.typ = Wężotyp::Opadający, .linia = x-y}, x+y};
        if (!left && !down && right && up && !ur) return T{{.typ = Wężotyp::Opadający, .linia = x-y+1}, x+y};
        return {};
    };
    
    auto ZdekodujKoniec = [&](const Wężolina , int pozycja) -> tuple<int, int> {
        int x, y;
        const int sumka = .linia + pozycja;
        if (.typ == Wężotyp::Górujący) {
            if (abs(sumka) % 2 == 0) {
                // opcja 2:
                x = sumka / 2;
                y = .linia - x;
            } else {
                // opcja 1:
                x = (sumka + 1) / 2;
                y = .linia + 1 - x;                
            }
        } else {
            if (abs(sumka) % 2 == 0) {
                // opcja 3:
                x = sumka / 2;
                y = pozycja - x;
            } else {
                // opcja 4:
                x = (sumka - 1) / 2;
                y = pozycja - x;
            }
        }
        return {x, y};
    };
    
    auto ZidentyfikujWąże = [&](int x, int y) -> set<Wąż> {
        // Segment albo koniec albo koniec dwóch węży!
        
        // Jam węża segment
        const auto& wp = JakaWężolinaSegmentu(x, y);
        if (wp) {
            const Wężolina w = get<0>(*wp);
            const int pozycja = get<1>(*wp);            
            const auto& końcew = końce[w];
            
            auto it = końcew.lower_bound(pozycja);
            //cerr << "jam węża segment (" << x << ", " << y << ")" << endl;
            //cerr << "moja pozycja: " << pozycja << endl;
            //cerr << "istnieją: " << endl;
            //for (const auto& x : końcew) 
            //    cerr << x << ", ";
            //cerr << endl;
            assert (it != końcew.end());
            assert(*it > pozycja);
            assert (it != końcew.begin());
            auto pit = prev(it);
            
            return {Wąż{.wężolina = w, .początek = *pit, .koniec = *it}};
        }
        
        // Zał: nie węża segment.        
        // Może jestem końcem chociaż?
        set<Wąż> wąże;
        
        auto ZbadajSąsiada = [&](int sx, int sy, bool początek_górującego, bool początek_opadającego) {        
            if (!plansza.contains({sx, sy})) return;
            const auto& wp = JakaWężolinaSegmentu(sx, sy);
            if (!wp) return;
                     
            const Wężolina w = get<0>(*wp);
            const int pozycja = get<1>(*wp);
            const auto& końcew = końce[w];
            if ((w.typ == Wężotyp::Górujący && początek_górującego) || (w.typ == Wężotyp::Opadający && początek_opadającego)) {
                // ja jestem początkiem
                auto it = końcew.find(pozycja - 1);
                assert (it != końcew.end());
                auto para = next(it);
                wąże.insert(Wąż{.wężolina = w, .początek = pozycja - 1, .koniec = *para});
            } else {
                // ja jestem końcem
                auto it = końcew.find(pozycja + 1);
                assert (it != końcew.end());                
                auto para = prev(it);
                wąże.insert(Wąż{.wężolina = w, .początek = *para, .koniec = pozycja + 1});
            }
        };
        
        ZbadajSąsiada(x, y-1, true, false);
        ZbadajSąsiada(x, y+1, false, true);
        ZbadajSąsiada(x-1, y, false, false);
        ZbadajSąsiada(x+1, y, true, true);
        
        return wąże;
    };
    
    auto KońceTakNaSzybko = [&](int x, int y) -> set<Wąż> { // bez korzystania z tablicy końców
        if (JakaWężolinaSegmentu(x, y)) return {};        
        
        set<Wąż> wąże;
        
        auto ZbadajSąsiadaSzybko = [&](int sx, int sy, bool początek_górującego, bool początek_opadającego) {   
            if (!plansza.contains({sx, sy})) return;       
            const auto& wp = JakaWężolinaSegmentu(sx, sy);
            if (!wp) return;
            
            //cerr << "KTNS " << x << "," << y << "  sąsiada: " << sx << "," << sy << endl;
                     
            const Wężolina w = get<0>(*wp);
            const int pozycja = get<1>(*wp);
            if ((w.typ == Wężotyp::Górujący && początek_górującego) || (w.typ == Wężotyp::Opadający && początek_opadającego)) {
                // ja jestem początkiem
                wąże.insert(Wąż{.wężolina = w, .początek = pozycja - 1, .koniec = pozycja - 1});
            } else {
                // ja jestem końcem
                wąże.insert(Wąż{.wężolina = w, .początek = pozycja + 1, .koniec = pozycja + 1});
            }
        };
        
        ZbadajSąsiadaSzybko(x, y-1, true, false);
        ZbadajSąsiadaSzybko(x, y+1, false, true);
        ZbadajSąsiadaSzybko(x-1, y, false, false);
        ZbadajSąsiadaSzybko(x+1, y, true, true);
        
        return wąże;
    };
    
    auto Ruch = [&](int x, int y) {
        auto Saldo = [&] {
            set<Wąż> wąże;
            int s = 0;
            for (int xx : {x-1, x, x+1}) {
                for (int yy: {y-1, y, y+1}) {                    
                    if (!plansza.contains({xx, yy})) continue;
                    
                    const auto& podwąże = ZidentyfikujWąże(xx, yy);
                    if (!podwąże.empty()) {
                        wąże.insert(podwąże.begin(), podwąże.end());
                        
                        // czy jestem tylko końcem?
                        const auto w = JakaWężolinaSegmentu(xx, yy);
                        if (!w) {
                            s += ZabieralnePole(xx, yy); // liczymy tutaj, by nie liczyć przypadkiem podwójnie.
                        }
                    } else {
                        s += ZabieralnePole(xx, yy);
                    }
                        
                }
            }
            
            // Zabieralne wąże:
            for (const Wąż& wąż : wąże) {
                const auto& [px, py] = ZdekodujKoniec(wąż.wężolina, wąż.początek);
                const auto& [kx, ky] = ZdekodujKoniec(wąż.wężolina, wąż.koniec);
                const bool z1 = ZabieralnePole(px, py);
                const bool z2 = ZabieralnePole(kx, ky);
                if (z1 || z2) {
                    s += wąż.koniec - wąż.początek - 1; // tu bez końców
                }
            }
            return s;
        };
        
        
        int saldo0 = Saldo();
        
        // Usuńmy stare wężokońce:    
        //cerr << "usuwanie wężykońców:" << endl;        
        for (int xx = x-2; xx <= x+2; ++xx) {
            for (int yy = y-2; yy <= y+2; ++yy) { 
                if (!plansza.contains({xx, yy})) continue;
                const set<Wąż>& sw = KońceTakNaSzybko(xx, yy);
                for (const Wąż& w : sw) {
                    //cerr << " " << xx << "," << yy << " ... poz" << w.początek << endl;
                    auto it = końce[w.wężolina].find(w.początek);
                    assert(it != końce[w.wężolina].end());
                    końce[w.wężolina].erase(it);
                    //końce[w.wężolina].erase(w.początek);
                } 
            }
        }
        //cerr << "---" << endl;
        
        if (plansza.contains({x, y})) {
            plansza.erase({x, y});
        } else {
            plansza.insert({x, y});
        }
        
        // Dodajmy nowe wężokońce:  
        //cerr << "dodawanie wężykońców:" << endl;         
        for (int xx = x-2; xx <= x+2; ++xx) {
            for (int yy = y-2; yy <= y+2; ++yy) { 
                if (!plansza.contains({xx, yy})) continue;
                const set<Wąż>& sw = KońceTakNaSzybko(xx, yy);
                for (const Wąż& w : sw) {
                    //cerr << " " << xx << "," << yy << " ... poz" << w.początek << endl;
                    końce[w.wężolina].insert(w.początek);
                } 
            }
        }
        //cerr << "---" << endl;
        
        int saldo1 = Saldo();
        
        zabieralne += saldo1 - saldo0;
    };
    
    for (int i = 0; i < K; ++i) {
        const int x = $;
        const int y = $;
        Ruch(x, y);
    }
    cout << zabieralne << endl;
    for (int i = 0; i < Q; ++i) {
        //cerr << "Ruch " << (i+1) << ": " << endl;
        const int x = $;
        const int y = $;
        //cerr << "(x = " << x << ", y = " << y << ")" << endl;
        Ruch(x, y);
        
        /*int minx = 1;
        int maxx = -999999;
        int miny=1;
        int maxy = -999999;
        for (const auto& p : plansza) {
            minx = min(minx, p.first);
            miny = min(miny, p.second);
            maxx = max(maxx, p.first);
            maxy = max(maxy, p.second);
        }
        for (int y = miny; y <= maxy; ++y) {
            for (int x = minx; x <= maxx; ++x) {
                if (plansza.contains({x, y}))
                    cerr << '#';
                else
                    cerr << ' ';
            }
            cerr << endl;
        }*/
        
        cout << zabieralne << endl;        
    }
}


[[maybe_unused]] void jeden_test() { test(); }
[[maybe_unused]] void wiele_test() { int T = $; while (T--) test(); }

int main() {
    jeden_test();
    return 0;
}