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
#include<bits/stdc++.h>
#define st first
#define nd second
#define all(x) x.begin(), x.end()
#define BOOST cin.tie(NULL); ios_base::sync_with_stdio(false);
 
// #define int ll
typedef long long ll;

using namespace std;
template <typename T> struct tag:reference_wrapper <T>{ using reference_wrapper <T>::reference_wrapper; };
template <typename T1, typename T2> static inline tag <ostream> operator<<(tag <ostream> os, pair<T1, T2> const& p){ return os.get()<<"{"<<p.first<<", "<<p.second<<"}", os;}
template <typename Other> static inline tag <ostream> operator<<(tag <ostream> os, Other const& o){ os.get()<<o; return os; }
template <typename T> static inline tag <ostream> operator <<(tag <ostream> os, vector <T> const& v){ os.get()<<"["; for (int i=0; i<v.size(); i++) if (i!=v.size()-1) os.get()<<v[i]<<", "; else os.get()<<v[i]; return os.get()<<"]", os; }
template <typename T> static inline tag <ostream> operator <<(tag <ostream> os, set <T> const& s){ vector <T> v; for (auto i: s) v.push_back(i); os.get()<<"["; for (int i=0; i<v.size(); i++) if (i!=v.size()-1) os.get()<<v[i]<<", "; else os.get()<<v[i]; return os.get()<<"]", os; }

int n,m;
map<pair<int, int>, int> blk;           // contains only present blocks, number of neighbours


int dx4[] = {-1, 0, 1, 0};
int dy4[] = {0, 1, 0, -1};

int pot[] = {1, 2, 4, 8};
int revpot[] = {4, 8, 1, 2};
int good[20];

int blkex(pair<int,int> a){
    if(blk.find(a) == blk.end()) return 0;
    return 1;
}

void blkprint(){
    cout << "BLK\n";
    for(auto i : blk){
        cout << "{ " << i.st.st << ", " << i.st.nd << "} = " << i.nd << "\n";
    }
    cout << "\n";
}

int neighbours(pair<int,int> pos){
    int ans = 0;
    for(int j = 0; j < 4; j++){
        if(blkex({pos.st + dx4[j], pos.nd + dy4[j]})){
            ans |= pot[j];
        }
    }
    return ans;
}

void find_out(){
    map<pair<int,int>, int> cpb = blk;
    queue<pair<int,int>> to_rem;
    int ans = 0;
    for(auto i : cpb){
        if(good[i.nd]){
            to_rem.push(i.st);
        }
    }

    pair<int,int> pos;
    pair<int,int> npos;
    while(!to_rem.empty()){
        pos = to_rem.front();
        to_rem.pop();
        ans++;
        for(int i = 0; i < 4; i++){
            npos = {pos.st + dx4[i], pos.nd + dy4[i]};
            if(blkex(npos)){
                if(!good[cpb[npos]]){
                    cpb[npos] ^= revpot[i];
                    if(good[cpb[npos]]){
                        to_rem.push(npos);
                    }
                }
            }
        }
    }
    cout << ans << "\n";
}

int32_t main(){
    BOOST;

    good[0] = 1;
    good[1] = 1;
    good[2] = 1;
    good[4] = 1;
    good[8] = 1;
    good[5] = 1;
    good[10] = 1;

    int k, q;
    int x, y;
    
    cin >> n >> m >> k >> q;

    for(int i = 0; i < k; i++){
        cin >> x >> y;
        blk[{x,y}] = 0;
    }
    pair<int,int> tmpp;
    for(auto i : blk){
        tmpp = i.st;
        blk[tmpp] = neighbours(tmpp);
    }

    find_out();

    int ctr = 0;
    while(q--){
        ctr++;
        cin >> x >> y;
        if(blkex({x, y})){
            blk.erase({x,y});
            for(int i = 0; i < 4; i++){
                if(blkex({x + dx4[i], y + dy4[i]})){
                    blk[{x + dx4[i], y + dy4[i]}] = neighbours({x + dx4[i], y + dy4[i]});
                }
            }
        } else {
            blk[{x, y}] = neighbours({x, y});
            for(int i = 0; i < 4; i++){
                if(blkex({x + dx4[i], y + dy4[i]})){
                    blk[{x + dx4[i], y + dy4[i]}] = neighbours({x + dx4[i], y + dy4[i]});
                }
            }
        }
        find_out();
    }

    return 0;
}

/*
BLK
{ 1, 3} = 1
{ 1, 4} = 1
{ 1, 5} = 0
{ 2, 2} = 0
{ 2, 3} = 1
{ 2, 4} = 1
{ 3, 1} = 1
{ 3, 2} = 1
{ 4, 1} = 1
{ 4, 2} = 1
*/