#include "bits/stdc++.h"
using namespace std;
#define all(x) x.begin(),x.end()
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << " " << p.second; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#define ASSERT(...) 42
#endif
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const int oo = 1e9;
typedef complex<int> pt;
#define X real()
#define Y imag()
const pt dir4[] = {{1,0},{0,1},{-1,0},{0,-1}};
const pt dir8[] = {{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};
const pt hdir[] = {{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2},{2,-1}};
const pt sq4[] = {{0,0},{0,1},{1,0},{1,1}};
struct node {
bool unit=1;
bool lfix=0, rfix=0;
bool on=0;
int cl=0,cr=0;
int ans=0;
node() {
// *this = node(0);
}
node(int x) {
// do nothing with the x...
unit=0;
}
void flip(bool stay) {
if(stay) {
lfix^=1;
rfix^=1;
} else {
ans^=1;
on^=1;
cl^=1;
}
}
void merge(const node& o) {
if(unit) {
*this = o;
return;
} else if(o.unit) {
return;
}
if(on and o.on) {
cl+=o.cl;
return;
}
if(on) {
int my = cl;
*this = o;
cl+=my;
return;
}
if(o.on) {
cr+=o.cl;
return;
}
ans+=o.ans;
if(rfix and o.lfix) {
ans+=cr+o.cl;
}
rfix=o.rfix;
cr=o.cr;
}
friend node merge(node a, const node& b) {
a.merge(b);
return a;
}
};
struct segtree {
int ptwo;
vector<node> seg;
vi mine;
segtree(){}
node& operator[](int i) {
return seg[i+ptwo];
}
segtree(int nn) {
ptwo=nn;
seg.assign(ptwo*2,node(0));
mine.assign(ptwo*2,0);
}
auto query(int l, int r) {
assert(l>=0 and l<ptwo and r >=0 and r<ptwo);
l+=ptwo; r+=ptwo;
node resl, resr;
while(l and l<=r) {
if(l%2==1) resl = merge(resl,seg[l++]);
if(r%2==0) resr = merge(seg[r--],resr);
l/=2; r/=2;
}
return merge(resl,resr);
}
void update(int i, int val) {
assert(i>=0 and i<ptwo);
i+=ptwo;
mine[i]^=1<<val;
seg[i] = node(0);
if(mine[i]>=2) seg[i].flip(1);
else if(mine[i]==1) seg[i].flip(0);
for(i/=2;i>=1;i/=2) {
seg[i] = merge(seg[2*i],seg[2*i+1]);
}
}
void build() {
for(int i=ptwo-1;i>=1;--i) {
seg[i] = merge(seg[2*i],seg[2*i+1]);
}
}
};
struct event {
int when;
pt p;
bool stay;
bool operator<(const event& o) const {
return when<o.when;
}
};
void read(pt& p) {
int a,b; cin >> a >> b;
p = {a,b};
}
vi ans;
const int S = 1<<20;
segtree seg(S);
void solve(const vector<event>& es, int diag, bool want) {
int lastans=0;
for(auto e : es) {
int a = e.p.X+e.p.Y-diag;
int b = (e.p.X-e.p.Y);
if((!!(b%2))^want ^ a) {
continue;
}
seg.update(b+S/2,e.stay);
int now = seg.query(0,S-1).ans;
ans[e.when]+=now-lastans;
lastans=now;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
seg.build();
int n,m,k,q; cin >> n >> m >> k >> q;
vector<pt> pts(k+q);
for(auto& i : pts) {
read(i);
}
set<pi> s;
for(auto i : pts) {
int x = i.X,y = i.Y;
if(s.count({x,y})) s.erase({x,y});
else s.insert({x,y});
}
for(auto [x,y] : s) pts.push_back({x,y});
s.clear();
int N = pts.size();
vi res(N+1);
ans.assign(N+1,0);
map<pi,int> insq;
int bad=0;
int t=0;
map<int,vector<event>> es,es2;
auto addE = [&](pt to, bool stay) {
es[to.X+to.Y].push_back({t,to,stay});
to*=pt{0,1};
es2[to.X+to.Y].push_back({t,to,stay});
to*=pt{0,-1};
};
auto checksq = [&](pt p, int sgn) {
for(auto d : sq4) {
pt to = d+p;
if(!s.count({to.X,to.Y})) return;
}
for(auto d : sq4) {
pt to = d+p;
auto& res = insq[{to.X,to.Y}];
if(res==0) {
addE(to,1);
bad++;
}
res+=sgn;
if(res==0) {
addE(to,1);
bad--;
}
}
};
auto update = [&](int x, int y) {
pt p = {x,y};
addE({x,y},0);
if(s.count({x,y})) {
// deletion
for(pt d : sq4) {
pt to = p-d;
checksq(to,-1);
}
s.erase({x,y});
insq.erase({x,y});
} else {
// addition
s.insert({x,y});
insq[{x,y}]=0;
for(pt d : sq4) {
pt to = p-d;
checksq(to,1);
}
}
};
for(t=0;t<N;++t) {
int x = pts[t].X, y = pts[t].Y;
update(x,y);
res[t]=s.size()-bad;
}
for(int r=0;r<2;++r) {
for(auto& [x,v] : es) {
if(es.count(x+1)) {
auto w = v;
w.insert(w.end(),all(es[x+1]));
sort(all(w));
solve(w,x,0);
solve(w,x,1);
}
}
swap(es,es2);
}
partial_sum(all(ans),ans.begin());
for(int i=0;i<=k+q;++i) ans[i] = res[i]-ans[i];
ans = vi(ans.begin()+k-1,ans.begin()+k+q);
for(auto x : ans) cout << x << '\n';
}
/*
5 5 8 1
1 1
1 2
2 2
2 1
3 3
3 4
4 4
4 3
3 2
*/
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 | #include "bits/stdc++.h" using namespace std; #define all(x) x.begin(),x.end() template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << " " << p.second; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; } #ifdef LOCAL #include "debug.h" #else #define debug(...) 42 #define ASSERT(...) 42 #endif typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> pi; const int oo = 1e9; typedef complex<int> pt; #define X real() #define Y imag() const pt dir4[] = {{1,0},{0,1},{-1,0},{0,-1}}; const pt dir8[] = {{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}}; const pt hdir[] = {{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2},{2,-1}}; const pt sq4[] = {{0,0},{0,1},{1,0},{1,1}}; struct node { bool unit=1; bool lfix=0, rfix=0; bool on=0; int cl=0,cr=0; int ans=0; node() { // *this = node(0); } node(int x) { // do nothing with the x... unit=0; } void flip(bool stay) { if(stay) { lfix^=1; rfix^=1; } else { ans^=1; on^=1; cl^=1; } } void merge(const node& o) { if(unit) { *this = o; return; } else if(o.unit) { return; } if(on and o.on) { cl+=o.cl; return; } if(on) { int my = cl; *this = o; cl+=my; return; } if(o.on) { cr+=o.cl; return; } ans+=o.ans; if(rfix and o.lfix) { ans+=cr+o.cl; } rfix=o.rfix; cr=o.cr; } friend node merge(node a, const node& b) { a.merge(b); return a; } }; struct segtree { int ptwo; vector<node> seg; vi mine; segtree(){} node& operator[](int i) { return seg[i+ptwo]; } segtree(int nn) { ptwo=nn; seg.assign(ptwo*2,node(0)); mine.assign(ptwo*2,0); } auto query(int l, int r) { assert(l>=0 and l<ptwo and r >=0 and r<ptwo); l+=ptwo; r+=ptwo; node resl, resr; while(l and l<=r) { if(l%2==1) resl = merge(resl,seg[l++]); if(r%2==0) resr = merge(seg[r--],resr); l/=2; r/=2; } return merge(resl,resr); } void update(int i, int val) { assert(i>=0 and i<ptwo); i+=ptwo; mine[i]^=1<<val; seg[i] = node(0); if(mine[i]>=2) seg[i].flip(1); else if(mine[i]==1) seg[i].flip(0); for(i/=2;i>=1;i/=2) { seg[i] = merge(seg[2*i],seg[2*i+1]); } } void build() { for(int i=ptwo-1;i>=1;--i) { seg[i] = merge(seg[2*i],seg[2*i+1]); } } }; struct event { int when; pt p; bool stay; bool operator<(const event& o) const { return when<o.when; } }; void read(pt& p) { int a,b; cin >> a >> b; p = {a,b}; } vi ans; const int S = 1<<20; segtree seg(S); void solve(const vector<event>& es, int diag, bool want) { int lastans=0; for(auto e : es) { int a = e.p.X+e.p.Y-diag; int b = (e.p.X-e.p.Y); if((!!(b%2))^want ^ a) { continue; } seg.update(b+S/2,e.stay); int now = seg.query(0,S-1).ans; ans[e.when]+=now-lastans; lastans=now; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); seg.build(); int n,m,k,q; cin >> n >> m >> k >> q; vector<pt> pts(k+q); for(auto& i : pts) { read(i); } set<pi> s; for(auto i : pts) { int x = i.X,y = i.Y; if(s.count({x,y})) s.erase({x,y}); else s.insert({x,y}); } for(auto [x,y] : s) pts.push_back({x,y}); s.clear(); int N = pts.size(); vi res(N+1); ans.assign(N+1,0); map<pi,int> insq; int bad=0; int t=0; map<int,vector<event>> es,es2; auto addE = [&](pt to, bool stay) { es[to.X+to.Y].push_back({t,to,stay}); to*=pt{0,1}; es2[to.X+to.Y].push_back({t,to,stay}); to*=pt{0,-1}; }; auto checksq = [&](pt p, int sgn) { for(auto d : sq4) { pt to = d+p; if(!s.count({to.X,to.Y})) return; } for(auto d : sq4) { pt to = d+p; auto& res = insq[{to.X,to.Y}]; if(res==0) { addE(to,1); bad++; } res+=sgn; if(res==0) { addE(to,1); bad--; } } }; auto update = [&](int x, int y) { pt p = {x,y}; addE({x,y},0); if(s.count({x,y})) { // deletion for(pt d : sq4) { pt to = p-d; checksq(to,-1); } s.erase({x,y}); insq.erase({x,y}); } else { // addition s.insert({x,y}); insq[{x,y}]=0; for(pt d : sq4) { pt to = p-d; checksq(to,1); } } }; for(t=0;t<N;++t) { int x = pts[t].X, y = pts[t].Y; update(x,y); res[t]=s.size()-bad; } for(int r=0;r<2;++r) { for(auto& [x,v] : es) { if(es.count(x+1)) { auto w = v; w.insert(w.end(),all(es[x+1])); sort(all(w)); solve(w,x,0); solve(w,x,1); } } swap(es,es2); } partial_sum(all(ans),ans.begin()); for(int i=0;i<=k+q;++i) ans[i] = res[i]-ans[i]; ans = vi(ans.begin()+k-1,ans.begin()+k+q); for(auto x : ans) cout << x << '\n'; } /* 5 5 8 1 1 1 1 2 2 2 2 1 3 3 3 4 4 4 4 3 3 2 */ |
English