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

using namespace std;

#define ll long long

#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;

typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;

typedef pair<int,int> ii;

template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;

const int MOD = 998244353;

ll read(){
    ll i;
    cin>>i;
    return i;
}

vi readvi(int n,int off=0,int shift=0){
    vi v(n+shift);
    rep(i,shift)v[i]=0;
    rep(i,n)v[i+shift]=read()+off;
    return v;
}

void YesNo(bool condition, bool do_exit=true) {
    if (condition)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
    if (do_exit)
        exit(0);
}

int n,m,k,q;

inline int get_square (int x, int y, set<ii> & s) {
    if (x < 1 or y < 1 or x > n or y > m)
        return 0;
    else
        return s.count({x,y});
}

int get_type(int x, int y, set<ii> & s) {
    if (s.count({x,y}) == 0)
        return -1;
    if (s.count({x-1, y}) + s.count({x+1,y}) == 0)
        return 0;
    if (s.count({x, y-1}) + s.count({x,y+1}) == 0)
        return 0;

    // >= 1 pionowo i >= 1 poziomo sąsiednie zajęte

    for (int corner_x = x-1; corner_x <= x; corner_x++)
        for (int corner_y = y-1; corner_y <= y; corner_y++)
            if (get_square(corner_x, corner_y, s) + get_square(corner_x+1, corner_y, s) + get_square(corner_x, corner_y+1, s) + get_square(corner_x+1, corner_y+1, s) == 4)
                return 2;

    return 1;
}

inline int num_neighbors_type (int x, int y, int type, set<ii> & s) {
     return (get_type(x-1,y, s) == type) + (get_type(x+1,y,s) == type) + (get_type(x,y-1,s) == type) + (get_type(x,y+1,s) == type);
}

ii directions[] = {{-1, 0},
                   {1,  0},
                   {0,  -1},
                   {0,  1}};


int compute (set<ii> & s) {
    int res = 0;

    for (auto [x,y] : s)
        if (get_type(x, y, s) == 0)
            ++res;

    map<ii,ii> left_end, right_end, prev, next;
    map<ii,bool> blocked;

    for (auto [x, y] : s) {
        if (get_type(x, y, s) == 1 and left_end.count({x,y})==0 and num_neighbors_type(x,y,1,s) <= 1) {
            //endpoint of a diagonal
            left_end[{x,y}] = {x,y};
            prev[{x,y}] = {x,y};

            int len = 1;
            int xx = x;
            int yy = y;
            bool endpoint = false;
            while (not endpoint) {
                endpoint = true;
                for (ii dir: directions)
                    if ((get_type(xx + dir.first, yy + dir.second, s) == 1) and
                        (left_end.count({xx + dir.first, yy + dir.second}) == 0)) {
                        next[{xx,yy}] = {xx + dir.first, yy + dir.second};
                        prev[{xx + dir.first, yy + dir.second}] = {xx,yy};
                        xx = xx + dir.first;
                        yy = yy + dir.second;
                        left_end[{xx,yy}] = {x,y};
                        endpoint = false;
                        ++len;
                    }
            }
            right_end[{x,y}] = {xx,yy};
            next[{xx,yy}] = {xx,yy};
            blocked[{x,y}] = num_neighbors_type(x,y,0,s) + num_neighbors_type(xx,yy,0,s) == 0;
            if (not blocked[{x,y}])
                res += len;

        }
    }
    return res;
}

int main(void ) {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

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

    set<ii> s;

    rep(i,k) {
        int x, y;
        cin >> x >> y;
        s.insert({x,y});
    }


    cout << compute(s) << endl;

    rep(_,q) {
        int x,y;
        cin >> x >> y;
        if (s.count({x,y})) {
            // usun
            s.erase({x,y});
        }
        else {
            // dodaj
            s.insert({x,y});
        }
        cout << compute(s) << endl;
    }

    return 0;
}