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 <cstdlib>
#include <bitset>
#include <functional>
#include <utility>
#include <chrono>
#include <tuple>
#include <new>
#include <memory>
#include <climits>
#include <cfloat>
#include <cinttypes>
#include <exception>
#include <string>
#include <array>
#include <vector>
#include <deque>
#include <list>
#include <forward_list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stack>
#include <queue>
#include <algorithm>
#include <iterator>
#include <cmath>
#include <complex>
#include <valarray>
#include <ios>
#include <istream>
#include <ostream>
#include <iostream>
#include <fstream>
#include <streambuf>
#include <cstdio>
#include <thread>
// #include <bits/stdc++.h>
using namespace std;

int n, m, k, q;
map<pair<int, int>, int> dodane;
set<pair<int, int>> klocki, klocki_akt;
int wyn, a, b, x, y;
int tab[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
queue<pair<int, int>> Q;

bool check(int a, int b)
{
    if (klocki_akt.count({a, b}) == 0)
        return 0;

    if ((klocki_akt.count({a - 1, b}) == 0 && klocki_akt.count({a + 1, b}) == 0) || (klocki_akt.count({a, b - 1}) == 0 && klocki_akt.count({a, b + 1}) == 0))
        return 1;
    else
        return 0;
}
int main()
{
    std::ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> n >> m >> k >> q;

    for (int i = 1; i <= k; i++)
    {
        cin >> a >> b;
        klocki.insert({a, b});
    }
    q++;

    klocki_akt = klocki;
    for (auto para : klocki_akt)
    {
        tie(a, b) = para;
        if (check(a, b))
        {
            Q.push({a, b});
            dodane[{a, b}] = 1;
        }
    }

    while (!Q.empty())
    {
        tie(a, b) = Q.front();
        Q.pop();
        wyn++;
        klocki_akt.erase({a, b});

        for (int i = 0; i <= 3; i++)
        {
            x = a + tab[i][0];
            y = b + tab[i][1];

            if (dodane[{x, y}] == 0 && check(x, y) == 1)
            {
                Q.push({x, y});
                dodane[{x, y}] = 1;
            }
        }
    }
    cout << wyn << "\n";

    for (int zap = 2; zap <= q; zap++)
    {
        cin >> a >> b;
        if (klocki.count({a, b}) == 1)
            klocki.erase({a, b});
        else
            klocki.insert({a, b});

        klocki_akt = klocki;
        wyn = 0;

        for (auto para : klocki_akt)
        {
            tie(a, b) = para;
            if (check(a, b))
            {
                Q.push({a, b});
                dodane[{a, b}] = zap;
            }
        }

        while (!Q.empty())
        {
            tie(a, b) = Q.front();
            Q.pop();
            wyn++;
            klocki_akt.erase({a, b});

            for (int i = 0; i <= 3; i++)
            {
                x = a + tab[i][0];
                y = b + tab[i][1];

                if (dodane[{x, y}] != zap && check(x, y) == 1)
                {
                    Q.push({x, y});
                    dodane[{x, y}] = zap;
                }
            }
        }
        cout << wyn << "\n";
    }
}