#include <iostream> #include <iomanip> #include <string> #include <algorithm> #include <map> #include <set> #include <vector> using namespace std; //#define DEBUG(x) x //#define CALC_TIME #ifndef DEBUG #define DEBUG(x) #endif #define REP(x,n) for(int x=0;x<(n);++x) #define VAR(x,n) __typeof(n) x = (n) #define FOREACH(x,c) for(VAR(x, (c).begin()); x != (c).end(); ++x) #define CONTAINS(x,elem) ((x).find(elem) != (x).end()) struct Item { int x; int y; bool visible; Item *left=nullptr, *right=nullptr, *top=nullptr, *bottom=nullptr; int hideOrder=10000000; // Item(){} // Item(int x, int y, bool visible): x(x), y(y), visible(visible), // left(nullptr), right(nullptr), top(nullptr), bottom(nullptr), hideOrder(-1) {} }; ostream& operator<<(ostream& os, const Item& item) { return os << "[" << item.x<<","<<item.y<<"]/"<<item.visible<<"/"<<item.hideOrder; } ostream& operator<<(ostream& os, const Item* item) { if (item) { return os << *item; } else { return os << "null"; } } enum Direction { HORIZONTAL, VERTICAL, BOTH }; map<int,map<int,Item>> items; map<int,set<int>> revIndices; bool inline hasLeftNeighbour(const Item& item) { // DEBUG(cerr<<"hasLeftNeighbour -> "<<item.left<<endl;) return item.left != nullptr && item.left->y == item.y-1; } bool inline hasRightNeighbour(const Item& item) { // DEBUG(cerr<<"hasRightNeighbour -> "<<item.right<<endl;) return item.right != nullptr && item.right->y == item.y+1; } bool inline hasTopNeighbour(const Item& item) { // DEBUG(cerr<<"hasTopRightNeighbour -> "<<item.top<<endl;) return item.top != nullptr && item.top->x == item.x-1; } bool inline hasBottomNeighbour(const Item& item) { // DEBUG(cerr<<"hasBottomNeighbour -> "<<item.bottom<<endl;) return item.bottom != nullptr && item.bottom->x == item.x+1; } bool inline hasFreeEdges(const Item& item, Direction direction) { switch (direction) { case HORIZONTAL: return (!hasLeftNeighbour(item) || !item.left->visible) && (!hasRightNeighbour(item) || !item.right->visible); case VERTICAL: return (!hasTopNeighbour(item) || !item.top->visible) && (!hasBottomNeighbour(item) || !item.bottom->visible); case BOTH: return ( (!hasLeftNeighbour(item) || !item.left->visible) && (!hasRightNeighbour(item) || !item.right->visible) ) || ( (!hasTopNeighbour(item) || !item.top->visible) && (!hasBottomNeighbour(item) || !item.bottom->visible) ); } return false; } int inline getHideOrderIndex(const Item& item) { int horizontalIndex = max( hasLeftNeighbour(item) ? item.left->hideOrder + 1 : 0, hasRightNeighbour(item) ? item.right->hideOrder + 1 : 0 ); int verticalIndex = max( hasBottomNeighbour(item) ? item.bottom->hideOrder + 1 : 0, hasTopNeighbour(item) ? item.top->hideOrder + 1 : 0 ); return min(horizontalIndex, verticalIndex); } void restoreWithChildren(Item& root, vector<Item*>& result) { if (root.visible) { return; } DEBUG(cerr<<"restoring "<<root<<endl;) result.push_back(&root); root.visible = true; DEBUG(cerr<<" check restore left "<<root.left<<endl;) if (root.left && root.left->hideOrder > root.hideOrder) restoreWithChildren(*root.left, result); DEBUG(cerr<<" check restore right "<<root.right<<endl;) if (root.right && root.right->hideOrder > root.hideOrder) restoreWithChildren(*root.right, result); DEBUG(cerr<<" check restore top "<<root.top<<endl;) if (root.top && root.top->hideOrder > root.hideOrder) restoreWithChildren(*root.top, result); DEBUG(cerr<<" check restore bottom "<<root.bottom<<endl;) if (root.bottom && root.bottom->hideOrder > root.hideOrder) restoreWithChildren(*root.bottom, result); root.hideOrder = 10000000; } vector<Item*> restoreVisibility(const Item& root) { vector<Item*> result; if (root.left) restoreWithChildren(*root.left, result); if (root.right) restoreWithChildren(*root.right, result); if (root.top) restoreWithChildren(*root.top, result); if (root.bottom) restoreWithChildren(*root.bottom, result); return result; } void calcNeighbourhood() { for (auto& row : items) { Item* previous = nullptr; for(auto& item : row.second) { if(previous) { item.second.left = previous; previous->right = &item.second; } auto& column = revIndices[item.second.y]; auto findItemInColumn = column.find(item.second.x); if (findItemInColumn != column.begin()) { item.second.top = &items[*prev(findItemInColumn)][item.second.y]; item.second.top->bottom = &item.second; } previous = &item.second; } } } int tryRecursiveHide(Item& item); int doHide(Item& item, Direction direction) { item.hideOrder = item.visible ? getHideOrderIndex(item) : item.hideOrder; DEBUG( cerr << "hide brick "<<item<<" - "<<direction<<endl; cerr << " left: "<<item.left<<endl; cerr << " right: "<<item.right<<endl; cerr << " top: "<<item.top<<endl; cerr << " bottom: "<<item.bottom<<endl; ) item.visible = false; int result = 0; switch (direction) { case HORIZONTAL: if (item.top) result += tryRecursiveHide(*item.top); if (item.bottom) result += tryRecursiveHide(*item.bottom); break; case VERTICAL: if (item.left) result += tryRecursiveHide(*item.left); if (item.right) result += tryRecursiveHide(*item.right); break; case BOTH: if (item.top) result += tryRecursiveHide(*item.top); if (item.bottom) result += tryRecursiveHide(*item.bottom); if (item.left) result += tryRecursiveHide(*item.left); if (item.right) result += tryRecursiveHide(*item.right); break; } return result; } int tryRecursiveHide(Item& item) { if (!item.visible) return 0; //DEBUG(cerr << "try recursive hide ["<<item.x<<","<<item.y<<"]"<<endl;) if (hasFreeEdges(item, HORIZONTAL)) { return 1 + doHide(item, HORIZONTAL); } else if (hasFreeEdges(item, VERTICAL)) { return 1 + doHide(item, VERTICAL); } else { return 0; } } int hideAllAndCount() { DEBUG( for (int i=1;i<=5;++i) { for (int j=1;j<=5;++j) { if (CONTAINS(items, i) && CONTAINS(items[i], j)) cerr << "X"; else cerr << " "; } cerr << endl; } ); int result = 0; for (auto& row : items) { for (auto& item : row.second) { result += tryRecursiveHide(item.second); } } return result; } int hideAndCount(const vector<Item*>& itemsToRecalculate) { DEBUG( for (int i=1;i<=5;++i) { for (int j=1;j<=5;++j) { if (CONTAINS(items, i) && CONTAINS(items[i], j)) cerr << "X"; else cerr << " "; } cerr << endl; } ); int result = 0; for(auto itemPtr : itemsToRecalculate) { result += tryRecursiveHide(*itemPtr); } return result; } int recalcNeighbours(Item& item) { return doHide(item, BOTH); } void solve() { int n,m,k,q; cin>>n>>m>>k>>q; int x,y; REP(i,k) { cin>>x>>y; items[x][y] = {x, y, true}; revIndices[y].insert(x); } calcNeighbourhood(); int currentResult = hideAllAndCount(); cout << currentResult << endl; REP(i,q) { cin>>x>>y; if (CONTAINS(items, x) && CONTAINS(items[x], y)) { Item& itemToRemove = items[x][y]; if (itemToRemove.visible) { DEBUG(cerr << "remove visible " << x << "," << y << endl;) itemToRemove.visible = false; itemToRemove.hideOrder = 0; currentResult += recalcNeighbours(itemToRemove); } else { DEBUG(cerr << "remove invisible " << x << "," << y << endl;) --currentResult; } if (itemToRemove.left) itemToRemove.left->right = itemToRemove.right; if (itemToRemove.right) itemToRemove.right->left = itemToRemove.left; if (itemToRemove.top) itemToRemove.top->bottom = itemToRemove.bottom; if (itemToRemove.bottom) itemToRemove.bottom->top = itemToRemove.top; DEBUG( cerr << "remove brick "<<itemToRemove<<endl; cerr << " left: "<<itemToRemove.left<<endl; cerr << " right: "<<itemToRemove.right<<endl; cerr << " top: "<<itemToRemove.top<<endl; cerr << " bottom: "<<itemToRemove.bottom<<endl; ) items[x].erase(y); revIndices[y].erase(x); } else { DEBUG(cerr << "add new " << x << "," << y << endl;) Item& newItem = items[x][y] = {x, y, true}; auto& column = revIndices[y]; column.insert(x); auto findRow = items.find(x); auto findItem = findRow->second.find(y); if (findItem != findRow->second.begin()) { newItem.left = &prev(findItem)->second; newItem.right = newItem.left->right; newItem.left->right = &newItem; if (newItem.right) newItem.right->left = &newItem; DEBUG(cerr << "setting left and right based on left" << newItem.left << ", " << newItem.right << endl;) } else if (next(findItem) != findRow->second.end()) { newItem.right = &next(findItem)->second; newItem.right->left = &newItem; // no need to set newItem.left, since we already know there's nothing there DEBUG(cerr << "setting left and right based on right" << newItem.left << ", " << newItem.right << endl;) } else { DEBUG(cerr << "not setting left and right - all empty" << endl;) } auto columnIter = column.find(x); if (columnIter != column.begin()) { newItem.top = &items[*prev(columnIter)][y]; newItem.bottom = newItem.top->bottom; newItem.top->bottom = &newItem; if (newItem.bottom) newItem.bottom->top = &newItem; DEBUG(cerr << "setting top and bottom based on top" << newItem.top << ", " << newItem.bottom << endl;) } else if (next(columnIter) != column.end()) { newItem.bottom = &items[*next(columnIter)][y]; newItem.bottom->top = &newItem; // no need to set newItem.top since we already know there's nothing there DEBUG(cerr << "setting top and bottom based on bottom" << newItem.top << ", " << newItem.bottom << endl;) } else { DEBUG(cerr << "not setting top and bottom - all empty" << endl;) } vector<Item*> affectedItems = restoreVisibility(newItem); currentResult -= affectedItems.size(); DEBUG(cerr<<"restored "<<affectedItems.size()<<" items"<<endl;) affectedItems.push_back(&newItem); currentResult += hideAndCount(affectedItems); } cout << currentResult << endl; } } #ifdef CALC_TIME #include <ctime> #endif int main() { ios_base::sync_with_stdio(0); #ifdef CALC_TIME clock_t begin = clock(); #endif solve(); #ifdef CALC_TIME cerr << "TIME: " << float(clock()-begin)/CLOCKS_PER_SEC << " s " << endl; #endif }
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 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | #include <iostream> #include <iomanip> #include <string> #include <algorithm> #include <map> #include <set> #include <vector> using namespace std; //#define DEBUG(x) x //#define CALC_TIME #ifndef DEBUG #define DEBUG(x) #endif #define REP(x,n) for(int x=0;x<(n);++x) #define VAR(x,n) __typeof(n) x = (n) #define FOREACH(x,c) for(VAR(x, (c).begin()); x != (c).end(); ++x) #define CONTAINS(x,elem) ((x).find(elem) != (x).end()) struct Item { int x; int y; bool visible; Item *left=nullptr, *right=nullptr, *top=nullptr, *bottom=nullptr; int hideOrder=10000000; // Item(){} // Item(int x, int y, bool visible): x(x), y(y), visible(visible), // left(nullptr), right(nullptr), top(nullptr), bottom(nullptr), hideOrder(-1) {} }; ostream& operator<<(ostream& os, const Item& item) { return os << "[" << item.x<<","<<item.y<<"]/"<<item.visible<<"/"<<item.hideOrder; } ostream& operator<<(ostream& os, const Item* item) { if (item) { return os << *item; } else { return os << "null"; } } enum Direction { HORIZONTAL, VERTICAL, BOTH }; map<int,map<int,Item>> items; map<int,set<int>> revIndices; bool inline hasLeftNeighbour(const Item& item) { // DEBUG(cerr<<"hasLeftNeighbour -> "<<item.left<<endl;) return item.left != nullptr && item.left->y == item.y-1; } bool inline hasRightNeighbour(const Item& item) { // DEBUG(cerr<<"hasRightNeighbour -> "<<item.right<<endl;) return item.right != nullptr && item.right->y == item.y+1; } bool inline hasTopNeighbour(const Item& item) { // DEBUG(cerr<<"hasTopRightNeighbour -> "<<item.top<<endl;) return item.top != nullptr && item.top->x == item.x-1; } bool inline hasBottomNeighbour(const Item& item) { // DEBUG(cerr<<"hasBottomNeighbour -> "<<item.bottom<<endl;) return item.bottom != nullptr && item.bottom->x == item.x+1; } bool inline hasFreeEdges(const Item& item, Direction direction) { switch (direction) { case HORIZONTAL: return (!hasLeftNeighbour(item) || !item.left->visible) && (!hasRightNeighbour(item) || !item.right->visible); case VERTICAL: return (!hasTopNeighbour(item) || !item.top->visible) && (!hasBottomNeighbour(item) || !item.bottom->visible); case BOTH: return ( (!hasLeftNeighbour(item) || !item.left->visible) && (!hasRightNeighbour(item) || !item.right->visible) ) || ( (!hasTopNeighbour(item) || !item.top->visible) && (!hasBottomNeighbour(item) || !item.bottom->visible) ); } return false; } int inline getHideOrderIndex(const Item& item) { int horizontalIndex = max( hasLeftNeighbour(item) ? item.left->hideOrder + 1 : 0, hasRightNeighbour(item) ? item.right->hideOrder + 1 : 0 ); int verticalIndex = max( hasBottomNeighbour(item) ? item.bottom->hideOrder + 1 : 0, hasTopNeighbour(item) ? item.top->hideOrder + 1 : 0 ); return min(horizontalIndex, verticalIndex); } void restoreWithChildren(Item& root, vector<Item*>& result) { if (root.visible) { return; } DEBUG(cerr<<"restoring "<<root<<endl;) result.push_back(&root); root.visible = true; DEBUG(cerr<<" check restore left "<<root.left<<endl;) if (root.left && root.left->hideOrder > root.hideOrder) restoreWithChildren(*root.left, result); DEBUG(cerr<<" check restore right "<<root.right<<endl;) if (root.right && root.right->hideOrder > root.hideOrder) restoreWithChildren(*root.right, result); DEBUG(cerr<<" check restore top "<<root.top<<endl;) if (root.top && root.top->hideOrder > root.hideOrder) restoreWithChildren(*root.top, result); DEBUG(cerr<<" check restore bottom "<<root.bottom<<endl;) if (root.bottom && root.bottom->hideOrder > root.hideOrder) restoreWithChildren(*root.bottom, result); root.hideOrder = 10000000; } vector<Item*> restoreVisibility(const Item& root) { vector<Item*> result; if (root.left) restoreWithChildren(*root.left, result); if (root.right) restoreWithChildren(*root.right, result); if (root.top) restoreWithChildren(*root.top, result); if (root.bottom) restoreWithChildren(*root.bottom, result); return result; } void calcNeighbourhood() { for (auto& row : items) { Item* previous = nullptr; for(auto& item : row.second) { if(previous) { item.second.left = previous; previous->right = &item.second; } auto& column = revIndices[item.second.y]; auto findItemInColumn = column.find(item.second.x); if (findItemInColumn != column.begin()) { item.second.top = &items[*prev(findItemInColumn)][item.second.y]; item.second.top->bottom = &item.second; } previous = &item.second; } } } int tryRecursiveHide(Item& item); int doHide(Item& item, Direction direction) { item.hideOrder = item.visible ? getHideOrderIndex(item) : item.hideOrder; DEBUG( cerr << "hide brick "<<item<<" - "<<direction<<endl; cerr << " left: "<<item.left<<endl; cerr << " right: "<<item.right<<endl; cerr << " top: "<<item.top<<endl; cerr << " bottom: "<<item.bottom<<endl; ) item.visible = false; int result = 0; switch (direction) { case HORIZONTAL: if (item.top) result += tryRecursiveHide(*item.top); if (item.bottom) result += tryRecursiveHide(*item.bottom); break; case VERTICAL: if (item.left) result += tryRecursiveHide(*item.left); if (item.right) result += tryRecursiveHide(*item.right); break; case BOTH: if (item.top) result += tryRecursiveHide(*item.top); if (item.bottom) result += tryRecursiveHide(*item.bottom); if (item.left) result += tryRecursiveHide(*item.left); if (item.right) result += tryRecursiveHide(*item.right); break; } return result; } int tryRecursiveHide(Item& item) { if (!item.visible) return 0; //DEBUG(cerr << "try recursive hide ["<<item.x<<","<<item.y<<"]"<<endl;) if (hasFreeEdges(item, HORIZONTAL)) { return 1 + doHide(item, HORIZONTAL); } else if (hasFreeEdges(item, VERTICAL)) { return 1 + doHide(item, VERTICAL); } else { return 0; } } int hideAllAndCount() { DEBUG( for (int i=1;i<=5;++i) { for (int j=1;j<=5;++j) { if (CONTAINS(items, i) && CONTAINS(items[i], j)) cerr << "X"; else cerr << " "; } cerr << endl; } ); int result = 0; for (auto& row : items) { for (auto& item : row.second) { result += tryRecursiveHide(item.second); } } return result; } int hideAndCount(const vector<Item*>& itemsToRecalculate) { DEBUG( for (int i=1;i<=5;++i) { for (int j=1;j<=5;++j) { if (CONTAINS(items, i) && CONTAINS(items[i], j)) cerr << "X"; else cerr << " "; } cerr << endl; } ); int result = 0; for(auto itemPtr : itemsToRecalculate) { result += tryRecursiveHide(*itemPtr); } return result; } int recalcNeighbours(Item& item) { return doHide(item, BOTH); } void solve() { int n,m,k,q; cin>>n>>m>>k>>q; int x,y; REP(i,k) { cin>>x>>y; items[x][y] = {x, y, true}; revIndices[y].insert(x); } calcNeighbourhood(); int currentResult = hideAllAndCount(); cout << currentResult << endl; REP(i,q) { cin>>x>>y; if (CONTAINS(items, x) && CONTAINS(items[x], y)) { Item& itemToRemove = items[x][y]; if (itemToRemove.visible) { DEBUG(cerr << "remove visible " << x << "," << y << endl;) itemToRemove.visible = false; itemToRemove.hideOrder = 0; currentResult += recalcNeighbours(itemToRemove); } else { DEBUG(cerr << "remove invisible " << x << "," << y << endl;) --currentResult; } if (itemToRemove.left) itemToRemove.left->right = itemToRemove.right; if (itemToRemove.right) itemToRemove.right->left = itemToRemove.left; if (itemToRemove.top) itemToRemove.top->bottom = itemToRemove.bottom; if (itemToRemove.bottom) itemToRemove.bottom->top = itemToRemove.top; DEBUG( cerr << "remove brick "<<itemToRemove<<endl; cerr << " left: "<<itemToRemove.left<<endl; cerr << " right: "<<itemToRemove.right<<endl; cerr << " top: "<<itemToRemove.top<<endl; cerr << " bottom: "<<itemToRemove.bottom<<endl; ) items[x].erase(y); revIndices[y].erase(x); } else { DEBUG(cerr << "add new " << x << "," << y << endl;) Item& newItem = items[x][y] = {x, y, true}; auto& column = revIndices[y]; column.insert(x); auto findRow = items.find(x); auto findItem = findRow->second.find(y); if (findItem != findRow->second.begin()) { newItem.left = &prev(findItem)->second; newItem.right = newItem.left->right; newItem.left->right = &newItem; if (newItem.right) newItem.right->left = &newItem; DEBUG(cerr << "setting left and right based on left" << newItem.left << ", " << newItem.right << endl;) } else if (next(findItem) != findRow->second.end()) { newItem.right = &next(findItem)->second; newItem.right->left = &newItem; // no need to set newItem.left, since we already know there's nothing there DEBUG(cerr << "setting left and right based on right" << newItem.left << ", " << newItem.right << endl;) } else { DEBUG(cerr << "not setting left and right - all empty" << endl;) } auto columnIter = column.find(x); if (columnIter != column.begin()) { newItem.top = &items[*prev(columnIter)][y]; newItem.bottom = newItem.top->bottom; newItem.top->bottom = &newItem; if (newItem.bottom) newItem.bottom->top = &newItem; DEBUG(cerr << "setting top and bottom based on top" << newItem.top << ", " << newItem.bottom << endl;) } else if (next(columnIter) != column.end()) { newItem.bottom = &items[*next(columnIter)][y]; newItem.bottom->top = &newItem; // no need to set newItem.top since we already know there's nothing there DEBUG(cerr << "setting top and bottom based on bottom" << newItem.top << ", " << newItem.bottom << endl;) } else { DEBUG(cerr << "not setting top and bottom - all empty" << endl;) } vector<Item*> affectedItems = restoreVisibility(newItem); currentResult -= affectedItems.size(); DEBUG(cerr<<"restored "<<affectedItems.size()<<" items"<<endl;) affectedItems.push_back(&newItem); currentResult += hideAndCount(affectedItems); } cout << currentResult << endl; } } #ifdef CALC_TIME #include <ctime> #endif int main() { ios_base::sync_with_stdio(0); #ifdef CALC_TIME clock_t begin = clock(); #endif solve(); #ifdef CALC_TIME cerr << "TIME: " << float(clock()-begin)/CLOCKS_PER_SEC << " s " << endl; #endif } |