//
#ifndef __SIZEOF_INT128__
#define __SIZEOF_INT128__
#endif
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace chrono;
using namespace __gnu_pbds;
template <typename T> using oset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define rep(i, p, k) for(int i(p); i < (k); ++i)
#define per(i, p, k) for(int i(p); i > (k); --i)
#define sz(x) (int)(x).size()
#define sc static_cast
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef __int128_t lll;
//#define int ll
template <typename T = int> using par = std::pair <T, T>;
#define fi first
#define se second
#define test int _number_of_tests(in()); while(_number_of_tests--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb emplace_back
struct Timer {
string name{""};
time_point<high_resolution_clock> end, start{high_resolution_clock::now()};
duration<float, std::milli> dur;
Timer() = default;
Timer(string nm): name(nm) {}
~Timer() {
end = high_resolution_clock::now(); dur= end - start;
cout << "@" << name << "> " << dur.count() << " ms" << '\n';
}
};
template <typename T = int> inline T in()
{
static T x;
std::cin >> x;
return x;
}
std::string yn(bool b)
{
if(b) return "YES\n";
else return "NO\n";
}
template <typename F, typename S> std::ostream& operator<<(std::ostream& out, const std::pair <F, S>& par);
template <typename T> std::ostream& operator<< (std::ostream& out, const std::vector <T>& wek)
{
for(const auto& i : wek)out << i << ' ';
return out;
}
template <typename F, typename S> std::ostream& operator<<(std::ostream& out, const std::pair <F, S>& par)
{
out << '{'<<par.first<<", "<<par.second<<"}";
return out;
}
#define show(x) cerr << #x << " = " << x << '\n';
constexpr int siz = 1<<20;
class ptree{
vector <int> le, ri;
vector <bool> v;
bool tr;
int l(int x){
if(le[x] == -1){
le[x] = sz(le);
le.pb(-1);
ri.pb(-1);
v.pb(0);
}
return le[x];
}
int r(int x){
if(ri[x] == -1){
ri[x] = sz(ri);
le.pb(-1);
ri.pb(-1);
v.pb(0);
}
return ri[x];
}
void iset(int w, int p, int k, int c, bool b){
if(p == k && p % 2 != tr)v[w] = 1;
if(p > c || k < c)return;
if(p == c && k == c){
v[w] = b;
return;
}
iset(l(w), p, (p+k)/2, c, b);
iset(r(w), (p+k)/2+1, k, c, b);
v[w] = v[l(w)] && v[r(w)];
}
bool ifull(int w, int p, int k, int a, int b){
if(p == k && p % 2 != tr)v[w] = 1;
if(p > b || a > k)return 1;
if(a <= p && k <= b)return v[w];
return ifull(l(w), p, (p+k)/2, a, b) && ifull(r(w), (p+k)/2+1, k, a, b);
}
public:
ptree() {}
ptree(bool b) : le{-1}, ri{-1}, v{0}, tr{b} {}
void set(int x, bool b){
iset(0, 1, siz, x+siz/2, b);
}
bool full(int a, int b){
// cerr << tr << ": " << a << '-' << b << '\n';
return ifull(0, 1, siz, a+siz/2, b+siz/2);
}
};
constexpr int maxn = 2e5 + 10, sizz = 3*maxn;
set <int> kwk[sizz], kww[sizz];
ptree twk[sizz], tww[sizz];
constexpr int inf = 1e9;
int wr(const set <int>& s, int x){
if(!sz(s))return inf;
auto p(s.lower_bound(x));
if(p == s.end())return inf;
else return *p;
}
int mr(const set <int>& s, int x){
if(!sz(s))return -inf;
auto p(s.upper_bound(x));
if(p == s.begin())return -inf;
else return *prev(p);
}
map <pair <int, int>, bool> ar;
map <pair <int, int>, int> kwa, ilkw;
vector <bool> zyw{0};
vector <pair <int, int>> gdz{{0,0}};
vector <set <int>> graf{set<int>{}};
int nid(){
zyw.pb(1);
graf.pb(set<int>{});
gdz.pb();
return sz(zyw)-1;
}
int ilewk = 0;
vector <pair<int,int>> ru{{-1,0},{1,0},{0,-1},{0,1}};
int newkw(int x, int y){
// cerr << "!\n";
kwa[{x,y}] = nid();
kww[x+maxn].insert(y);
kwk[y+maxn].insert(x);
// cerr << "newkw(" << sz(gdz)-1 << " = " << x << "," << y << ")\n";
gdz[sz(gdz)-1] = {x,y};
for(auto [dx,dy]: ru) if(++ilkw[{x+dx,y+dy}] == 1){
twk[y+dy+maxn].set(x+dx, 0);
tww[x+dx+maxn].set(y+dy, 0);
++ilewk;
}
return sz(gdz)-1;
}
void delkw(int k){
zyw[k] = 0;
auto [x,y] = gdz[k];
kww[x+maxn].erase(y);
kwk[y+maxn].erase(x);
// cerr << "delkw(" << k << " = " << x << "," << y << ")\n";
kwa[{x,y}] = 0;
for(auto [dx,dy]: ru)if(--ilkw[{x+dx,y+dy}] == 0){
twk[y+dy+maxn].set(x+dx, ar[{x+dx, y+dy}]);
tww[x+dx+maxn].set(y+dy, ar[{x+dx, y+dy}]);
--ilewk;
}
}
int odl(int x1, int y1, int x2, int y2){
return max(0,max(abs(x1-x2), abs(y1-y2))-3);
}
bool pel(int x1, int y1, int x2, int y2){
// cerr << "pel(" << x1 << "," << y1 << " | " << x2 << "," << y2 << ") = " << pel2(x1,y1,x2,y2) << '\n';
if(x1 == x2)return tww[x1+maxn].full(y1,y2);
if(y1 == y2)return twk[y1+maxn].full(x1,x2);
return 0;
}
bool kra(int x1, int y1, int x2, int y2){
if(!zyw[kwa[{x1,y1}]] || !zyw[kwa[{x2,y2}]] || odl(x1,y1,x2,y2) == 0)return 0;
// cerr << x1 << ',' << y1 << " -?- " << x2 << ',' << y2 << '\n';
if(abs(x1-x2) <= 1){
if(y1 > y2){
swap(x1, x2);
swap(y1, y2);
}
if(abs(x1-x2) == 1)return pel(x1,y1+2, x1,y2-2) && pel(x2,y1+2, x2,y2-2);
else return pel(x1,y1+2, x1,y2-2) && (pel(x1-1,y1+2, x1-1,y2-2) || pel(x1+1,y1+2, x1+1,y2-2));
}
if(abs(y1-y2) <= 1){
if(x1 > x2){
swap(x1, x2);
swap(y1, y2);
}
if(abs(y1-y2) == 1)return pel(x1+2,y1, x2-2,y1) && pel(x1+2,y2, x2-2,y2);
else return pel(x1+2,y1, x2-2,y1) && (pel(x1+2,y1-1, x2-2,y1-1) || pel(x1+2,y1+1, x2-2,y1+1));
}
return 0;
}
int sdlu = 0;
void updkra(int a, int b){
// cerr << "updkra(" << a << ", " << b << ")\n";
auto [x1, y1] = gdz[a];
auto [x2, y2] = gdz[b];
bool pow(kra(x1,y1,x2,y2)), jes(graf[a].find(b) != graf[a].end());
if(pow == jes)return;
if(pow){
// cerr << a << " -- " << b << '\n';
sdlu += odl(x1,y1,x2,y2);
graf[a].insert(b);
graf[b].insert(a);
}
else{
// cerr << a << " -/- " << b << '\n';
sdlu -= odl(x1,y1,x2,y2);
graf[a].erase(b);
graf[b].erase(a);
}
}
bool czykwa(int x, int y){return ar[{x-1,y}] && ar[{x+1,y}] && ar[{x,y-1}] && ar[{x,y+1}]; }
void update(int x, int y)
{
vector <int> ci;
for(auto [dx, dy]: ru){
bool pow(czykwa(x+dx,y+dy));
int jes(kwa[{x+dx,y+dy}]);
// cerr << x+dx<<":" << y+dy << '|' << jes << '-' << pow << ' ';
if(jes && !pow){
delkw(jes);
// cerr << "A" << jes << '\n';
ci.pb(jes);
}
if(!jes && pow){
ci.pb(newkw(x+dx,y+dy));
// cerr << "B" << kwa[{x+dx,y+dy}] << '\n';
}
}
vector <pair <int, int>> mo; int mi;
for(auto [p,k]: {pair{-2,-1}, pair{-1,2}, pair{2,3}}){
mo.clear(); mi = inf-1;
rep(j, p, k) mo.pb(wr(kwk[y+j+maxn], x+2),j);
for(auto i: mo)mi = min(mi, i.first);
for(auto [i,j]: mo)if(i == mi)ci.pb(kwa[{i,y+j}]);
mo.clear(); mi = -inf+1;
rep(j, p, k) mo.pb(mr(kwk[y+j+maxn], x-2),j);
for(auto i: mo)mi = max(mi, i.first);
for(auto [i,j]: mo)if(i == mi)ci.pb(kwa[{i,y+j}]);
mo.clear(); mi = inf-1;
rep(j, p, k) mo.pb(wr(kww[x+j+maxn], y+2),j);
for(auto i: mo)mi = min(mi, i.first);
for(auto [i,j]: mo)if(i == mi)ci.pb(kwa[{x+j,i}]);
mo.clear(); mi = -inf+1;
rep(j, p, k) mo.pb(mr(kww[x+j+maxn], y-2),j);
for(auto i: mo)mi = max(mi, i.first);
for(auto [i,j]: mo)if(i == mi)ci.pb(kwa[{x+j,i}]);
}
// cerr << "ci: " << ci << '\n';
rep(i, 0, sz(ci))rep(j, i+1, sz(ci))updkra(ci[i],ci[j]);
}
int raz;
void work(int x, int y)
{
// cerr << x << ' ' << y << '\n';
if(ar[{x,y}])--raz;
else ++raz;
ar[{x,y}] ^= 1;
twk[y+maxn].set(x, ar[{x,y}] && !ilkw[{x,y}]);
tww[x+maxn].set(y, ar[{x,y}] && !ilkw[{x,y}]);
update(x,y);
}
std::int32_t main()
{
rep(i, 0, sizz)twk[i] = i%2;
rep(i, 0, sizz)tww[i] = i%2;
std::cout.tie(nullptr); //for luck
std::cin.tie(nullptr); std::ios_base::sync_with_stdio(0);
int n(in()), m(in()), k(in()), q(in());
// vector <vector <bool>> v(n, vector<bool>(m));
if(k == 0)cout << 0 << '\n';
rep(i, 0, k+q){
int a(in()), b(in());
work(b-a, a+b);
// v[a-1][b-1] = v[a-1][b-1] ^ 1;
// if(i-k+2 >= 7430 && i-k+2 <= 7460){
// cout << '\n';
// cout << i-k+2 << '\n';
// rep(i, 0, 5){
// rep(j, 0, 5)cout << (v[i][j] ? '#' : '.');
// cout << '\n';
// }
// }
// rep(i, -5, 10){
// rep(j, -5, 10)cout << (kwa[{i,j}]? char('0'+kwa[{i,j}]) : (ar[{i,j}]? '#' : '.'));
// cout << '\n';
// }
// cout << '\n';
if(i >= k-1)cout << raz-sdlu - ilewk << '\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 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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | // #ifndef __SIZEOF_INT128__ #define __SIZEOF_INT128__ #endif #pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace chrono; using namespace __gnu_pbds; template <typename T> using oset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>; #define rep(i, p, k) for(int i(p); i < (k); ++i) #define per(i, p, k) for(int i(p); i > (k); --i) #define sz(x) (int)(x).size() #define sc static_cast typedef long long ll; typedef long double ld; typedef unsigned int uint; typedef unsigned long long ull; typedef __int128_t lll; //#define int ll template <typename T = int> using par = std::pair <T, T>; #define fi first #define se second #define test int _number_of_tests(in()); while(_number_of_tests--) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define pb emplace_back struct Timer { string name{""}; time_point<high_resolution_clock> end, start{high_resolution_clock::now()}; duration<float, std::milli> dur; Timer() = default; Timer(string nm): name(nm) {} ~Timer() { end = high_resolution_clock::now(); dur= end - start; cout << "@" << name << "> " << dur.count() << " ms" << '\n'; } }; template <typename T = int> inline T in() { static T x; std::cin >> x; return x; } std::string yn(bool b) { if(b) return "YES\n"; else return "NO\n"; } template <typename F, typename S> std::ostream& operator<<(std::ostream& out, const std::pair <F, S>& par); template <typename T> std::ostream& operator<< (std::ostream& out, const std::vector <T>& wek) { for(const auto& i : wek)out << i << ' '; return out; } template <typename F, typename S> std::ostream& operator<<(std::ostream& out, const std::pair <F, S>& par) { out << '{'<<par.first<<", "<<par.second<<"}"; return out; } #define show(x) cerr << #x << " = " << x << '\n'; constexpr int siz = 1<<20; class ptree{ vector <int> le, ri; vector <bool> v; bool tr; int l(int x){ if(le[x] == -1){ le[x] = sz(le); le.pb(-1); ri.pb(-1); v.pb(0); } return le[x]; } int r(int x){ if(ri[x] == -1){ ri[x] = sz(ri); le.pb(-1); ri.pb(-1); v.pb(0); } return ri[x]; } void iset(int w, int p, int k, int c, bool b){ if(p == k && p % 2 != tr)v[w] = 1; if(p > c || k < c)return; if(p == c && k == c){ v[w] = b; return; } iset(l(w), p, (p+k)/2, c, b); iset(r(w), (p+k)/2+1, k, c, b); v[w] = v[l(w)] && v[r(w)]; } bool ifull(int w, int p, int k, int a, int b){ if(p == k && p % 2 != tr)v[w] = 1; if(p > b || a > k)return 1; if(a <= p && k <= b)return v[w]; return ifull(l(w), p, (p+k)/2, a, b) && ifull(r(w), (p+k)/2+1, k, a, b); } public: ptree() {} ptree(bool b) : le{-1}, ri{-1}, v{0}, tr{b} {} void set(int x, bool b){ iset(0, 1, siz, x+siz/2, b); } bool full(int a, int b){ // cerr << tr << ": " << a << '-' << b << '\n'; return ifull(0, 1, siz, a+siz/2, b+siz/2); } }; constexpr int maxn = 2e5 + 10, sizz = 3*maxn; set <int> kwk[sizz], kww[sizz]; ptree twk[sizz], tww[sizz]; constexpr int inf = 1e9; int wr(const set <int>& s, int x){ if(!sz(s))return inf; auto p(s.lower_bound(x)); if(p == s.end())return inf; else return *p; } int mr(const set <int>& s, int x){ if(!sz(s))return -inf; auto p(s.upper_bound(x)); if(p == s.begin())return -inf; else return *prev(p); } map <pair <int, int>, bool> ar; map <pair <int, int>, int> kwa, ilkw; vector <bool> zyw{0}; vector <pair <int, int>> gdz{{0,0}}; vector <set <int>> graf{set<int>{}}; int nid(){ zyw.pb(1); graf.pb(set<int>{}); gdz.pb(); return sz(zyw)-1; } int ilewk = 0; vector <pair<int,int>> ru{{-1,0},{1,0},{0,-1},{0,1}}; int newkw(int x, int y){ // cerr << "!\n"; kwa[{x,y}] = nid(); kww[x+maxn].insert(y); kwk[y+maxn].insert(x); // cerr << "newkw(" << sz(gdz)-1 << " = " << x << "," << y << ")\n"; gdz[sz(gdz)-1] = {x,y}; for(auto [dx,dy]: ru) if(++ilkw[{x+dx,y+dy}] == 1){ twk[y+dy+maxn].set(x+dx, 0); tww[x+dx+maxn].set(y+dy, 0); ++ilewk; } return sz(gdz)-1; } void delkw(int k){ zyw[k] = 0; auto [x,y] = gdz[k]; kww[x+maxn].erase(y); kwk[y+maxn].erase(x); // cerr << "delkw(" << k << " = " << x << "," << y << ")\n"; kwa[{x,y}] = 0; for(auto [dx,dy]: ru)if(--ilkw[{x+dx,y+dy}] == 0){ twk[y+dy+maxn].set(x+dx, ar[{x+dx, y+dy}]); tww[x+dx+maxn].set(y+dy, ar[{x+dx, y+dy}]); --ilewk; } } int odl(int x1, int y1, int x2, int y2){ return max(0,max(abs(x1-x2), abs(y1-y2))-3); } bool pel(int x1, int y1, int x2, int y2){ // cerr << "pel(" << x1 << "," << y1 << " | " << x2 << "," << y2 << ") = " << pel2(x1,y1,x2,y2) << '\n'; if(x1 == x2)return tww[x1+maxn].full(y1,y2); if(y1 == y2)return twk[y1+maxn].full(x1,x2); return 0; } bool kra(int x1, int y1, int x2, int y2){ if(!zyw[kwa[{x1,y1}]] || !zyw[kwa[{x2,y2}]] || odl(x1,y1,x2,y2) == 0)return 0; // cerr << x1 << ',' << y1 << " -?- " << x2 << ',' << y2 << '\n'; if(abs(x1-x2) <= 1){ if(y1 > y2){ swap(x1, x2); swap(y1, y2); } if(abs(x1-x2) == 1)return pel(x1,y1+2, x1,y2-2) && pel(x2,y1+2, x2,y2-2); else return pel(x1,y1+2, x1,y2-2) && (pel(x1-1,y1+2, x1-1,y2-2) || pel(x1+1,y1+2, x1+1,y2-2)); } if(abs(y1-y2) <= 1){ if(x1 > x2){ swap(x1, x2); swap(y1, y2); } if(abs(y1-y2) == 1)return pel(x1+2,y1, x2-2,y1) && pel(x1+2,y2, x2-2,y2); else return pel(x1+2,y1, x2-2,y1) && (pel(x1+2,y1-1, x2-2,y1-1) || pel(x1+2,y1+1, x2-2,y1+1)); } return 0; } int sdlu = 0; void updkra(int a, int b){ // cerr << "updkra(" << a << ", " << b << ")\n"; auto [x1, y1] = gdz[a]; auto [x2, y2] = gdz[b]; bool pow(kra(x1,y1,x2,y2)), jes(graf[a].find(b) != graf[a].end()); if(pow == jes)return; if(pow){ // cerr << a << " -- " << b << '\n'; sdlu += odl(x1,y1,x2,y2); graf[a].insert(b); graf[b].insert(a); } else{ // cerr << a << " -/- " << b << '\n'; sdlu -= odl(x1,y1,x2,y2); graf[a].erase(b); graf[b].erase(a); } } bool czykwa(int x, int y){return ar[{x-1,y}] && ar[{x+1,y}] && ar[{x,y-1}] && ar[{x,y+1}]; } void update(int x, int y) { vector <int> ci; for(auto [dx, dy]: ru){ bool pow(czykwa(x+dx,y+dy)); int jes(kwa[{x+dx,y+dy}]); // cerr << x+dx<<":" << y+dy << '|' << jes << '-' << pow << ' '; if(jes && !pow){ delkw(jes); // cerr << "A" << jes << '\n'; ci.pb(jes); } if(!jes && pow){ ci.pb(newkw(x+dx,y+dy)); // cerr << "B" << kwa[{x+dx,y+dy}] << '\n'; } } vector <pair <int, int>> mo; int mi; for(auto [p,k]: {pair{-2,-1}, pair{-1,2}, pair{2,3}}){ mo.clear(); mi = inf-1; rep(j, p, k) mo.pb(wr(kwk[y+j+maxn], x+2),j); for(auto i: mo)mi = min(mi, i.first); for(auto [i,j]: mo)if(i == mi)ci.pb(kwa[{i,y+j}]); mo.clear(); mi = -inf+1; rep(j, p, k) mo.pb(mr(kwk[y+j+maxn], x-2),j); for(auto i: mo)mi = max(mi, i.first); for(auto [i,j]: mo)if(i == mi)ci.pb(kwa[{i,y+j}]); mo.clear(); mi = inf-1; rep(j, p, k) mo.pb(wr(kww[x+j+maxn], y+2),j); for(auto i: mo)mi = min(mi, i.first); for(auto [i,j]: mo)if(i == mi)ci.pb(kwa[{x+j,i}]); mo.clear(); mi = -inf+1; rep(j, p, k) mo.pb(mr(kww[x+j+maxn], y-2),j); for(auto i: mo)mi = max(mi, i.first); for(auto [i,j]: mo)if(i == mi)ci.pb(kwa[{x+j,i}]); } // cerr << "ci: " << ci << '\n'; rep(i, 0, sz(ci))rep(j, i+1, sz(ci))updkra(ci[i],ci[j]); } int raz; void work(int x, int y) { // cerr << x << ' ' << y << '\n'; if(ar[{x,y}])--raz; else ++raz; ar[{x,y}] ^= 1; twk[y+maxn].set(x, ar[{x,y}] && !ilkw[{x,y}]); tww[x+maxn].set(y, ar[{x,y}] && !ilkw[{x,y}]); update(x,y); } std::int32_t main() { rep(i, 0, sizz)twk[i] = i%2; rep(i, 0, sizz)tww[i] = i%2; std::cout.tie(nullptr); //for luck std::cin.tie(nullptr); std::ios_base::sync_with_stdio(0); int n(in()), m(in()), k(in()), q(in()); // vector <vector <bool>> v(n, vector<bool>(m)); if(k == 0)cout << 0 << '\n'; rep(i, 0, k+q){ int a(in()), b(in()); work(b-a, a+b); // v[a-1][b-1] = v[a-1][b-1] ^ 1; // if(i-k+2 >= 7430 && i-k+2 <= 7460){ // cout << '\n'; // cout << i-k+2 << '\n'; // rep(i, 0, 5){ // rep(j, 0, 5)cout << (v[i][j] ? '#' : '.'); // cout << '\n'; // } // } // rep(i, -5, 10){ // rep(j, -5, 10)cout << (kwa[{i,j}]? char('0'+kwa[{i,j}]) : (ar[{i,j}]? '#' : '.')); // cout << '\n'; // } // cout << '\n'; if(i >= k-1)cout << raz-sdlu - ilewk << '\n'; } return 0; } |
English