#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
#define _DEBUG
#ifdef _DEBUG
template<typename T1, typename T2> auto& operator<<(ostream& o, pair<T1, T2> a) { return o << "(" << a.first << ", " << a.second << ")"; }
template<typename T, typename OS> auto& operator<<(OS& o, T a) { o << "{"; for (auto b : a) o << b << ", "; return o << "}"; }
#define dbg(x...) cerr << "[" #x "]: ", [](auto... args) { ((cerr << args << ", "),...) << "\n"; }(x)
#else
#define dbg(...)
#endif
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
template<class T>
using iset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
using ll = long long;
using ld = long double;
using pll = pair<ll,ll>;
using vi = vector<int>;
set<pll> s, sc;
bool can_take(ll x, ll y) {
if((!sc.count({x-1, y}) && !sc.count({x+1, y})) ||
(!sc.count({x, y-1}) && !sc.count({x, y+1})))
return true;
return false;
}
ll solve() {
queue<pll> q;
sc = s;
for(auto [x, y] : sc) {
if(can_take(x, y))
q.emplace(x, y);
}
while(!q.empty()) {
auto [x, y] = q.front(); q.pop();
if(sc.count({x, y}))
sc.erase({x, y});
vector<pll> nxt = {{x-1, y}, {x+1, y}, {x, y-1}, {x, y+1}};
for(auto [ux, uy] : nxt) {
if(sc.count({ux, uy}) && can_take(ux, uy)) {
sc.erase({ux, uy});
q.emplace(ux, uy);
}
}
}
return sz(s) - sz(sc);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
ll n, m, k, q;
cin >> n >> m >> k >> q;
for(ll i = 0 ; i < k ; ++i) {
ll x, y;
cin >> x >> y;
s.emplace(x, y);
}
cout << solve() << '\n';
for(ll _ = 0 ; _ < q ; ++_) {
ll x, y; cin >> x >> y;
if(s.count({x, y})) s.erase({x, y});
else s.emplace(x, y);
cout << solve() << '\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 | #include <bits/stdc++.h> #include <bits/extc++.h> using namespace std; using namespace __gnu_pbds; #define _DEBUG #ifdef _DEBUG template<typename T1, typename T2> auto& operator<<(ostream& o, pair<T1, T2> a) { return o << "(" << a.first << ", " << a.second << ")"; } template<typename T, typename OS> auto& operator<<(OS& o, T a) { o << "{"; for (auto b : a) o << b << ", "; return o << "}"; } #define dbg(x...) cerr << "[" #x "]: ", [](auto... args) { ((cerr << args << ", "),...) << "\n"; }(x) #else #define dbg(...) #endif #define sz(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() #define F first #define S second template<class T> using iset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; using ll = long long; using ld = long double; using pll = pair<ll,ll>; using vi = vector<int>; set<pll> s, sc; bool can_take(ll x, ll y) { if((!sc.count({x-1, y}) && !sc.count({x+1, y})) || (!sc.count({x, y-1}) && !sc.count({x, y+1}))) return true; return false; } ll solve() { queue<pll> q; sc = s; for(auto [x, y] : sc) { if(can_take(x, y)) q.emplace(x, y); } while(!q.empty()) { auto [x, y] = q.front(); q.pop(); if(sc.count({x, y})) sc.erase({x, y}); vector<pll> nxt = {{x-1, y}, {x+1, y}, {x, y-1}, {x, y+1}}; for(auto [ux, uy] : nxt) { if(sc.count({ux, uy}) && can_take(ux, uy)) { sc.erase({ux, uy}); q.emplace(ux, uy); } } } return sz(s) - sz(sc); } int main() { cin.tie(0)->sync_with_stdio(0); ll n, m, k, q; cin >> n >> m >> k >> q; for(ll i = 0 ; i < k ; ++i) { ll x, y; cin >> x >> y; s.emplace(x, y); } cout << solve() << '\n'; for(ll _ = 0 ; _ < q ; ++_) { ll x, y; cin >> x >> y; if(s.count({x, y})) s.erase({x, y}); else s.emplace(x, y); cout << solve() << '\n'; } return 0; } |
English