#include <bits/stdc++.h>
using namespace std;
#define fwd(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) fwd(i, 0, n)
#define all(X) X.begin(), X.end()
#define sz(X) int(size(X))
#define pb push_back
#define eb emplace_back
#define st first
#define nd second
using pii = pair<int, int>; using vi = vector<int>;
using ll = long long; using ld = long double;
#ifdef LOC
auto SS = signal(6, [](int) { *(int *)0 = 0; });
#define DTP(x, y) auto operator << (auto &o, auto a) -> decltype(y, o) { o << "("; x; return o << ")"; }
DTP(o << a.st << ", " << a.nd, a.nd);
DTP(for (auto i : a) o << i << ", ", all(a));
void dump(auto... x) { (( cerr << x << ", " ), ...) <<
'\n'; }
#define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", dump(x)
#else
#define deb(...) 0
#endif
struct Node{
int left = -1, right = -1, res = 0, resLeft = -1, resRight = -1;
bool full = false;
};
vector<Node> nodes;
int makeNode(){
Node node;
int id = sz(nodes);
nodes.pb(node);
return id;
}
void update(int id, int a, int b){
Node& node = nodes[id];
int res1, res2, resLeft1, resLeft2, resRight1, resRight2, full1, full2;
int cnt1, cnt2;
int m = (a + b) / 2;
cnt1 = m - a + 1;
cnt2 = b - m;
if(node.left != -1){
res1 = nodes[node.left].res;
resLeft1 = nodes[node.left].resLeft;
resRight1 = nodes[node.left].resRight;
full1 = nodes[node.left].full;
}else{
res1 = 0;
resLeft1 = resRight1 = -1;
full1 = false;
}
if(node.right != -1){
res2 = nodes[node.right].res;
resLeft2 = nodes[node.right].resLeft;
resRight2 = nodes[node.right].resRight;
full2 = nodes[node.right].full;
}else{
res2 = 0;
resLeft2 = resRight2 = -1;
full2 = false;
}
// deb("update", id);
// deb(node.left);
// deb(node.right);
// deb(res1, resLeft1, resRight1, full1);
// deb(res2, resLeft2, resRight2, full2);
node.res = res1 + res2;
node.resLeft = node.resRight = -1;
if(resRight1 != -1 && resLeft2 != -1){
node.res += resRight1 + resLeft2;
}
if(resLeft1 != -1){
node.resLeft = resLeft1;
}else if(full1 && resLeft2 != -1){
node.resLeft = cnt1 + resLeft2;
}
if(resRight2 != -1){
node.resRight = resRight2;
}else if(full2 && resRight1 != -1){
node.resRight = resRight1 + cnt2;
}
node.full = full1 && full2;
}
enum Type{
EMPTY, NORMAL, STABLE
};
void setType(int id, int pos, Type type, int a, int b){
assert(a <= pos && pos <= b);
if(a == b){
if(type == EMPTY){
nodes[id].res = 0;
nodes[id].resLeft = nodes[id].resRight = -1;
nodes[id].full = false;
}else if(type == NORMAL){
nodes[id].res = 0;
nodes[id].resLeft = nodes[id].resRight = -1;
nodes[id].full = true;
}else if(type == STABLE){
nodes[id].res = 0;
nodes[id].resLeft = nodes[id].resRight = 0;
nodes[id].full = false;
}
}else{
int m = (a + b)/2;
if(pos <= m){
if(nodes[id].left == -1)nodes[id].left = makeNode();
setType(nodes[id].left, pos, type, a, m);
update(id, a, b);
}else{
if(nodes[id].right == -1)nodes[id].right = makeNode();
setType(nodes[id].right, pos, type, m+1, b);
update(id, a, b);
}
}
}
const int N = 15'000'000;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(10);
nodes.reserve(N);
int n, m, k, q;
cin >> n >> m >> k >> q;
vector<int> diag1(n + m);
vector<int> diag2(n+ m);
const int A = 0;
const int B = n + m;
rep(i, n+ m)diag1[i] = makeNode();
rep(i, n+ m)diag2[i] = makeNode();
int total = 0;
int special = 0;
auto setPos = [&](int x, int y, Type type){
// "right" diagonals
int id = x - y + m - 1;
int pos = x + y - 2;
int old = nodes[diag1[id]].res + nodes[diag1[id + 1]].res;
setType(diag1[id], pos, type, A, B);
setType(diag1[id+1], pos, type, A, B);
special += nodes[diag1[id]].res + nodes[diag1[id + 1]].res - old;
//other diagonals
swap(id, pos);
old = nodes[diag2[id]].res + nodes[diag2[id + 1]].res;
setType(diag2[id], pos, type, A, B);
setType(diag2[id + 1], pos, type, A, B);
special += nodes[diag2[id]].res + nodes[diag2[id + 1]].res - old;
};
set<pii> bricks;
set<pii> stable;
auto stability = [&](int x, int y){
if(bricks.count({x, y}) == 0)return;
rep(i, 2)rep(j, 2){
int dx = 2*i - 1;
int dy = 2*j - 1;
if(bricks.count({x + dx, y}) && bricks.count({x, y + dy}) && bricks.count({x + dx, y + dy})){
stable.insert({x, y});
setPos(x, y, STABLE);
return;
}
}
stable.erase({x, y});
setPos(x, y, NORMAL);
};
auto toggle = [&](int x, int y){
if(bricks.count({x, y})){
// deb("removinb");
bricks.erase({x, y});
stable.erase({x, y});
total--;
setPos(x, y, EMPTY);
for(int i = -1; i <= 1; i++){
for(int j = -1; j <= 1; j++){
if(i == 0 && j == 0)continue;
stability(x + i, y + j);
}
}
}else{
// deb("adding");
bricks.insert({x, y});
total++;
for(int i = -1; i <= 1; i++){
for(int j = -1; j <= 1; j++){
stability(x + i, y + j);
}
}
}
};
rep(i, k){
int x, y; cin >> x >> y;
toggle(x, y);
}
cout << (total - stable.size() - special) << "\n";
rep(i, q){
int x, y; cin >> x >> y;
toggle(x, y);
cout << (total - stable.size() - special) << "\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 | #include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i++) #define rep(i, n) fwd(i, 0, n) #define all(X) X.begin(), X.end() #define sz(X) int(size(X)) #define pb push_back #define eb emplace_back #define st first #define nd second using pii = pair<int, int>; using vi = vector<int>; using ll = long long; using ld = long double; #ifdef LOC auto SS = signal(6, [](int) { *(int *)0 = 0; }); #define DTP(x, y) auto operator << (auto &o, auto a) -> decltype(y, o) { o << "("; x; return o << ")"; } DTP(o << a.st << ", " << a.nd, a.nd); DTP(for (auto i : a) o << i << ", ", all(a)); void dump(auto... x) { (( cerr << x << ", " ), ...) << '\n'; } #define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", dump(x) #else #define deb(...) 0 #endif struct Node{ int left = -1, right = -1, res = 0, resLeft = -1, resRight = -1; bool full = false; }; vector<Node> nodes; int makeNode(){ Node node; int id = sz(nodes); nodes.pb(node); return id; } void update(int id, int a, int b){ Node& node = nodes[id]; int res1, res2, resLeft1, resLeft2, resRight1, resRight2, full1, full2; int cnt1, cnt2; int m = (a + b) / 2; cnt1 = m - a + 1; cnt2 = b - m; if(node.left != -1){ res1 = nodes[node.left].res; resLeft1 = nodes[node.left].resLeft; resRight1 = nodes[node.left].resRight; full1 = nodes[node.left].full; }else{ res1 = 0; resLeft1 = resRight1 = -1; full1 = false; } if(node.right != -1){ res2 = nodes[node.right].res; resLeft2 = nodes[node.right].resLeft; resRight2 = nodes[node.right].resRight; full2 = nodes[node.right].full; }else{ res2 = 0; resLeft2 = resRight2 = -1; full2 = false; } // deb("update", id); // deb(node.left); // deb(node.right); // deb(res1, resLeft1, resRight1, full1); // deb(res2, resLeft2, resRight2, full2); node.res = res1 + res2; node.resLeft = node.resRight = -1; if(resRight1 != -1 && resLeft2 != -1){ node.res += resRight1 + resLeft2; } if(resLeft1 != -1){ node.resLeft = resLeft1; }else if(full1 && resLeft2 != -1){ node.resLeft = cnt1 + resLeft2; } if(resRight2 != -1){ node.resRight = resRight2; }else if(full2 && resRight1 != -1){ node.resRight = resRight1 + cnt2; } node.full = full1 && full2; } enum Type{ EMPTY, NORMAL, STABLE }; void setType(int id, int pos, Type type, int a, int b){ assert(a <= pos && pos <= b); if(a == b){ if(type == EMPTY){ nodes[id].res = 0; nodes[id].resLeft = nodes[id].resRight = -1; nodes[id].full = false; }else if(type == NORMAL){ nodes[id].res = 0; nodes[id].resLeft = nodes[id].resRight = -1; nodes[id].full = true; }else if(type == STABLE){ nodes[id].res = 0; nodes[id].resLeft = nodes[id].resRight = 0; nodes[id].full = false; } }else{ int m = (a + b)/2; if(pos <= m){ if(nodes[id].left == -1)nodes[id].left = makeNode(); setType(nodes[id].left, pos, type, a, m); update(id, a, b); }else{ if(nodes[id].right == -1)nodes[id].right = makeNode(); setType(nodes[id].right, pos, type, m+1, b); update(id, a, b); } } } const int N = 15'000'000; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); nodes.reserve(N); int n, m, k, q; cin >> n >> m >> k >> q; vector<int> diag1(n + m); vector<int> diag2(n+ m); const int A = 0; const int B = n + m; rep(i, n+ m)diag1[i] = makeNode(); rep(i, n+ m)diag2[i] = makeNode(); int total = 0; int special = 0; auto setPos = [&](int x, int y, Type type){ // "right" diagonals int id = x - y + m - 1; int pos = x + y - 2; int old = nodes[diag1[id]].res + nodes[diag1[id + 1]].res; setType(diag1[id], pos, type, A, B); setType(diag1[id+1], pos, type, A, B); special += nodes[diag1[id]].res + nodes[diag1[id + 1]].res - old; //other diagonals swap(id, pos); old = nodes[diag2[id]].res + nodes[diag2[id + 1]].res; setType(diag2[id], pos, type, A, B); setType(diag2[id + 1], pos, type, A, B); special += nodes[diag2[id]].res + nodes[diag2[id + 1]].res - old; }; set<pii> bricks; set<pii> stable; auto stability = [&](int x, int y){ if(bricks.count({x, y}) == 0)return; rep(i, 2)rep(j, 2){ int dx = 2*i - 1; int dy = 2*j - 1; if(bricks.count({x + dx, y}) && bricks.count({x, y + dy}) && bricks.count({x + dx, y + dy})){ stable.insert({x, y}); setPos(x, y, STABLE); return; } } stable.erase({x, y}); setPos(x, y, NORMAL); }; auto toggle = [&](int x, int y){ if(bricks.count({x, y})){ // deb("removinb"); bricks.erase({x, y}); stable.erase({x, y}); total--; setPos(x, y, EMPTY); for(int i = -1; i <= 1; i++){ for(int j = -1; j <= 1; j++){ if(i == 0 && j == 0)continue; stability(x + i, y + j); } } }else{ // deb("adding"); bricks.insert({x, y}); total++; for(int i = -1; i <= 1; i++){ for(int j = -1; j <= 1; j++){ stability(x + i, y + j); } } } }; rep(i, k){ int x, y; cin >> x >> y; toggle(x, y); } cout << (total - stable.size() - special) << "\n"; rep(i, q){ int x, y; cin >> x >> y; toggle(x, y); cout << (total - stable.size() - special) << "\n"; } return 0; } |
English