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
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double db;
typedef pair<int,int> pii;

struct hash_pii{
    inline size_t operator()(const pii &a) const{
        return a.first*123456+a.second;
    }
};

// typedef unordered_set<pii,hash_pii>  _set;
typedef set<pii>  _set;

constexpr pii off[4] = {{0,1},{1,0},{0,-1},{-1,0}};

void update(const int &x, const int &y, _set &blocks, _set &loose){
    const pii a = make_pair(x,y);
    if(blocks.find(a) == blocks.end()) return;
    bool blocked[4];
    for(int i=0; i<4; ++i){
        const auto &[xo,yo] = off[i];
        blocked[i] = blocks.find(make_pair(x+xo,y+yo)) != blocks.end();
    }
    if((!blocked[0] && !blocked[2]) || (!blocked[1] && !blocked[3])){
        // Jest luźny
        loose.insert(a);
    }else{
        // Nie jest luźny
        loose.erase(a);
    }
}

int brute_calculation(_set blocks, _set loose){
    int res = 0;
    pii p;
    while(!loose.empty()){
        p = *loose.begin();
        // cout << "(" << x << "," << y << ")" << endl;
        loose.erase(loose.begin());
        blocks.erase(p);
        ++res;

        for(const auto &[xo,yo] : off)
            update(p.first+xo, p.second+yo, blocks, loose);
    }
    return res;
}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    int X,Y,k,q;
    pii p;
    _set blocks,loose;
    cin >> X >> Y >> k >> q;
    for(int i=0,x,y; i<k; ++i){
        cin >> x >> y;
        p.first = x;
        p.second = y;
        blocks.insert(p);
    }
    for(const auto &[x,y] : blocks)
        update(x, y, blocks, loose);

    cout << brute_calculation(blocks, loose) << "\n";

    for(int i=0,x,y; i<q; ++i){
        cin >> x >> y;
        p.first = x;
        p.second = y;
        if(blocks.find(p) == blocks.end()){
            blocks.insert(p);
            update(x, y, blocks, loose);
        }else{
            blocks.erase(p);
            loose.erase(p);
        }
        for(const auto &[xo,yo] : off)
            update(x+xo, y+yo, blocks, loose);
        
        cout << brute_calculation(blocks, loose) << "\n";
    }
}