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

using namespace std;

const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};

struct Point {
    int x, y;
    bool operator==(const Point &other) const {
        return x == other.x && y == other.y;
    }
};

struct PointHash {
    inline size_t operator()(const Point &c) const {
        size_t h1 = std::hash<int>()(c.x);
        size_t h2 = std::hash<int>()(c.y);
        return h1 ^ (h2 + 0x9e3774a7c15ULL + (h1 << 4) + (h1 >> 3));
    }
};


int n, m, k, q;

bool valid(int x, int y) {
    return (x >= 1 && x <= n && y >= 1 && y <= m);
}

vector<Point> ngh(const Point &c) {
    vector<Point> res;
    for (int i = 0; i < 4; i++) {
        int xx = c.x + dx[i], yy = c.y + dy[i];
        if (valid(xx, yy)) res.push_back({xx, yy});
    }

    return res;
}

bool rmv(const Point &b, const unordered_set<Point, PointHash> &X) {
    int x = b.x, y = b.y;
    bool hor = ((y == 1 || X.find({x, y - 1}) == X.end()) && (y == m || X.find({x, y + 1}) == X.end()));
    bool ver = ((x == 1 || X.find({x - 1, y}) == X.end()) && (x == n || X.find({x + 1, y}) == X.end()));
    return hor || ver;
}

unordered_set<Point, PointHash> find_unrem(const unordered_set<Point, PointHash> grph) {
    unordered_set<Point, PointHash> res = grph;
    deque<Point> dq;

    for (const auto &b : grph) {
        if (rmv(b, grph)) dq.push_back(b);
    }

    while (!dq.empty()) {
        Point b = dq.front();
        dq.pop_front();
        if (grph.find(b) == grph.end()) continue;
        if (rmv(b, res)) {
            res.erase(b);
            for (auto nb : ngh(b)) {
                if (res.find(nb) != res.end()) dq.push_back(nb);
            }
        }
    }

    return res;
}

unordered_set<Point, PointHash> find_all_unrem(const unordered_set<Point, PointHash> &board) {
    unordered_set<Point, PointHash> tmp = board;
    deque<Point> dq;

    for (auto b : tmp) {
        if (rmv(b, tmp)) dq.push_back(b);
    }

    while (!dq.empty()) {
        Point b = dq.front();
        dq.pop_front();

        if (tmp.find(b) == tmp.end()) continue;
        if (rmv(b, tmp)) {
            tmp.erase(b);
            for (auto nb : ngh(b)) {
                if (tmp.find(nb) != tmp.end()) dq.push_back(nb);
            }
        }
    }

    return tmp;
}

void rem(Point &b, unordered_set<Point, PointHash> &board, unordered_set<Point, PointHash> &grph) {
    deque<Point> dq;
    for (const auto &nb : ngh(b)) {
        if (board.find(nb) != board.end() && grph.find(nb) != grph.end()) dq.push_back(nb);
    }

    while (!dq.empty()) {
        Point c = dq.front();
        dq.pop_front();
        if (grph.find(c) != grph.end() && rmv(c, grph)) {
            grph.erase(c);
            for (const auto &nb : ngh(c)) {
                if (board.find(nb) != board.end() && grph.find(nb) != grph.end()) dq.push_back(nb);
            }
        }
    }
}

void add(Point &b, unordered_set<Point, PointHash> &board, unordered_set<Point, PointHash> &grph) {
    unordered_set<Point, PointHash> res;
    deque<Point> dq;
    dq.push_back(b);
    res.insert(b);

    while (!dq.empty()) {
        Point cur = dq.front();
        dq.pop_front();
        for (const auto &nb : ngh(cur)) {
            if (board.find(nb) != board.end() && res.find(nb) == res.end()) {
                res.insert(nb);
                dq.push_back(nb);
            }
        }
    }

    unordered_set<Point, PointHash> new_comp = find_unrem(res);
    vector<Point> torm;
    for (const auto &b_old : res) {
        if (res.find(b_old) != res.end()) torm.push_back(b_old);
    }

    for (const auto &x : torm) grph.erase(x);
    for (const auto &b_new : new_comp) grph.insert(b_new);
}

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

    cin>>n>>m>>k>>q;
    unordered_set<Point, PointHash> board;
    for (int i = 0; i < k; i++) {
        int x, y; cin>>x>>y;
        board.insert({x, y});
    }

    unordered_set<Point, PointHash> unrm = find_all_unrem(board);
    cout<<board.size()-unrm.size()<<"\n";
    
    for (int i = 0; i < q; i++){
        int x, y;
        cin>>x>>y;

        Point Point = {x, y};
        if (board.find(Point) != board.end()) {
            board.erase(Point);
            if (unrm.find(Point) != unrm.end()) unrm.erase(Point);
            rem(Point, board, unrm);
        } else {
            board.insert(Point);
            add(Point, board, unrm);
        }
        cout<<board.size()-unrm.size()<<"\n";
    }
    
    return 0;
}