#include <cstdlib>
#include <cstdio>
#include <list>
#include <set>
#include <stack>
struct Punkt
{
int x, y;
bool operator<(const Punkt& q) const {
return (x < q.x) || (x == q.x && y < q.y);
}
};
struct PunktKierunek
{
int x, y;
int dx1, dy1, dx2, dy2;
};
class Plansza
{
std::set<Punkt> punkty;
std::set<Punkt> hardy;
std::set<Punkt> greje;
int x_cache_first = 0;
int y_cache_first = 0;
int x_cache_last = -1;
int y_cache_last = -1;
bool cache_data[5][5];
public:
template<class T>
Plansza(const T& lista)
: punkty(lista.begin(), lista.end())
{
for (const Punkt& p : punkty) {
if (check_hard(p.x, p.y)) {
hardy.insert(p);
}
}
std::set<Punkt> sprawdzone;
for (const Punkt& p : punkty) {
if (!hardy.count(p) && !sprawdzone.count(p)) {
update_grey_main(p, sprawdzone);
}
}
}
bool has(int x, int y) const {
if (x_cache_first <= x && x <= x_cache_last
&& y_cache_first <= y && y <= y_cache_last) {
return cache_data[x - x_cache_first][y - y_cache_first];
}
return punkty.count({x, y});
}
bool hard(int x, int y) const {
return hardy.count({x, y}) || greje.count({x, y});
}
int count_hard() {
return hardy.size() + greje.size();
}
int count_all() {
return punkty.size();
}
// true if added, false otherwise
bool toggle(int x, int y) {
auto pair = punkty.emplace(x, y);
if (!pair.second) {
punkty.erase(pair.first);
}
// dla każdego sprawdzanego punktu
// jeśli ma dwu sąsiadów i jednego po przekątnej, to jest hard 1. rodzaju
// jeśli natomiast ma dwu sąsiadów, to trzeba sprawdzić,
// czy każdy z tych sąsiadów też ma dwu sąsiadów na tym samym rogu
// i wtedy wszystkie będą hard 2. rodzaju (greje)
if (!pair.second) {
hardy.erase({x, y});
greje.erase({x, y});
}
cache(x-2, y-2);
for (int dx=-1; dx<=1; ++dx) {
for (int dy=-1; dy<=1; ++dy) {
Punkt p { x+dx, y+dy };
if (has(p.x, p.y)) {
if (check_hard(p.x, p.y)) {
hardy.insert(p);
greje.erase(p);
} else {
hardy.erase(p);
}
}
}
}
// tutaj tablica hardy jest już na pewno aktualna
// i na pewno w tablicy greje nie ma już żadnych nieistniejących punktów
// (ale mogą być istniejące punkty, których już nie powinno tam być
std::set<Punkt> sprawdzone;
for (int dx=-2; dx<=2; ++dx) {
for (int dy=-2; dy<=2; ++dy) {
Punkt p { x+dx, y+dy };
if (has(p.x, p.y) && !hardy.count(p) && !sprawdzone.count(p)) {
update_grey_main(p, sprawdzone);
}
}
}
return pair.second;
}
private:
void cache(int x0, int y0) {
for (int dx=0; dx<5; ++dx) {
for (int dy=0; dy<5; ++dy) {
cache_data[dx][dy] = punkty.count({x0+dx, y0+dy});
}
}
x_cache_first = x0;
y_cache_first = y0;
x_cache_last = x0 + 4;
y_cache_last = y0 + 4;
}
bool check_hard(int x, int y) {
bool H[3][3] = {
has(x-1, y-1), has(x, y-1), has(x+1, y-1),
has(x-1, y), false, has(x+1, y),
has(x-1, y+1), has(x, y+1), has(x+1, y+1),
};
return (H[0][0] && H[0][1] && H[1][0])
|| (H[2][0] && H[2][1] && H[1][0])
|| (H[0][2] && H[0][1] && H[1][2])
|| (H[2][2] && H[2][1] && H[1][2]);
}
void update_grey_main(const Punkt& p, std::set<Punkt>& sprawdzone) {
// tutaj sprawdzamy, czy ma dwu sąsiadów na rogu, z których każdy też ma dwu sąsiadów na rogu
bool hu = has(p.x, p.y-1);
bool hd = has(p.x, p.y+1);
bool hl = has(p.x-1, p.y);
bool hr = has(p.x+1, p.y);
if (hu + hd + hl + hr == 2) {
if (hl && hu) {
update_grey(p.x, p.y, -1, -1, sprawdzone);
} else if (hu && hr) {
update_grey(p.x, p.y, +1, -1, sprawdzone);
} else if (hr && hd) {
update_grey(p.x, p.y, +1, +1, sprawdzone);
} else if (hd && hl) {
update_grey(p.x, p.y, -1, +1, sprawdzone);
} else {
greje.erase(p);
}
} else {
greje.erase(p);
}
}
void update_grey(int x, int y, int dx, int dy, std::set<Punkt>& sprawdzone) {
std::stack<PunktKierunek> stos;
std::list<Punkt> tu_sprawdzone;
tu_sprawdzone.push_back({x, y});
sprawdzone.insert({x, y});
stos.push({x+dx, y, 0, -dy, dx, 0});
stos.push({x, y+dy, -dx, 0, 0, dy});
// będziemy wymagać, żeby ten punkt miał sąsiadów w kierunkach dx i dy
// z czego obaj muszą spełniać warunki
bool result = true;
while (!stos.empty()) {
PunktKierunek p = stos.top();
stos.pop();
if (!punkty.count({p.x, p.y})) {
result = false;
} else {
if (!hardy.count({p.x, p.y})) {
stos.push({p.x+p.dx1, p.y+p.dy1, p.dx2, p.dy2, p.dx1, p.dy1});
tu_sprawdzone.push_back({p.x, p.y});
sprawdzone.insert({p.x, p.y});
}
}
}
for (const Punkt& p : tu_sprawdzone) {
if (result) {
greje.insert(p);
} else {
greje.erase(p);
}
}
}
};
int main() {
int N, M, K, Q;
scanf("%d%d%d%d", &N, &M, &K, &Q);
std::list<Punkt> punkty;
for (int k=0; k<K; ++k) {
int x, y; scanf("%d%d", &x, &y);
punkty.push_back({x, y});
}
Plansza plansza(punkty);
int count_total = punkty.size();
printf("%d\n", count_total - plansza.count_hard());
//const int KROK = (argc > 1) ? atoi(argv[1]) : -1;
for (int q=0; q<Q; ++q) {
int x, y; scanf("%d%d", &x, &y);
if (plansza.toggle(x, y)) {
count_total++;
} else {
count_total--;
}
printf("%d\n", count_total - plansza.count_hard());
/*
if (q == KROK) {
char T[N+2][M+2];
for (int R=0; R<3; ++R) {
printf("%d/%d\n", plansza.count_hard(), count_total);
for (int x=0; x<=N+1; ++x) {
for (int y=0; y<=M+1; ++y) {
T[x][y] = !plansza.has(x, y) ? ' ' : (
plansza.hard(x, y) ? 'H' : 'x'
);
}
}
for (int y=1; y<=M; ++y) {
for (int x=1; x<=N; ++x) {
char c = T[x][y];
putchar(c);
}
putchar('\n');
}
for (int x=1; x<=N; ++x) {
for (int y=1; y<=M; ++y) {
if (plansza.has(x, y)) {
if (
(!plansza.has(x-1, y) && !plansza.has(x+1, y))
|| (!plansza.has(x, y-1) && !plansza.has(x, y+1))
) {
plansza.toggle(x, y);
count_total--;
if (T[x][y] == 'H') {
puts("ERROR! HARD REMOVED!");
}
}
}
}
}
}
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 | #include <cstdlib> #include <cstdio> #include <list> #include <set> #include <stack> struct Punkt { int x, y; bool operator<(const Punkt& q) const { return (x < q.x) || (x == q.x && y < q.y); } }; struct PunktKierunek { int x, y; int dx1, dy1, dx2, dy2; }; class Plansza { std::set<Punkt> punkty; std::set<Punkt> hardy; std::set<Punkt> greje; int x_cache_first = 0; int y_cache_first = 0; int x_cache_last = -1; int y_cache_last = -1; bool cache_data[5][5]; public: template<class T> Plansza(const T& lista) : punkty(lista.begin(), lista.end()) { for (const Punkt& p : punkty) { if (check_hard(p.x, p.y)) { hardy.insert(p); } } std::set<Punkt> sprawdzone; for (const Punkt& p : punkty) { if (!hardy.count(p) && !sprawdzone.count(p)) { update_grey_main(p, sprawdzone); } } } bool has(int x, int y) const { if (x_cache_first <= x && x <= x_cache_last && y_cache_first <= y && y <= y_cache_last) { return cache_data[x - x_cache_first][y - y_cache_first]; } return punkty.count({x, y}); } bool hard(int x, int y) const { return hardy.count({x, y}) || greje.count({x, y}); } int count_hard() { return hardy.size() + greje.size(); } int count_all() { return punkty.size(); } // true if added, false otherwise bool toggle(int x, int y) { auto pair = punkty.emplace(x, y); if (!pair.second) { punkty.erase(pair.first); } // dla każdego sprawdzanego punktu // jeśli ma dwu sąsiadów i jednego po przekątnej, to jest hard 1. rodzaju // jeśli natomiast ma dwu sąsiadów, to trzeba sprawdzić, // czy każdy z tych sąsiadów też ma dwu sąsiadów na tym samym rogu // i wtedy wszystkie będą hard 2. rodzaju (greje) if (!pair.second) { hardy.erase({x, y}); greje.erase({x, y}); } cache(x-2, y-2); for (int dx=-1; dx<=1; ++dx) { for (int dy=-1; dy<=1; ++dy) { Punkt p { x+dx, y+dy }; if (has(p.x, p.y)) { if (check_hard(p.x, p.y)) { hardy.insert(p); greje.erase(p); } else { hardy.erase(p); } } } } // tutaj tablica hardy jest już na pewno aktualna // i na pewno w tablicy greje nie ma już żadnych nieistniejących punktów // (ale mogą być istniejące punkty, których już nie powinno tam być std::set<Punkt> sprawdzone; for (int dx=-2; dx<=2; ++dx) { for (int dy=-2; dy<=2; ++dy) { Punkt p { x+dx, y+dy }; if (has(p.x, p.y) && !hardy.count(p) && !sprawdzone.count(p)) { update_grey_main(p, sprawdzone); } } } return pair.second; } private: void cache(int x0, int y0) { for (int dx=0; dx<5; ++dx) { for (int dy=0; dy<5; ++dy) { cache_data[dx][dy] = punkty.count({x0+dx, y0+dy}); } } x_cache_first = x0; y_cache_first = y0; x_cache_last = x0 + 4; y_cache_last = y0 + 4; } bool check_hard(int x, int y) { bool H[3][3] = { has(x-1, y-1), has(x, y-1), has(x+1, y-1), has(x-1, y), false, has(x+1, y), has(x-1, y+1), has(x, y+1), has(x+1, y+1), }; return (H[0][0] && H[0][1] && H[1][0]) || (H[2][0] && H[2][1] && H[1][0]) || (H[0][2] && H[0][1] && H[1][2]) || (H[2][2] && H[2][1] && H[1][2]); } void update_grey_main(const Punkt& p, std::set<Punkt>& sprawdzone) { // tutaj sprawdzamy, czy ma dwu sąsiadów na rogu, z których każdy też ma dwu sąsiadów na rogu bool hu = has(p.x, p.y-1); bool hd = has(p.x, p.y+1); bool hl = has(p.x-1, p.y); bool hr = has(p.x+1, p.y); if (hu + hd + hl + hr == 2) { if (hl && hu) { update_grey(p.x, p.y, -1, -1, sprawdzone); } else if (hu && hr) { update_grey(p.x, p.y, +1, -1, sprawdzone); } else if (hr && hd) { update_grey(p.x, p.y, +1, +1, sprawdzone); } else if (hd && hl) { update_grey(p.x, p.y, -1, +1, sprawdzone); } else { greje.erase(p); } } else { greje.erase(p); } } void update_grey(int x, int y, int dx, int dy, std::set<Punkt>& sprawdzone) { std::stack<PunktKierunek> stos; std::list<Punkt> tu_sprawdzone; tu_sprawdzone.push_back({x, y}); sprawdzone.insert({x, y}); stos.push({x+dx, y, 0, -dy, dx, 0}); stos.push({x, y+dy, -dx, 0, 0, dy}); // będziemy wymagać, żeby ten punkt miał sąsiadów w kierunkach dx i dy // z czego obaj muszą spełniać warunki bool result = true; while (!stos.empty()) { PunktKierunek p = stos.top(); stos.pop(); if (!punkty.count({p.x, p.y})) { result = false; } else { if (!hardy.count({p.x, p.y})) { stos.push({p.x+p.dx1, p.y+p.dy1, p.dx2, p.dy2, p.dx1, p.dy1}); tu_sprawdzone.push_back({p.x, p.y}); sprawdzone.insert({p.x, p.y}); } } } for (const Punkt& p : tu_sprawdzone) { if (result) { greje.insert(p); } else { greje.erase(p); } } } }; int main() { int N, M, K, Q; scanf("%d%d%d%d", &N, &M, &K, &Q); std::list<Punkt> punkty; for (int k=0; k<K; ++k) { int x, y; scanf("%d%d", &x, &y); punkty.push_back({x, y}); } Plansza plansza(punkty); int count_total = punkty.size(); printf("%d\n", count_total - plansza.count_hard()); //const int KROK = (argc > 1) ? atoi(argv[1]) : -1; for (int q=0; q<Q; ++q) { int x, y; scanf("%d%d", &x, &y); if (plansza.toggle(x, y)) { count_total++; } else { count_total--; } printf("%d\n", count_total - plansza.count_hard()); /* if (q == KROK) { char T[N+2][M+2]; for (int R=0; R<3; ++R) { printf("%d/%d\n", plansza.count_hard(), count_total); for (int x=0; x<=N+1; ++x) { for (int y=0; y<=M+1; ++y) { T[x][y] = !plansza.has(x, y) ? ' ' : ( plansza.hard(x, y) ? 'H' : 'x' ); } } for (int y=1; y<=M; ++y) { for (int x=1; x<=N; ++x) { char c = T[x][y]; putchar(c); } putchar('\n'); } for (int x=1; x<=N; ++x) { for (int y=1; y<=M; ++y) { if (plansza.has(x, y)) { if ( (!plansza.has(x-1, y) && !plansza.has(x+1, y)) || (!plansza.has(x, y-1) && !plansza.has(x, y+1)) ) { plansza.toggle(x, y); count_total--; if (T[x][y] == 'H') { puts("ERROR! HARD REMOVED!"); } } } } } } return 0; } */ } } |
English