#include <bits/stdc++.h> using namespace std; struct Block { int x, y; }; struct Node { int left, right, up, down; }; const int MAX_BLOCKS = 75002 * 2; const int MAX_WIDTH_HEIGHT = 200002; int height, width; Block blocks[MAX_BLOCKS]; int blocks_cnt; unordered_map<int, int> block_idx_by_yx[MAX_WIDTH_HEIGHT]; Node graph[MAX_BLOCKS]; bool removed[MAX_BLOCKS]; unordered_set<int> starting_blocks; bool idx_discarded[MAX_BLOCKS]; vector<int> discarded_idxs; bool can_remove(int idx) { int left = removed[graph[idx].left] ? -1 : graph[idx].left; int right = removed[graph[idx].right] ? -1 : graph[idx].right; int up = removed[graph[idx].up] ? -1 : graph[idx].up; int down = removed[graph[idx].down] ? -1 : graph[idx].down; return (left == -1 && right == -1) || (up == -1 && down == -1); } int que[MAX_BLOCKS]; int que_size; int bfs(int idx) { int curr_idx, neighbor_idx, i; int neighbors[4]; int removed_cnt = 0; que_size = 0; que[que_size++] = idx; while (que_size > 0) { curr_idx = que[--que_size]; if (removed[curr_idx]) { continue; } removed[curr_idx] = true; ++removed_cnt; neighbors[0] = graph[curr_idx].left; neighbors[1] = graph[curr_idx].right, neighbors[2] = graph[curr_idx].up; neighbors[3] = graph[curr_idx].down; for (i = 0; i < 4; ++i) { neighbor_idx = neighbors[i]; if (neighbor_idx == -1 || removed[neighbor_idx]) { continue; } if (can_remove(neighbor_idx)) { que[que_size++] = neighbor_idx; } } } return removed_cnt; } int calc() { int removed_cnt = 0; for (int starting_block : starting_blocks) { removed_cnt += bfs(starting_block); } return removed_cnt; } void update_block(int i) { int y = blocks[i].y; int x = blocks[i].x; graph[i].left = block_idx_by_yx[y].count(x - 1) ? block_idx_by_yx[y][x - 1] : -1; graph[i].right = block_idx_by_yx[y].count(x + 1) ? block_idx_by_yx[y][x + 1] : -1; graph[i].up = block_idx_by_yx[y - 1].count(x) ? block_idx_by_yx[y - 1][x] : -1; graph[i].down = block_idx_by_yx[y + 1].count(x) ? block_idx_by_yx[y + 1][x] : -1; if (can_remove(i) && !idx_discarded[i]) { starting_blocks.insert(i); } else { starting_blocks.erase(i); } } void update_blocks() { starting_blocks.clear(); for (int i = 0; i < blocks_cnt; ++i) { if (idx_discarded[i]) continue; update_block(i); } } void update_starting_block(int y, int x) { if (!block_idx_by_yx[y].count(x)) { return; } int idx = block_idx_by_yx[y][x]; update_block(idx); } void process_query(int y, int x) { int idx; if (!block_idx_by_yx[y].count(x)) { if (!discarded_idxs.empty()) { idx = discarded_idxs.back(); discarded_idxs.pop_back(); idx_discarded[idx] = false; } else { idx = blocks_cnt++; } blocks[idx].x = x; blocks[idx].y = y; block_idx_by_yx[y][x] = idx; } else { idx = block_idx_by_yx[y][x]; discarded_idxs.push_back(idx); idx_discarded[idx] = true; block_idx_by_yx[y].erase(x); } update_block(idx); update_starting_block(y, x - 1); update_starting_block(y, x + 1); update_starting_block(y - 1, x); update_starting_block(y + 1, x); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int queries_cnt; int x, y; int i; cin >> height >> width >> blocks_cnt >> queries_cnt; for (i = 0; i < blocks_cnt; ++i) { cin >> blocks[i].y >> blocks[i].x; block_idx_by_yx[blocks[i].y][blocks[i].x] = i; } for (i = 0; i < blocks_cnt; ++i) { update_block(i); } int res = calc(); cout << res << "\n"; while (queries_cnt--) { memset(removed, 0, blocks_cnt); cin >> y >> x; process_query(y, x); res = calc(); cout << res << "\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 207 208 | #include <bits/stdc++.h> using namespace std; struct Block { int x, y; }; struct Node { int left, right, up, down; }; const int MAX_BLOCKS = 75002 * 2; const int MAX_WIDTH_HEIGHT = 200002; int height, width; Block blocks[MAX_BLOCKS]; int blocks_cnt; unordered_map<int, int> block_idx_by_yx[MAX_WIDTH_HEIGHT]; Node graph[MAX_BLOCKS]; bool removed[MAX_BLOCKS]; unordered_set<int> starting_blocks; bool idx_discarded[MAX_BLOCKS]; vector<int> discarded_idxs; bool can_remove(int idx) { int left = removed[graph[idx].left] ? -1 : graph[idx].left; int right = removed[graph[idx].right] ? -1 : graph[idx].right; int up = removed[graph[idx].up] ? -1 : graph[idx].up; int down = removed[graph[idx].down] ? -1 : graph[idx].down; return (left == -1 && right == -1) || (up == -1 && down == -1); } int que[MAX_BLOCKS]; int que_size; int bfs(int idx) { int curr_idx, neighbor_idx, i; int neighbors[4]; int removed_cnt = 0; que_size = 0; que[que_size++] = idx; while (que_size > 0) { curr_idx = que[--que_size]; if (removed[curr_idx]) { continue; } removed[curr_idx] = true; ++removed_cnt; neighbors[0] = graph[curr_idx].left; neighbors[1] = graph[curr_idx].right, neighbors[2] = graph[curr_idx].up; neighbors[3] = graph[curr_idx].down; for (i = 0; i < 4; ++i) { neighbor_idx = neighbors[i]; if (neighbor_idx == -1 || removed[neighbor_idx]) { continue; } if (can_remove(neighbor_idx)) { que[que_size++] = neighbor_idx; } } } return removed_cnt; } int calc() { int removed_cnt = 0; for (int starting_block : starting_blocks) { removed_cnt += bfs(starting_block); } return removed_cnt; } void update_block(int i) { int y = blocks[i].y; int x = blocks[i].x; graph[i].left = block_idx_by_yx[y].count(x - 1) ? block_idx_by_yx[y][x - 1] : -1; graph[i].right = block_idx_by_yx[y].count(x + 1) ? block_idx_by_yx[y][x + 1] : -1; graph[i].up = block_idx_by_yx[y - 1].count(x) ? block_idx_by_yx[y - 1][x] : -1; graph[i].down = block_idx_by_yx[y + 1].count(x) ? block_idx_by_yx[y + 1][x] : -1; if (can_remove(i) && !idx_discarded[i]) { starting_blocks.insert(i); } else { starting_blocks.erase(i); } } void update_blocks() { starting_blocks.clear(); for (int i = 0; i < blocks_cnt; ++i) { if (idx_discarded[i]) continue; update_block(i); } } void update_starting_block(int y, int x) { if (!block_idx_by_yx[y].count(x)) { return; } int idx = block_idx_by_yx[y][x]; update_block(idx); } void process_query(int y, int x) { int idx; if (!block_idx_by_yx[y].count(x)) { if (!discarded_idxs.empty()) { idx = discarded_idxs.back(); discarded_idxs.pop_back(); idx_discarded[idx] = false; } else { idx = blocks_cnt++; } blocks[idx].x = x; blocks[idx].y = y; block_idx_by_yx[y][x] = idx; } else { idx = block_idx_by_yx[y][x]; discarded_idxs.push_back(idx); idx_discarded[idx] = true; block_idx_by_yx[y].erase(x); } update_block(idx); update_starting_block(y, x - 1); update_starting_block(y, x + 1); update_starting_block(y - 1, x); update_starting_block(y + 1, x); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int queries_cnt; int x, y; int i; cin >> height >> width >> blocks_cnt >> queries_cnt; for (i = 0; i < blocks_cnt; ++i) { cin >> blocks[i].y >> blocks[i].x; block_idx_by_yx[blocks[i].y][blocks[i].x] = i; } for (i = 0; i < blocks_cnt; ++i) { update_block(i); } int res = calc(); cout << res << "\n"; while (queries_cnt--) { memset(removed, 0, blocks_cnt); cin >> y >> x; process_query(y, x); res = calc(); cout << res << "\n"; } return 0; } |