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
#include <iostream>
#include <map>
#include <queue>
#include <vector>

constexpr int LEFT = 0;
constexpr int RIGHT = 1;
constexpr int UP = 2;
constexpr int DOWN = 3;

using namespace std;
using PII = pair<int, int>;

struct Block {
    int x, y;
    bool removed, deleted;
    vector<int> nei;
    vector<bool> blocked;
};

const vector<int> dx{ -1, 1, 0, 0 };
const vector<int> dy{ 0, 0, -1, 1 };

map<int, map<int, int>> board;
vector<Block> blocks;
queue<int> removed;
int score;

int get_block(int x, int y)
{
    auto it = board.find(x);

    if (it != board.end())
    {
        auto it2 = it->second.find(y);

        if (it2 != it->second.end())
        {
            return it2->second;
        }
    }

    return -1;
}

void add_block(int x, int y)
{
    int ind = blocks.size();
    Block blk = { x, y, false, false, vector<int>(4, -1), vector<bool>(4) };

    blocks.push_back(blk);

    auto it = board.find(x);

    if (it != board.end())
        it->second[y] = ind;
    else
        board[x] = map<int, int>{ { y, ind } };

    for (int d = 0; d < 4; ++d)
    {
        int ind2 = get_block(x + dx[d], y + dy[d]);

        if (ind2 != -1)
        {
            blocks[ind].nei[d] = ind2;
            blocks[ind2].nei[d ^ 1] = ind;

            if (!blocks[ind2].deleted)
            {
                blocks[ind].blocked[d] = true;
                blocks[ind2].blocked[d ^ 1] = true;
            }
        }
    }
}

void readd_block(int ind)
{
    blocks[ind].removed = false;
    blocks[ind].deleted = false;

    for (int d = 0; d < 4; ++d)
    {
        int ind2 = blocks[ind].nei[d];

        if (ind2 != -1)
        {
            if (!blocks[ind2].deleted)
            {
                blocks[ind].blocked[d] = true;
                blocks[ind2].blocked[d ^ 1] = true;
            }
        }
    }
}

void remove_block(int ind)
{
    for (int d = 0; d < 4; ++d)
    {
        int ind2 = blocks[ind].nei[d];

        if (ind2 != -1 && !blocks[ind2].deleted)
        {
            blocks[ind].blocked[d] = false;
            blocks[ind2].blocked[d ^ 1] = false;

            if (!blocks[ind2].removed && !blocks[ind2].blocked[d])
            {
                blocks[ind2].removed = true;
                removed.push(ind2);
            }
        }
    }
}

void delete_block(int ind)
{
    if (blocks[ind].removed)
        --score;

    blocks[ind].deleted = true;

    remove_block(ind);
}

void reset_blocks()
{
    for (size_t i = 0; i < blocks.size(); ++i)
    {
        if (!blocks[i].deleted && blocks[i].removed)
        {
            blocks[i].removed = false;

            for (int d = 0; d < 4; ++d)
            {
                int ind2 = blocks[i].nei[d];

                if (ind2 != -1 && !blocks[ind2].deleted)
                {
                    blocks[i].blocked[d] = true;
                    blocks[ind2].blocked[d ^ 1] = true;
                }
            }
        }
    }
}

void check_removable_blocks()
{
    for (size_t i = 0; i < blocks.size(); ++i)
    {
        if (!blocks[i].deleted && !blocks[i].removed)
        {
            if ((!blocks[i].blocked[LEFT] && !blocks[i].blocked[RIGHT]) || (!blocks[i].blocked[UP] && !blocks[i].blocked[DOWN]))
            {
                blocks[i].removed = true;
                removed.push(i);
            }
        }
    }
}

void solve()
{
    while (!removed.empty())
    {
        int ind = removed.front();
        removed.pop();

        ++score;

        remove_block(ind);
    }
}

int main()
{
    int n, m, k, q, x, y;
    cin >> n >> m >> k >> q;

    for (int i = 0; i < k; ++i)
    {
        cin >> x >> y;

        add_block(x, y);
    }

    score = 0;

    check_removable_blocks();
    solve();

    cout << score << endl;

    for (int i = 0; i < q; ++i)
    {
        cin >> x >> y;

        int ind = get_block(x, y);

        if (ind != -1 && !blocks[ind].deleted)
        {
            delete_block(ind);
            solve();
        }
        else
        {
            reset_blocks();

            if (ind == -1)
                add_block(x, y);
            else
                readd_block(ind);

            score = 0;
                
            check_removable_blocks();
            solve();
        }

        cout << score << endl;
    }

    return 0;
}