fork download
  1. #include <iostream>
  2. #include <set>
  3. #include <algorithm>
  4.  
  5. struct Vector3 {
  6. int x, y, z;
  7. };
  8. struct Area {
  9. Vector3 first;
  10. Vector3 second;
  11. inline bool inArea(Vector3 &vec) const
  12. {
  13. auto minX = std::min(first.x, second.x);
  14. auto maxX = std::max(first.x, second.x);
  15. auto minY = std::min(first.y, second.y);
  16. auto maxY = std::max(first.y, second.y);
  17. auto minZ = std::min(first.z, second.z);
  18. auto maxZ = std::max(first.z, second.z);
  19. auto &x = vec.x;
  20. auto &y = vec.y;
  21. auto &z = vec.z;
  22.  
  23. return x >= minX && x <= maxX && y >= minY && y <= maxY && z >= minZ && z <= maxZ;
  24. }
  25.  
  26. inline bool operator<(Area const& rhs) const
  27. {
  28. return std::max(first.x, second.x) < std::max(rhs.first.x, rhs.second.x);
  29. }
  30. };
  31.  
  32. int debugCnt = 0;
  33. std::set<Area> areas;
  34. // std::map<int, std::set<Area>::iterator> myMap;
  35. bool canPlaceBlock(Vector3 block)
  36. {
  37. for (auto it = areas.lower_bound({block, block}); it != areas.end() && std::min(it->first.x, it->second.x) <= block.x; ++it) {
  38. ++debugCnt;
  39. if (it->inArea(block))
  40. return false;
  41. }
  42. return true;
  43. }
  44.  
  45. int main() {
  46. // test code
  47. for (int l = 0; l <= 10000; l++) {
  48. int r = l + 100;
  49. Vector3 first{l, l, l};
  50. Vector3 second{r, r, r};
  51. areas.insert({first, second});
  52. }
  53. std::cout << (canPlaceBlock({123, 456, 789}) ? "true" : "false") << '\n';
  54. std::cout << debugCnt << '\n';
  55. return 0;
  56. }
Success #stdin #stdout 0.01s 5452KB
stdin
Standard input is empty
stdout
true
101