#include <iostream>
#include <map>
#include <utility>
using namespace std;
int n, m, k, q;
map<pair<int,int>, bool> blocks;
bool square(int x, int y, int row, int col) {
bool flag = true;
for(int i : {x, x+row}) {
for(int j : {y, y+col}) {
if(blocks.find( {i, j} ) == blocks.end()) {
flag = false;
}
}
}
return flag;
}
bool ok(int x, int y) {
bool flag = true;
for(int i : {1, -1}) {
for(int j : {1, -1}) {
if(square(x, y, i, j)) {
flag = false;
}
}
}
return flag;
}
int check9(int x, int y) {
int cnt = 0;
for(int row = x-1; row <= x+1; row++) {
for(int col = y-1; col <= y+1; col++) {
if(ok(row, col) && (blocks.find( {row, col} ) != blocks.end())) {
cnt++;
}
}
}
return cnt;
}
int solve() {
int cnt = 0;
for(auto bl : blocks) {
if(ok(bl.first.first, bl.first.second)) {
cnt++;
}
}
return cnt;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> k >> q;
for(int i = 0; i < k; i++) {
int x, y;
cin >> x >> y;
blocks[ {x, y} ] = true;
}
int ans = solve();
cout << ans << "\n";
for(int i = 0; i < q; i++) {
int a, b;
cin >> a >> b;
int last_check = check9(a, b);
if(blocks.find( {a, b} ) == blocks.end()) {
blocks[ {a, b} ] = true;
}
else {
blocks.erase( {a, b} );
}
int new_check = check9(a, b);
int diff = new_check - last_check;
ans += diff;
cout << ans << "\n";
}
return 0;
}
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 | #include <iostream> #include <map> #include <utility> using namespace std; int n, m, k, q; map<pair<int,int>, bool> blocks; bool square(int x, int y, int row, int col) { bool flag = true; for(int i : {x, x+row}) { for(int j : {y, y+col}) { if(blocks.find( {i, j} ) == blocks.end()) { flag = false; } } } return flag; } bool ok(int x, int y) { bool flag = true; for(int i : {1, -1}) { for(int j : {1, -1}) { if(square(x, y, i, j)) { flag = false; } } } return flag; } int check9(int x, int y) { int cnt = 0; for(int row = x-1; row <= x+1; row++) { for(int col = y-1; col <= y+1; col++) { if(ok(row, col) && (blocks.find( {row, col} ) != blocks.end())) { cnt++; } } } return cnt; } int solve() { int cnt = 0; for(auto bl : blocks) { if(ok(bl.first.first, bl.first.second)) { cnt++; } } return cnt; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m >> k >> q; for(int i = 0; i < k; i++) { int x, y; cin >> x >> y; blocks[ {x, y} ] = true; } int ans = solve(); cout << ans << "\n"; for(int i = 0; i < q; i++) { int a, b; cin >> a >> b; int last_check = check9(a, b); if(blocks.find( {a, b} ) == blocks.end()) { blocks[ {a, b} ] = true; } else { blocks.erase( {a, b} ); } int new_check = check9(a, b); int diff = new_check - last_check; ans += diff; cout << ans << "\n"; } return 0; } |
English