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

int calculateAllCollectable(const std::set<std::pair<int, int>>& board, 
                           std::map<std::pair<int, int>, bool>& isValuable) {
    int totalPoints = 0;
    
    for (const auto& pos : board) {
        int x = pos.first;
        int y = pos.second;
        
        bool isWorthless = false;
        
        if (board.find({x-1, y}) != board.end() && 
            board.find({x, y-1}) != board.end() && 
            board.find({x-1, y-1}) != board.end()) {
            isWorthless = true;
        }
        
        if (!isWorthless && 
            board.find({x-1, y}) != board.end() && 
            board.find({x, y+1}) != board.end() && 
            board.find({x-1, y+1}) != board.end()) {
            isWorthless = true;
        }
        
        if (!isWorthless && 
            board.find({x+1, y}) != board.end() && 
            board.find({x, y-1}) != board.end() && 
            board.find({x+1, y-1}) != board.end()) {
            isWorthless = true;
        }
        
        if (!isWorthless && 
            board.find({x+1, y}) != board.end() && 
            board.find({x, y+1}) != board.end() && 
            board.find({x+1, y+1}) != board.end()) {
            isWorthless = true;
        }
        
        if (isWorthless) {
            isValuable[{x, y}] = false;
        } else {
            isValuable[{x, y}] = true;
            totalPoints++;
        }
    }
    
    return totalPoints;
}


int updateCollectable(std::set<std::pair<int, int>>& board, 
                     std::map<std::pair<int, int>, bool>& isValuable,
                     int x, int y, int currentPoints) {
    
    std::set<std::pair<int, int>> toCheck;
    
    for (int dx = -1; dx <= 1; dx++) {
        for (int dy = -1; dy <= 1; dy++) {
            std::pair<int, int> neighbor = {x + dx, y + dy};
            if (board.find(neighbor) != board.end()) {
                toCheck.insert(neighbor);
            }
        }
    }
    
    for (const auto& pos : toCheck) {
        int px = pos.first;
        int py = pos.second;
        
        bool wasValuable = isValuable[{px, py}];
        
        bool isWorthless = false;
        
        if (board.find({px-1, py}) != board.end() && 
            board.find({px, py-1}) != board.end() && 
            board.find({px-1, py-1}) != board.end()) {
            isWorthless = true;
        }
        
        if (!isWorthless && 
            board.find({px-1, py}) != board.end() && 
            board.find({px, py+1}) != board.end() && 
            board.find({px-1, py+1}) != board.end()) {
            isWorthless = true;
        }
        
        if (!isWorthless && 
            board.find({px+1, py}) != board.end() && 
            board.find({px, py-1}) != board.end() && 
            board.find({px+1, py-1}) != board.end()) {
            isWorthless = true;
        }
        
        if (!isWorthless && 
            board.find({px+1, py}) != board.end() && 
            board.find({px, py+1}) != board.end() && 
            board.find({px+1, py+1}) != board.end()) {
            isWorthless = true;
        }
        
        if (isWorthless) {
            isValuable[{px, py}] = false;
            if (wasValuable) {
                currentPoints--; 
            }
        } else {
            isValuable[{px, py}] = true;
            if (!wasValuable) {
                currentPoints++; 
            }
        }
    }
    
    return currentPoints;
}

int main() {
    int n, m, k, q;
    std::cin >> n >> m >> k >> q;
    
    std::set<std::pair<int, int>> board;
    std::map<std::pair<int, int>, bool> isValuable;
    
    for (int i = 0; i < k; i++) {
        int x, y;
        std::cin >> x >> y;
        board.insert({x, y});
        isValuable[{x, y}] = true;
    }
    
    int currentPoints = calculateAllCollectable(board, isValuable);
    std::cout << currentPoints << std::endl;

    for (int i = 0; i < q; i++) {
        int x, y;
        std::cin >> x >> y;
        
        auto it = board.find({x, y});
        if (it != board.end()) {
            if (isValuable[{x, y}]) {
                currentPoints--;
            }
            board.erase(it);
            isValuable.erase({x, y});
        } else {
            board.insert({x, y});
            isValuable[{x, y}] = true;
            currentPoints++;
        }
        
        currentPoints = updateCollectable(board, isValuable, x, y, currentPoints);    
        std::cout << currentPoints << std::endl;
    }
    
    return 0;
}