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

#define rep(i, b, e) for(int i = (b); i <= (e); i++)
#define per(i, b, e) for(int i = (e); i >= (b); i--)
#define FOR(i, b, e) rep(i, b, (e) - 1)
#define SZ(x) int(x.size())
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define st first
#define nd second
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;

auto &operator<<(auto &o, pair<auto, auto> p) {
    return o << "(" << p.st << ", " << p.nd << ")"; }
auto operator<<(auto &o, auto x)->decltype(end(x), o) {
    o << "{"; int i=0; for(auto e: x) o << ", " + 2*!i++ << e;
    return o << "}"; }
#ifdef LOCAL
#define deb(x...) cerr << "[" #x "]: ", [](auto...$) { \
    ((cerr << $ << "; "),...) << endl; }(x)
#else
#define deb(...)
#endif

#include <ext/pb_ds/assoc_container.hpp> /** keep-include */
using namespace __gnu_pbds;
// for places where hacking might be a problem:
const int R = chrono::high_resolution_clock::now().time_since_epoch().count();
struct chash { // to use most bits rather than just the lowest
	const uint64_t C = ll(4e18 * acos(0)) | 71;
	ll operator()(ll x) const { 
		return __builtin_bswap64((x^R)*C); }
};
// 1 << 16 is initial size, don't put it if you want small map
gp_hash_table<ll, int, chash> remap({},{},{},{}, {1 << 18});

vector<pii> klocki;

int getId(pii x) {
    auto it = remap.find(ll(x.st) << 30 ^ x.nd);
    return it == remap.end() ? -1 : it->nd;
}

pii operator+(const pii &a, const pii &b) {
    return {a.st + b.st, a.nd + b.nd};
}

pii operator-(const pii &a, const pii &b) {
    return {a.st - b.st, a.nd - b.nd};
}

struct UF {
    vi e, status, sz, taken;
    int curAns;
    vector<pair<int*, int>> data;
    
    int size(int x) { return -e[find(x)]; }
    int find(int x) { return e[x] < 0 ? x : find(e[x]); }
    int time() { return SZ(data); }
    void rollback(int t) {
        for(int i = time(); i --> t;) *data[i].st = data[i].nd;
        data.resize(t);
    }
    
    bool join(int a, int b) {
        a = find(a), b = find(b);
        if(a == b) return 0;
        if(e[a] > e[b]) swap(a, b);
        data.pb({&e[a], e[a]}), e[a] += e[b];
        data.pb({&e[b], e[b]}), e[b] = a;
        if(taken[a] + taken[b] == 1) {
            data.pb({&curAns, curAns});
            curAns += taken[a] * sz[b] + taken[b] * sz[a];
        }
        data.pb({&taken[a], taken[a]}), taken[a] |= taken[b];
        data.pb({&sz[a], sz[a]}), sz[a] += sz[b];
        return 1;
    }

    void flagTaken(int a) {
        a = find(a);
        if(taken[a]) return;
        data.pb({&curAns, curAns}), curAns += sz[a];
        data.pb({&taken[a], taken[a]}), taken[a] = 1;
    }

    void setStatus(int a, int s) {
        data.pb({&status[a], status[a]}), status[a] = s;
    }

    void hide(int a) {
        setStatus(a, 0);
        a = find(a);
        if(taken[a]) data.pb({&curAns, curAns}), curAns--;
        data.pb({&sz[a], sz[a]}), sz[a]--;
    }

    bool isSquared(pii x) {
        pii d[] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
        FOR(start, 0, 4) {
            int sum = 0;
            FOR(shift, 0, 4) {
                int pole = getId(x - d[start] + d[shift]);
                if(pole != -1) sum += status[pole] > 0;
            }
            if(sum == 4) return 1;
        }
        return 0;
    }

