#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll N = 2e5, M = 2e5;
int n, m, k, q, a, c, res;
unordered_map<ll, bool> blocks, temp_blocks;
ll get_hash(ll x, ll y) {
return x * (N + 5) + y;
}
pair <int, int> get_pair(ll hash) {
return {hash / (N + 5), hash % (N + 5)};
}
bool b(int x, int y) {
ll hash = get_hash(x, y);
auto it = temp_blocks.find(hash);
if (it != temp_blocks.end()) return it->second;
return false;
}
bool can_remove(int x, int y) {
return b(x, y) && (!b(x-1, y) && !b(x+1, y) || !b(x, y-1) && !b(x, y+1));
}
void calculate_res(){
int changes = 1;
res = 0;
temp_blocks.clear();
for(const auto& block : blocks) {
temp_blocks[block.first] = block.second;
}
while(changes){
changes = 0;
for(const auto& block : temp_blocks) {
pair<int, int> p = get_pair(block.first);
int x = p.first, y = p.second;
if(can_remove(x, y)){
res++;
changes++;
temp_blocks[block.first] = false;
}
}
}
}
int main() {
cin>>n>>m>>k>>q;
for(int i = 1 ; i <= k ; i++) {
cin >> a >> c;
blocks[get_hash(a, c)] = true;
}
int changes = 1;
calculate_res();
cout<<res<<"\n";
for(int i = 1 ; i <= q ; i++) {
cin>>a>>c;
ll hash = get_hash(a, c);
blocks[hash] = !blocks[hash];
calculate_res();
cout<<res<<"\n";
}
}
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 | #include <bits/stdc++.h> #define ll long long using namespace std; const ll N = 2e5, M = 2e5; int n, m, k, q, a, c, res; unordered_map<ll, bool> blocks, temp_blocks; ll get_hash(ll x, ll y) { return x * (N + 5) + y; } pair <int, int> get_pair(ll hash) { return {hash / (N + 5), hash % (N + 5)}; } bool b(int x, int y) { ll hash = get_hash(x, y); auto it = temp_blocks.find(hash); if (it != temp_blocks.end()) return it->second; return false; } bool can_remove(int x, int y) { return b(x, y) && (!b(x-1, y) && !b(x+1, y) || !b(x, y-1) && !b(x, y+1)); } void calculate_res(){ int changes = 1; res = 0; temp_blocks.clear(); for(const auto& block : blocks) { temp_blocks[block.first] = block.second; } while(changes){ changes = 0; for(const auto& block : temp_blocks) { pair<int, int> p = get_pair(block.first); int x = p.first, y = p.second; if(can_remove(x, y)){ res++; changes++; temp_blocks[block.first] = false; } } } } int main() { cin>>n>>m>>k>>q; for(int i = 1 ; i <= k ; i++) { cin >> a >> c; blocks[get_hash(a, c)] = true; } int changes = 1; calculate_res(); cout<<res<<"\n"; for(int i = 1 ; i <= q ; i++) { cin>>a>>c; ll hash = get_hash(a, c); blocks[hash] = !blocks[hash]; calculate_res(); cout<<res<<"\n"; } } |
English