#pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,abm,mmx,tune=native") //#pragma clang diagnostic ignored "-Wunused-const-variable" #include<vector> #include<iostream> #include<stack> #include<cmath> #include<algorithm> #include<set> #include<map> #include<string> #include<tuple> #include<bitset> #include<queue> #include<unordered_map> #include<unordered_set> #include<random> #include<assert.h> #include<ctime> #include <cassert> #include <thread> #include <mutex> #include <semaphore> #include <chrono> #include <hash_map> //#define int long long #define all(a) a.begin(), a.end() typedef long long ll; typedef long double ld; typedef unsigned long long ull; using namespace std; ll gcd(ll i, ll j) { if (i < j)swap(i, j); if (j == 0)return i; else return gcd(j, i % j); } #ifdef _MY ll __builtin_popcount(ll x) { return x ? (__builtin_popcount(x >> 1) + (x & 1)) : 0; } #endif template<typename T> inline T getint() { T val = 0; char c; bool neg = false; while ((c = getchar()) && !(c >= '0' && c <= '9')) { neg |= c == '-'; } do { val = (val * 10) + c - '0'; } while ((c = getchar()) && (c >= '0' && c <= '9')); return val * (neg ? -1 : 1); } const ll INF = 1e7 + 100; const int mod = 998244353, mod1 = 1000000007; const ld eps = 1e-7, pi = acos(-1); const int maxT = 503, maxN = 5'010, A = 311; ll bp(ll et, ll b) { ll res = 1; for (int i = 30; i >= 0; --i) { res = (res * res) % mod; if ((b & (1 << i)) != 0)res = (res * et) % mod; } return res; } mt19937 mt_rand(time(0)); void panic() { cout << "No\n"; exit(0); } ld get_time() { ll tmr = clock(); return (ld)tmr / CLOCKS_PER_SEC; } int pl(int a, int b) { a += b; if (a >= mod) a -= mod; return a; } int mul(ll a, ll b) { return a * b % mod; } const ll sz = 1 << 19; ll t[sz * 2]; ll mx_ans = 0; ll get(int ps) { return t[ps]; } //stdext::hash_map<ll, int>mp; unordered_map<ll, int>mp; const ll BASE = 1 << 20; const vector<ll>goo = { -BASE, BASE, -1, 1, }; ll get_code(int x, int y) { return (ll)x * BASE + y; } ll curr_ans = -1; bool can_delete_code_init(const ll code) { if ((!mp.count(code + BASE) && !mp.count(code - BASE)) || (!mp.count(code + 1) && !mp.count(code - 1))) { return true; } return false; } int get_or_else(const ll code, const int el) { auto it = mp.find(code); if (it != mp.end()) { return it->second; } return el; } bool can_delete_code_on_iter(const ll code) { if ((get_or_else(code + BASE, 1) && get_or_else(code - BASE, 1)) || (get_or_else(code + 1, 1) && get_or_else(code - 1, 1))) { return true; } return false; } ll m_q[1 << 21]; ll m_ql = -1, m_qr = -1; void add_to_q(ll cd) { m_q[m_qr++] = cd; } bool q_not_empty() { return m_qr != m_ql; } ll get_from_q_front() { return m_q[m_ql++]; } void reset_q() { m_ql = m_qr = 0; } void run_bfs() { while (q_not_empty()) { const ll cd = get_from_q_front(); ++curr_ans; for (auto delCD : goo) { const ll ncd = cd + delCD; auto nit = mp.find(ncd); if (nit != mp.end() && nit->second == 0) { if (can_delete_code_on_iter(ncd)) { add_to_q(ncd); nit->second = 1; } } } } } void hard_recalc() { reset_q(); curr_ans = 0; for (auto& vert : mp) { vert.second = 0; if (can_delete_code_init(vert.first)) { vert.second = 1; add_to_q(vert.first); } } run_bfs(); } //google::dense_hash_map void solve() { int n, m, k, q; cin >> n >> m >> k >> q; for (int i = 0; i < k; ++i) { int x, y; cin >> x >> y; ll code = get_code(x, y); auto aa = mp.emplace(code, 0); if (!aa.second) { mp.erase(aa.first); } } hard_recalc(); cout << curr_ans << "\n"; while (q--) { int x, y; //x = rand() % 100 + 1 + 1e5, y = rand() % 100 + 1 + 1e5; cin >> x >> y; const ll code = get_code(x, y); auto aa = mp.emplace(code, 0); if (aa.second) { //insert code if (can_delete_code_init(code)) { ++curr_ans; aa.first->second = 1; } else { vector<ll>nqq; reset_q(); add_to_q(code); while (q_not_empty()) { const ll cd = get_from_q_front(); nqq.push_back(cd); for (auto delCD : goo) { const ll ncd = cd + delCD; auto nit = mp.find(ncd); if (nit != mp.end() && nit->second == 1 && !can_delete_code_init(ncd) ) { add_to_q(ncd); nit->second = 0; --curr_ans; } } } if (!nqq.empty()) { reset_q(); for (auto x : nqq) { if (can_delete_code_on_iter(x)) { add_to_q(x); auto it = mp.find(x); it->second = 1; } } run_bfs(); } } } else { //delete code if (aa.first->second) { --curr_ans; mp.erase(aa.first); } else { mp.erase(aa.first); //hard_recalc(); reset_q(); for (auto dcd : goo) { const ll ncd = code + dcd; auto nit = mp.find(ncd); if (nit != mp.end() && nit->second == 0) { if (can_delete_code_on_iter(ncd)) { add_to_q(ncd); nit->second = 1; } } } run_bfs(); } } //#ifdef _DEBUG //ll cmm_ans = curr_ans; //hard_recalc(); //assert(cmm_ans == curr_ans); //#endif // _DEBUG cout << curr_ans << "\n"; } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.precision(14); #ifdef _MY freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); /*time_t start, end; time(&start);*/ #else //freopen("epic.in", "r", stdin); freopen("epic.out", "w", stdout); #endif //init(); int t = 1; //cin >> t; while (t--) { solve(); } #ifdef _MY cout << "Time taken is : " << fixed << get_time() << " sec " << "\n"; #endif 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 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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,abm,mmx,tune=native") //#pragma clang diagnostic ignored "-Wunused-const-variable" #include<vector> #include<iostream> #include<stack> #include<cmath> #include<algorithm> #include<set> #include<map> #include<string> #include<tuple> #include<bitset> #include<queue> #include<unordered_map> #include<unordered_set> #include<random> #include<assert.h> #include<ctime> #include <cassert> #include <thread> #include <mutex> #include <semaphore> #include <chrono> #include <hash_map> //#define int long long #define all(a) a.begin(), a.end() typedef long long ll; typedef long double ld; typedef unsigned long long ull; using namespace std; ll gcd(ll i, ll j) { if (i < j)swap(i, j); if (j == 0)return i; else return gcd(j, i % j); } #ifdef _MY ll __builtin_popcount(ll x) { return x ? (__builtin_popcount(x >> 1) + (x & 1)) : 0; } #endif template<typename T> inline T getint() { T val = 0; char c; bool neg = false; while ((c = getchar()) && !(c >= '0' && c <= '9')) { neg |= c == '-'; } do { val = (val * 10) + c - '0'; } while ((c = getchar()) && (c >= '0' && c <= '9')); return val * (neg ? -1 : 1); } const ll INF = 1e7 + 100; const int mod = 998244353, mod1 = 1000000007; const ld eps = 1e-7, pi = acos(-1); const int maxT = 503, maxN = 5'010, A = 311; ll bp(ll et, ll b) { ll res = 1; for (int i = 30; i >= 0; --i) { res = (res * res) % mod; if ((b & (1 << i)) != 0)res = (res * et) % mod; } return res; } mt19937 mt_rand(time(0)); void panic() { cout << "No\n"; exit(0); } ld get_time() { ll tmr = clock(); return (ld)tmr / CLOCKS_PER_SEC; } int pl(int a, int b) { a += b; if (a >= mod) a -= mod; return a; } int mul(ll a, ll b) { return a * b % mod; } const ll sz = 1 << 19; ll t[sz * 2]; ll mx_ans = 0; ll get(int ps) { return t[ps]; } //stdext::hash_map<ll, int>mp; unordered_map<ll, int>mp; const ll BASE = 1 << 20; const vector<ll>goo = { -BASE, BASE, -1, 1, }; ll get_code(int x, int y) { return (ll)x * BASE + y; } ll curr_ans = -1; bool can_delete_code_init(const ll code) { if ((!mp.count(code + BASE) && !mp.count(code - BASE)) || (!mp.count(code + 1) && !mp.count(code - 1))) { return true; } return false; } int get_or_else(const ll code, const int el) { auto it = mp.find(code); if (it != mp.end()) { return it->second; } return el; } bool can_delete_code_on_iter(const ll code) { if ((get_or_else(code + BASE, 1) && get_or_else(code - BASE, 1)) || (get_or_else(code + 1, 1) && get_or_else(code - 1, 1))) { return true; } return false; } ll m_q[1 << 21]; ll m_ql = -1, m_qr = -1; void add_to_q(ll cd) { m_q[m_qr++] = cd; } bool q_not_empty() { return m_qr != m_ql; } ll get_from_q_front() { return m_q[m_ql++]; } void reset_q() { m_ql = m_qr = 0; } void run_bfs() { while (q_not_empty()) { const ll cd = get_from_q_front(); ++curr_ans; for (auto delCD : goo) { const ll ncd = cd + delCD; auto nit = mp.find(ncd); if (nit != mp.end() && nit->second == 0) { if (can_delete_code_on_iter(ncd)) { add_to_q(ncd); nit->second = 1; } } } } } void hard_recalc() { reset_q(); curr_ans = 0; for (auto& vert : mp) { vert.second = 0; if (can_delete_code_init(vert.first)) { vert.second = 1; add_to_q(vert.first); } } run_bfs(); } //google::dense_hash_map void solve() { int n, m, k, q; cin >> n >> m >> k >> q; for (int i = 0; i < k; ++i) { int x, y; cin >> x >> y; ll code = get_code(x, y); auto aa = mp.emplace(code, 0); if (!aa.second) { mp.erase(aa.first); } } hard_recalc(); cout << curr_ans << "\n"; while (q--) { int x, y; //x = rand() % 100 + 1 + 1e5, y = rand() % 100 + 1 + 1e5; cin >> x >> y; const ll code = get_code(x, y); auto aa = mp.emplace(code, 0); if (aa.second) { //insert code if (can_delete_code_init(code)) { ++curr_ans; aa.first->second = 1; } else { vector<ll>nqq; reset_q(); add_to_q(code); while (q_not_empty()) { const ll cd = get_from_q_front(); nqq.push_back(cd); for (auto delCD : goo) { const ll ncd = cd + delCD; auto nit = mp.find(ncd); if (nit != mp.end() && nit->second == 1 && !can_delete_code_init(ncd) ) { add_to_q(ncd); nit->second = 0; --curr_ans; } } } if (!nqq.empty()) { reset_q(); for (auto x : nqq) { if (can_delete_code_on_iter(x)) { add_to_q(x); auto it = mp.find(x); it->second = 1; } } run_bfs(); } } } else { //delete code if (aa.first->second) { --curr_ans; mp.erase(aa.first); } else { mp.erase(aa.first); //hard_recalc(); reset_q(); for (auto dcd : goo) { const ll ncd = code + dcd; auto nit = mp.find(ncd); if (nit != mp.end() && nit->second == 0) { if (can_delete_code_on_iter(ncd)) { add_to_q(ncd); nit->second = 1; } } } run_bfs(); } } //#ifdef _DEBUG //ll cmm_ans = curr_ans; //hard_recalc(); //assert(cmm_ans == curr_ans); //#endif // _DEBUG cout << curr_ans << "\n"; } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.precision(14); #ifdef _MY freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); /*time_t start, end; time(&start);*/ #else //freopen("epic.in", "r", stdin); freopen("epic.out", "w", stdout); #endif //init(); int t = 1; //cin >> t; while (t--) { solve(); } #ifdef _MY cout << "Time taken is : " << fixed << get_time() << " sec " << "\n"; #endif return 0; } |