#ifndef LOCAL #pragma GCC optimize("O3,unroll-loops") #endif #include <bits/stdc++.h> #define fi first #define se second #define pn printf("\n") #define ssize(x) int(x.size()) #define all(x) x.begin(),x.end() #define rall(x) x.rbegin(),x.rend() #define bitcount(x) __builtin_popcount(x) #define clz(x) __builtin_clz(x) #define ctz(x) __builtin_ctz(x) #define mp make_pair //~ #define r(x) resize(x) //~ #define rf(x, c) resize(x, c) using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<int, ll> pil; typedef pair<ll, int> pli; typedef pair<ll, ll> pll; typedef double db; typedef long double ldb; #define V vector int inf = 2e09; ll infll = 2e18; int mod = (1<<23)*119+1; int add(int a, int b){return a+b >= mod ? a+b - mod : a+b;} int sub(int a, int b){return a-b < 0 ? a-b + mod : a-b;} int mul(int a, int b){return int(a * ll(b) % mod);} int fpow(int a, ll b){ int ret = 1; while(b){ if(b & 1) ret = mul(ret, a); b >>= 1, a = mul(a, a); } return ret; } int inv(int a){ return fpow(a, mod-2); } struct coeff{ V<int> fac, invfac; coeff(int n){ fac.resize(n+1), invfac.resize(n+1); fac[0] = 1, invfac[0] = 1; for(int i = 1; i <= n; ++i) fac[i] = mul(fac[i-1], i); invfac[n] = inv(fac[n]); for(int i = n-1; i; --i) invfac[i] = mul(invfac[i+1], i+1); } int get(int n, int k){ if(n < k) return 0; return mul(fac[n], mul(invfac[n-k], invfac[k])); } }; void answer(){ int n, m, k, Q; scanf("%d%d%d%d", &n, &m, &k, &Q); V<int> t(k+Q+1, 0); unordered_map<ll, int> mp; V<tuple<int, int>> cord(k+Q+1); auto hsh = [&](int a, int b){ return (ll(a)<<32)|b; }; auto nr = [&](int a, int b){ return mp[hsh(a, b)]; }; int N = 0; for(int i = 0; i < k; ++i){ int a, b; scanf("%d%d", &a, &b); if(!mp[hsh(a, b)]) mp[hsh(a, b)] = ++N; cord[nr(a, b)] = {a, b}; t[nr(a, b)] = 1; } V<tuple<int, int>> q_list(Q); for(int i = 0; i < Q; ++i){ int a, b; scanf("%d%d", &a, &b); q_list[i] = {a, b}; if(!mp[hsh(a, b)]) mp[hsh(a, b)] = ++N; cord[nr(a, b)] = {a, b}; } V<int> type(N+1); // hard = 3, soft = 1, mid(rozwidlak) = 2 auto chk_hard = [&](int a, int b){ bool hard = 0; for(int i = -1; i <= 1; i += 2) for(int j = -1; j <= 1; j += 2) if(t[nr(a+i, b)] && t[nr(a+i, b+j)] && t[nr(a, b+j)]){ hard = 1; break; } return hard; }; auto chk_soft = [&](int a, int b){ return (!t[nr(a-1, b)] && !t[nr(a+1, b)]) || (!t[nr(a, b-1)] && !t[nr(a, b+1)]); }; auto get_type = [&](int a, int b){ if(chk_soft(a, b)) return 1; if(chk_hard(a, b)) return 3; return 2; // w przeciwnym przypadku gosciu to rozwidlak }; auto update_board = [&](int a, int b){ t[nr(a, b)] ^= 1; for(int i = a-1; i <= a+1; ++i) for(int j = b-1; j <= b+1; ++j) if(nr(i, j)) type[nr(i, j)] = t[nr(i, j)] ? get_type(i, j) : 0; // potencjalnie wyoptować }; for(int i = 1; i <= N; ++i){ auto [a, b] = cord[i]; type[i] = get_type(a, b); } V<int> col(N+1, 0), col_size(N+1, 0), good(N+1, 0); auto bfs = [&](int x, int l){ queue<int> q; q.emplace(x); col[x] = l; while(ssize(q)){ x = q.front(); q.pop(); auto &[a, b] = cord[x]; if(type[x] == 1) good[col[x]] = 1; ++col_size[col[x]]; for(int i = -1, u; i <= 1; i += 2){ u = nr(a+i, b); if(t[u] && type[u] != 3 && !col[u]) // tutaj chyba t[0] powinno zawsze byc 0, musze tego pilnowac q.emplace(u), col[u] = l; u = nr(a, b+i); if(t[u] && type[u] != 3 && !col[u]) // tutaj chyba t[0] powinno zawsze byc 0, musze tego pilnowac q.emplace(u), col[u] = l; } } }; for(int tt = -1; tt < Q; ++tt){ if(~tt){ auto &[a, b] = q_list[tt]; update_board(a, b); } int l = 0; col = V<int>(N+1, 0), col_size = V<int>(N+1, 0), good = V<int> (N+1, 0); for(int i = 1; i <= N; ++i) if(t[i] && type[i] != 3 && !col[i]) ++l, bfs(i, l); // printf("%d:\n", tt); // for(int i = 1; i <= N; ++i) printf("%d ", col[i]); // pn; // for(int i = 1; i <= N; ++i) printf("%d ", type[i]); // pn; int result = 0; for(int i = 1; i <= N; ++i) result += good[i]*col_size[i]; printf("%d\n", result); } } int main(){ int T = 1; // scanf("%d", &T); //~ ios_base::sync_with_stdio(0); cin.tie(0); cin >> T; for(++T; --T; ) answer(); 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 | #ifndef LOCAL #pragma GCC optimize("O3,unroll-loops") #endif #include <bits/stdc++.h> #define fi first #define se second #define pn printf("\n") #define ssize(x) int(x.size()) #define all(x) x.begin(),x.end() #define rall(x) x.rbegin(),x.rend() #define bitcount(x) __builtin_popcount(x) #define clz(x) __builtin_clz(x) #define ctz(x) __builtin_ctz(x) #define mp make_pair //~ #define r(x) resize(x) //~ #define rf(x, c) resize(x, c) using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<int, ll> pil; typedef pair<ll, int> pli; typedef pair<ll, ll> pll; typedef double db; typedef long double ldb; #define V vector int inf = 2e09; ll infll = 2e18; int mod = (1<<23)*119+1; int add(int a, int b){return a+b >= mod ? a+b - mod : a+b;} int sub(int a, int b){return a-b < 0 ? a-b + mod : a-b;} int mul(int a, int b){return int(a * ll(b) % mod);} int fpow(int a, ll b){ int ret = 1; while(b){ if(b & 1) ret = mul(ret, a); b >>= 1, a = mul(a, a); } return ret; } int inv(int a){ return fpow(a, mod-2); } struct coeff{ V<int> fac, invfac; coeff(int n){ fac.resize(n+1), invfac.resize(n+1); fac[0] = 1, invfac[0] = 1; for(int i = 1; i <= n; ++i) fac[i] = mul(fac[i-1], i); invfac[n] = inv(fac[n]); for(int i = n-1; i; --i) invfac[i] = mul(invfac[i+1], i+1); } int get(int n, int k){ if(n < k) return 0; return mul(fac[n], mul(invfac[n-k], invfac[k])); } }; void answer(){ int n, m, k, Q; scanf("%d%d%d%d", &n, &m, &k, &Q); V<int> t(k+Q+1, 0); unordered_map<ll, int> mp; V<tuple<int, int>> cord(k+Q+1); auto hsh = [&](int a, int b){ return (ll(a)<<32)|b; }; auto nr = [&](int a, int b){ return mp[hsh(a, b)]; }; int N = 0; for(int i = 0; i < k; ++i){ int a, b; scanf("%d%d", &a, &b); if(!mp[hsh(a, b)]) mp[hsh(a, b)] = ++N; cord[nr(a, b)] = {a, b}; t[nr(a, b)] = 1; } V<tuple<int, int>> q_list(Q); for(int i = 0; i < Q; ++i){ int a, b; scanf("%d%d", &a, &b); q_list[i] = {a, b}; if(!mp[hsh(a, b)]) mp[hsh(a, b)] = ++N; cord[nr(a, b)] = {a, b}; } V<int> type(N+1); // hard = 3, soft = 1, mid(rozwidlak) = 2 auto chk_hard = [&](int a, int b){ bool hard = 0; for(int i = -1; i <= 1; i += 2) for(int j = -1; j <= 1; j += 2) if(t[nr(a+i, b)] && t[nr(a+i, b+j)] && t[nr(a, b+j)]){ hard = 1; break; } return hard; }; auto chk_soft = [&](int a, int b){ return (!t[nr(a-1, b)] && !t[nr(a+1, b)]) || (!t[nr(a, b-1)] && !t[nr(a, b+1)]); }; auto get_type = [&](int a, int b){ if(chk_soft(a, b)) return 1; if(chk_hard(a, b)) return 3; return 2; // w przeciwnym przypadku gosciu to rozwidlak }; auto update_board = [&](int a, int b){ t[nr(a, b)] ^= 1; for(int i = a-1; i <= a+1; ++i) for(int j = b-1; j <= b+1; ++j) if(nr(i, j)) type[nr(i, j)] = t[nr(i, j)] ? get_type(i, j) : 0; // potencjalnie wyoptować }; for(int i = 1; i <= N; ++i){ auto [a, b] = cord[i]; type[i] = get_type(a, b); } V<int> col(N+1, 0), col_size(N+1, 0), good(N+1, 0); auto bfs = [&](int x, int l){ queue<int> q; q.emplace(x); col[x] = l; while(ssize(q)){ x = q.front(); q.pop(); auto &[a, b] = cord[x]; if(type[x] == 1) good[col[x]] = 1; ++col_size[col[x]]; for(int i = -1, u; i <= 1; i += 2){ u = nr(a+i, b); if(t[u] && type[u] != 3 && !col[u]) // tutaj chyba t[0] powinno zawsze byc 0, musze tego pilnowac q.emplace(u), col[u] = l; u = nr(a, b+i); if(t[u] && type[u] != 3 && !col[u]) // tutaj chyba t[0] powinno zawsze byc 0, musze tego pilnowac q.emplace(u), col[u] = l; } } }; for(int tt = -1; tt < Q; ++tt){ if(~tt){ auto &[a, b] = q_list[tt]; update_board(a, b); } int l = 0; col = V<int>(N+1, 0), col_size = V<int>(N+1, 0), good = V<int> (N+1, 0); for(int i = 1; i <= N; ++i) if(t[i] && type[i] != 3 && !col[i]) ++l, bfs(i, l); // printf("%d:\n", tt); // for(int i = 1; i <= N; ++i) printf("%d ", col[i]); // pn; // for(int i = 1; i <= N; ++i) printf("%d ", type[i]); // pn; int result = 0; for(int i = 1; i <= N; ++i) result += good[i]*col_size[i]; printf("%d\n", result); } } int main(){ int T = 1; // scanf("%d", &T); //~ ios_base::sync_with_stdio(0); cin.tie(0); cin >> T; for(++T; --T; ) answer(); return 0; } |