#include <cstdio> #include <set> #include <vector> #include <map> #include <queue> #define DEBUG false using namespace std; int comp_iteration = 0; int total_zdejmovables = 0; queue<int> klocklejka; struct klocek { int id; int neighbors[4]; // up - down - left - right int zdejmovability; // up + 2down + 4left + 8right int iteration_seen; int iteration_visited; bool removed; }; bool zdejmovable(int zity) { int updown = zity % 4; int leftright = zity % 16; return updown == 0 || leftright < 4; } vector<klocek> klockis; map<pair<int, int>, int> klockids; int get_klocek_id(int x, int y) { if (x < 0) return -1; if (y < 0) return -1; auto val = klockids.find(make_pair(x, y)); if (val == klockids.end()) return -1; return val->second; } void renew_zdejmovability(int klocek_id) { if constexpr (DEBUG) printf("Renewing %d\n", klocek_id); int zdejm = 0; if (klockis[klocek_id].neighbors[0] != -1) { if constexpr (DEBUG) printf("Has up neb\n"); zdejm += 1; } if (klockis[klocek_id].neighbors[1] != -1) { if constexpr (DEBUG) printf("Has down neb\n"); zdejm += 2; } if (klockis[klocek_id].neighbors[2] != -1) { if constexpr (DEBUG) printf("Has left neb\n"); zdejm += 4; } if (klockis[klocek_id].neighbors[3] != -1) { if constexpr (DEBUG) printf("Has right neb\n"); zdejm += 8; } klockis[klocek_id].zdejmovability = zdejm; if constexpr (DEBUG) printf("Renewed to %d zity\n", zdejm); } void compute_zdejmovables() { while (!klocklejka.empty()) { int klocek_id = klocklejka.front(); klocklejka.pop(); if constexpr (DEBUG) printf("Checking %d (current_zity: %2d, last_visited: %2d))\n", klocek_id, klockis[klocek_id].zdejmovability, klockis[klocek_id].iteration_visited); if (zdejmovable(klockis[klocek_id].zdejmovability) && klockis[klocek_id].iteration_visited < comp_iteration) { if constexpr (DEBUG) printf("Zdejming %d!\n", klocek_id); klockis[klocek_id].iteration_visited = comp_iteration; for (int i = 0; i < 4; ++i) { int nid = klockis[klocek_id].neighbors[i]; if (nid != 1) { if constexpr (DEBUG) printf("Processing neighbor of %d : %d\n", klocek_id, nid); if (klockis[nid].iteration_seen < comp_iteration) { klockis[nid].iteration_seen = comp_iteration; renew_zdejmovability(nid); } int zity_minus = 0; switch (i) { case 0: zity_minus = 2; break; case 1: zity_minus = 1; break; case 2: zity_minus = 8; break; case 3: zity_minus = 4; break; } klockis[nid].zdejmovability -= zity_minus; } } total_zdejmovables += 1; for (int i = 0; i < 4; ++i) { int nid = klockis[klocek_id].neighbors[i]; if (nid != -1) klocklejka.push(nid); } } } } void start_compute_zdejmovables() { // compute ab ovo, based only on klocek placements klocklejka = queue<int>(); total_zdejmovables = 0; if constexpr (DEBUG) printf("======================================\n"); if constexpr (DEBUG) printf("ITERATION %d\n", comp_iteration); for (int i = 0; i < klockis.size(); ++i) { if (klockis[i].removed) continue; if (klockis[i].iteration_seen < comp_iteration) { klockis[i].iteration_seen = comp_iteration; renew_zdejmovability(i); } klocklejka.push(i); compute_zdejmovables(); } printf("%d\n", total_zdejmovables); } // przerabiamy na graf, wstawienie/usunięcie klocka dodaje/usuwa krawędzie (max 4) // przechodzimy DFSem po tym grafie żeby policzyć zdejmovability int main() { int n, m, k, q; scanf("%d %d %d %d", &n, &m, &k, &q); int klocek_id = 0; for (int i = 0; i < k; ++i) { int x, y; scanf("%d %d", &x, &y); klockids[make_pair(x, y)] = klocek_id; int upneigh = get_klocek_id(x, y-1); int downeigh = get_klocek_id(x, y+1); int leftneigh = get_klocek_id(x-1, y); int rightneigh = get_klocek_id(x+1, y); klockis.push_back(klocek{klocek_id, {upneigh, downeigh, leftneigh, rightneigh}, 0, -1, -1, false}); if constexpr (DEBUG) printf("Creating klocek %d, neighbor ids: %d | %d | %d | %d\n", klocek_id, upneigh, downeigh, leftneigh, rightneigh); if (upneigh != -1) { klockis[upneigh].neighbors[1] = klocek_id; } if (downeigh != -1) { klockis[downeigh].neighbors[0] = klocek_id; } if (leftneigh != -1) { klockis[leftneigh].neighbors[3] = klocek_id; } if (rightneigh != -1) { klockis[rightneigh].neighbors[2] = klocek_id; } klocek_id += 1; } start_compute_zdejmovables(); for (int i = 0; i < q; ++i) { comp_iteration++; int x, y; scanf("%d %d", &x, &y); int id = get_klocek_id(x, y); int upneigh = get_klocek_id(x, y-1); int downeigh = get_klocek_id(x, y+1); int leftneigh = get_klocek_id(x-1, y); int rightneigh = get_klocek_id(x+1, y); if (id == -1) { // add klocek klockis.push_back(klocek{klocek_id, {upneigh, downeigh, leftneigh, rightneigh}, 0, -1, -1, false}); if (upneigh != -1) { klockis[upneigh].neighbors[1] = klocek_id; } if (downeigh != -1) { klockis[downeigh].neighbors[0] = klocek_id; } if (leftneigh != -1) { klockis[leftneigh].neighbors[3] = klocek_id; } if (rightneigh != -1) { klockis[rightneigh].neighbors[2] = klocek_id; } } else { // remove klocek klockis[id].removed = true; if (upneigh != -1) { klockis[upneigh].neighbors[1] = -1; } if (downeigh != -1) { klockis[downeigh].neighbors[0] = -1; } if (leftneigh != -1) { klockis[leftneigh].neighbors[3] = -1; } if (rightneigh != -1) { klockis[rightneigh].neighbors[2] = -1; } } start_compute_zdejmovables(); } 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 | #include <cstdio> #include <set> #include <vector> #include <map> #include <queue> #define DEBUG false using namespace std; int comp_iteration = 0; int total_zdejmovables = 0; queue<int> klocklejka; struct klocek { int id; int neighbors[4]; // up - down - left - right int zdejmovability; // up + 2down + 4left + 8right int iteration_seen; int iteration_visited; bool removed; }; bool zdejmovable(int zity) { int updown = zity % 4; int leftright = zity % 16; return updown == 0 || leftright < 4; } vector<klocek> klockis; map<pair<int, int>, int> klockids; int get_klocek_id(int x, int y) { if (x < 0) return -1; if (y < 0) return -1; auto val = klockids.find(make_pair(x, y)); if (val == klockids.end()) return -1; return val->second; } void renew_zdejmovability(int klocek_id) { if constexpr (DEBUG) printf("Renewing %d\n", klocek_id); int zdejm = 0; if (klockis[klocek_id].neighbors[0] != -1) { if constexpr (DEBUG) printf("Has up neb\n"); zdejm += 1; } if (klockis[klocek_id].neighbors[1] != -1) { if constexpr (DEBUG) printf("Has down neb\n"); zdejm += 2; } if (klockis[klocek_id].neighbors[2] != -1) { if constexpr (DEBUG) printf("Has left neb\n"); zdejm += 4; } if (klockis[klocek_id].neighbors[3] != -1) { if constexpr (DEBUG) printf("Has right neb\n"); zdejm += 8; } klockis[klocek_id].zdejmovability = zdejm; if constexpr (DEBUG) printf("Renewed to %d zity\n", zdejm); } void compute_zdejmovables() { while (!klocklejka.empty()) { int klocek_id = klocklejka.front(); klocklejka.pop(); if constexpr (DEBUG) printf("Checking %d (current_zity: %2d, last_visited: %2d))\n", klocek_id, klockis[klocek_id].zdejmovability, klockis[klocek_id].iteration_visited); if (zdejmovable(klockis[klocek_id].zdejmovability) && klockis[klocek_id].iteration_visited < comp_iteration) { if constexpr (DEBUG) printf("Zdejming %d!\n", klocek_id); klockis[klocek_id].iteration_visited = comp_iteration; for (int i = 0; i < 4; ++i) { int nid = klockis[klocek_id].neighbors[i]; if (nid != 1) { if constexpr (DEBUG) printf("Processing neighbor of %d : %d\n", klocek_id, nid); if (klockis[nid].iteration_seen < comp_iteration) { klockis[nid].iteration_seen = comp_iteration; renew_zdejmovability(nid); } int zity_minus = 0; switch (i) { case 0: zity_minus = 2; break; case 1: zity_minus = 1; break; case 2: zity_minus = 8; break; case 3: zity_minus = 4; break; } klockis[nid].zdejmovability -= zity_minus; } } total_zdejmovables += 1; for (int i = 0; i < 4; ++i) { int nid = klockis[klocek_id].neighbors[i]; if (nid != -1) klocklejka.push(nid); } } } } void start_compute_zdejmovables() { // compute ab ovo, based only on klocek placements klocklejka = queue<int>(); total_zdejmovables = 0; if constexpr (DEBUG) printf("======================================\n"); if constexpr (DEBUG) printf("ITERATION %d\n", comp_iteration); for (int i = 0; i < klockis.size(); ++i) { if (klockis[i].removed) continue; if (klockis[i].iteration_seen < comp_iteration) { klockis[i].iteration_seen = comp_iteration; renew_zdejmovability(i); } klocklejka.push(i); compute_zdejmovables(); } printf("%d\n", total_zdejmovables); } // przerabiamy na graf, wstawienie/usunięcie klocka dodaje/usuwa krawędzie (max 4) // przechodzimy DFSem po tym grafie żeby policzyć zdejmovability int main() { int n, m, k, q; scanf("%d %d %d %d", &n, &m, &k, &q); int klocek_id = 0; for (int i = 0; i < k; ++i) { int x, y; scanf("%d %d", &x, &y); klockids[make_pair(x, y)] = klocek_id; int upneigh = get_klocek_id(x, y-1); int downeigh = get_klocek_id(x, y+1); int leftneigh = get_klocek_id(x-1, y); int rightneigh = get_klocek_id(x+1, y); klockis.push_back(klocek{klocek_id, {upneigh, downeigh, leftneigh, rightneigh}, 0, -1, -1, false}); if constexpr (DEBUG) printf("Creating klocek %d, neighbor ids: %d | %d | %d | %d\n", klocek_id, upneigh, downeigh, leftneigh, rightneigh); if (upneigh != -1) { klockis[upneigh].neighbors[1] = klocek_id; } if (downeigh != -1) { klockis[downeigh].neighbors[0] = klocek_id; } if (leftneigh != -1) { klockis[leftneigh].neighbors[3] = klocek_id; } if (rightneigh != -1) { klockis[rightneigh].neighbors[2] = klocek_id; } klocek_id += 1; } start_compute_zdejmovables(); for (int i = 0; i < q; ++i) { comp_iteration++; int x, y; scanf("%d %d", &x, &y); int id = get_klocek_id(x, y); int upneigh = get_klocek_id(x, y-1); int downeigh = get_klocek_id(x, y+1); int leftneigh = get_klocek_id(x-1, y); int rightneigh = get_klocek_id(x+1, y); if (id == -1) { // add klocek klockis.push_back(klocek{klocek_id, {upneigh, downeigh, leftneigh, rightneigh}, 0, -1, -1, false}); if (upneigh != -1) { klockis[upneigh].neighbors[1] = klocek_id; } if (downeigh != -1) { klockis[downeigh].neighbors[0] = klocek_id; } if (leftneigh != -1) { klockis[leftneigh].neighbors[3] = klocek_id; } if (rightneigh != -1) { klockis[rightneigh].neighbors[2] = klocek_id; } } else { // remove klocek klockis[id].removed = true; if (upneigh != -1) { klockis[upneigh].neighbors[1] = -1; } if (downeigh != -1) { klockis[downeigh].neighbors[0] = -1; } if (leftneigh != -1) { klockis[leftneigh].neighbors[3] = -1; } if (rightneigh != -1) { klockis[rightneigh].neighbors[2] = -1; } } start_compute_zdejmovables(); } return 0; } |