#include <cstdio>
#include <set>
#include <queue>
using namespace std;
void printp(long long i) {
int x = (int)(i >> 32);
int y = (int)i;
printf("%d %d\n", x, y);
}
int main() {
int n, m, k, q;
set<long long> og_board;
set<long long> board;
scanf("%d%d%d%d", &n, &m, &k, &q);
for (int i = 0; i < k; i++) {
long long x, y;
scanf("%lld%lld", &x, &y);
og_board.insert((x << 32) + y);
}
for (int i = 0; i <= q; i++) {
if (i != 0) {
long long x, y;
scanf("%lld%lld", &x, &y);
long long v = (x << 32) + y;
if (og_board.count(v) == 0) {
og_board.insert(v);
k++;
}
else {
og_board.erase(v);
k--;
}
}
board = og_board;
set<long long>::iterator it = board.begin();
queue<long long> to_check;
while (it != board.end()) {
//printf("Entered "); printp(*it);
to_check.push(*it);
it++;
while (!to_check.empty()) {
long long cc = to_check.front();
to_check.pop();
//printf("started "); printp(cc);
if (board.count(cc) == 0) continue;
//printf("resolved "); printp(cc);
if ((board.count(cc+1) == 0 && board.count(cc-1) == 0) || (board.count(cc+(1L << 32)) == 0 && board.count(cc-(1L << 32)) == 0)) {
if (*it == cc) it++;
board.erase(cc);
//printf("erased "); printp(cc);
if (board.count(cc+1) != 0) {
to_check.push(cc+1);
//printf("queued "); printp(cc+1);
}
if (board.count(cc-1) != 0) {
to_check.push(cc-1);
//printf("queued "); printp(cc-1);
}
if (board.count(cc+(1L<<32)) != 0) {
to_check.push(cc+(1L<<32));
//printf("queued "); printp(cc+(1L<<32));
}
if (board.count(cc-(1L<<32)) != 0) {
to_check.push(cc-(1L<<32));
//printf("queued "); printp(cc-(1L<<32));
}
}
}
}
int r = k - board.size();
printf("%d\n", r);
}
}
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 | #include <cstdio> #include <set> #include <queue> using namespace std; void printp(long long i) { int x = (int)(i >> 32); int y = (int)i; printf("%d %d\n", x, y); } int main() { int n, m, k, q; set<long long> og_board; set<long long> board; scanf("%d%d%d%d", &n, &m, &k, &q); for (int i = 0; i < k; i++) { long long x, y; scanf("%lld%lld", &x, &y); og_board.insert((x << 32) + y); } for (int i = 0; i <= q; i++) { if (i != 0) { long long x, y; scanf("%lld%lld", &x, &y); long long v = (x << 32) + y; if (og_board.count(v) == 0) { og_board.insert(v); k++; } else { og_board.erase(v); k--; } } board = og_board; set<long long>::iterator it = board.begin(); queue<long long> to_check; while (it != board.end()) { //printf("Entered "); printp(*it); to_check.push(*it); it++; while (!to_check.empty()) { long long cc = to_check.front(); to_check.pop(); //printf("started "); printp(cc); if (board.count(cc) == 0) continue; //printf("resolved "); printp(cc); if ((board.count(cc+1) == 0 && board.count(cc-1) == 0) || (board.count(cc+(1L << 32)) == 0 && board.count(cc-(1L << 32)) == 0)) { if (*it == cc) it++; board.erase(cc); //printf("erased "); printp(cc); if (board.count(cc+1) != 0) { to_check.push(cc+1); //printf("queued "); printp(cc+1); } if (board.count(cc-1) != 0) { to_check.push(cc-1); //printf("queued "); printp(cc-1); } if (board.count(cc+(1L<<32)) != 0) { to_check.push(cc+(1L<<32)); //printf("queued "); printp(cc+(1L<<32)); } if (board.count(cc-(1L<<32)) != 0) { to_check.push(cc-(1L<<32)); //printf("queued "); printp(cc-(1L<<32)); } } } } int r = k - board.size(); printf("%d\n", r); } } |
English