    void updateSoms(pii x) {
        pii d[] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
        int mask = 15;
        FOR(k, 0, 4) {
            int pole = getId(x + d[k]);
            if(pole == -1 || status[pole] == 0 || taken[find(pole)]) mask ^= 1 << k; 
        }
        FOR(k, 0, 4) if((mask & (k < 2 ? 3 : 12)) == 1 << k) {
            int pole = getId(x + d[k]);
            if(status[pole] == 2) join(getId(x), pole);
        }
        if((mask & 3) == 0 || (mask & 12) == 0) flagTaken(getId(x));
    }

    void init() {
        int n = SZ(klocki);
        e = vi(n, -1);
        status = vi(n, 2);
        sz = vi(n, 1);
        taken = vi(n, 0);
        curAns = 0;
        FOR(i, 0, n) if(isSquared(klocki[i])) setStatus(i, 1);
        FOR(i, 0, n) if(status[i] == 2) updateSoms(klocki[i]);
    }

    int ans() {
        return curAns;
    }

    void godlike(int a) {
        pii x = klocki[a];
        hide(a);
        pii d[] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
        auto somsiaduj = [&](pii xd) {
            FOR(k, 0, 8) {
                pii y = xd + d[k];
                int soms = getId(y);
                if(soms != -1 && status[soms] == 2) updateSoms(y);
            }
        };
        FOR(k, 0, 8) {
            pii y = x + d[k];
            int soms = getId(y);
            if(soms != -1 && status[soms] == 1 && !isSquared(y)) {
                setStatus(soms, 2), somsiaduj(y);
            }
        }
        somsiaduj(x);
    }
} jan;

struct Tree {
    int T = 1;
    vector<vi> kill;
    vi res;
    
    Tree(int N) {
        while(T < N) T *= 2;
        kill.resize(T * 2);
    }

    void add(int l, int r, int id) {
        for(l += T, r += T; l <= r; l >>= 1, r >>= 1) {
            if(l & 1) kill[l++].pb(id);
            if((r & 1) ^ 1) kill[r--].pb(id);
        }
    }

    void get(int v, int l, int r) {
        int t = jan.time();
        for(auto &x: kill[v]) jan.godlike(x);
        if(v >= T) {
            res.pb(jan.ans());
            jan.rollback(t);
            return;
        }
        int mid = (l + r) / 2;
        get(v * 2, l, mid), get(v * 2 + 1, mid + 1, r);
        jan.rollback(t);
    }

    vi getAns() {
        get(1, 0, T - 1);
        return res;
    }
};

void solve() {
    int n, m, k, q;
    cin >> n >> m >> k >> q;
    map<pii, int> czas;
    map<pii, vector<pii>> ranges; 
    FOR(i, 0, k) {
        pii p; cin >> p.st >> p.nd;
        czas[p] = 0;
    }
    FOR(i, 0, q) {
        pii p;
        cin >> p.st >> p.nd;
        if(czas.count(p)) ranges[p].pb({czas[p], i}), czas.erase(p);
        else czas[p] = i + 1;
    }
    for(auto &[p, t]: czas) ranges[p].pb({t, q});
    for(auto &[p, _]: ranges) {
        remap[ll(p.st) << 30 ^ p.nd] = SZ(klocki);
        klocki.pb(p);
    }
    auto invert = [&q](vector<pii> vec) {
        vector<pii> ret;
        if(vec[0].st != 0) ret.pb({0, vec[0].st - 1});
        FOR(i, 1, SZ(vec)) ret.pb({vec[i-1].nd + 1, vec[i].st - 1});
        if(end(vec)[-1].nd != q) ret.pb({end(vec)[-1].nd + 1, q});
        return ret;
    };
    Tree seg(q + 1);
    for(auto &[p, ran]: ranges) {
        auto nowe = invert(ran);
        for(auto &[l, r]: nowe) seg.add(l, r, getId(p));
    }
    jan.init();
    auto res = seg.getAns();
    res.resize(q + 1);
    for(auto &x: res) cout << x << '\n';
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int tt = 1;
    // cin >> tt;
    FOR(te, 0, tt) solve();
    return 0;
}