Problem p02680 (C++)

Buggy submission (status: Runtime Error):
#ifdef _MSC_VER
#include <__msvc_all_public_headers.hpp>
#undef min
#undef max
#else
#include <bits/stdc++.h>
#endif

using namespace std;
using large = long long;

struct Point {
  int x, y;
};

struct Bar {
  Point a, b;
};

int main() {
  int n, m;
  cin >> n >> m;

  auto vertical_bars = vector<Bar>(n);
  auto horizontal_bars = vector<Bar>(m);

  auto Sx = set<int>();
  auto Sy = set<int>();

  Sx.insert(0); // cow position
  Sy.insert(0);

  for (int i = 0; i < n; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    vertical_bars[i].a = {a, c};
    vertical_bars[i].b = {b, c};

    Sx.insert(a);
    Sx.insert(b);
    Sy.insert(c);
  }
  for (int i = 0; i < m; i++) {
    int d, e, f;
    cin >> d >> e >> f;
    horizontal_bars[i].a = {d, e};
    horizontal_bars[i].b = {d, f};
    Sx.insert(d);
    Sy.insert(e);
    Sy.insert(f);
  }

  auto Qx_of = map<int, int>();
  auto Qy_of = map<int, int>();
  {
    auto index = 0;
    for (auto x : Sx)
      Qx_of[x] = index++;
    index = 0;
    for (auto y : Sy)
      Qy_of[y] = index++;
  }

  constexpr int Dim = 4096;
  auto plain_ptr = make_unique<array<array<char, Dim>, Dim>>();
  auto &plain = *plain_ptr;

  for (auto bar : vertical_bars) {
    auto x1 = Qx_of[bar.a.x] * 2 + 10;
    auto x2 = Qx_of[bar.b.x] * 2 + 10;
    auto y1 = Qy_of[bar.a.y] * 2 + 10;
    for (auto x = x1; x <= x2; ++x) {
      plain[x][y1] = 1;
    }
  }
  for (auto bar : horizontal_bars) {
    auto x1 = Qx_of[bar.a.x] * 2 + 10;
    auto y1 = Qy_of[bar.a.y] * 2 + 10;
    auto y2 = Qy_of[bar.b.y] * 2 + 10;
    for (auto y = y1; y <= y2; ++y) {
      plain[x1][y] = 1;
    }
  }

  // quantize cow position
  const auto cx = Qx_of[0] * 2 + 10;
  const auto cy = Qy_of[0] * 2 + 10;

#ifdef _MSC_VER
  for (int x = 10; x < 80; x++) {
    for (int y = 10; y < 80; y++) {
      if (cx == x && cy == y)
        cerr << '@';
      else
        cerr << (".#c"[plain[x][y]]);
    }
    cerr << endl;
  }
#endif

  auto q = vector<Point>();
  q.reserve(Dim * Dim);
  auto pop = size_t{0};
  auto unbounded = false;
  q.emplace_back(Point{cx, cy});
  const auto delta =
      array<Point, 4>{Point{-1, 0}, Point{0, -1}, Point{1, 0}, Point{0, 1}};
  while (pop < q.size() && !unbounded) {
    auto [px, py] = q[pop++];
    for (auto [dx, dy] : delta) {
      auto nx = px + dx;
      auto ny = py + dy;
      if (nx < 0 || nx >= Dim || ny < 0 || ny >= Dim)
        continue;
      if (plain[nx][ny] != 0)
        continue;

      plain[nx][ny] = 2;
      q.emplace_back(Point{nx, ny});

      if (nx == 0 && ny == 0) {
        unbounded = true;
        break;
      }
    }
  }

#ifdef _MSC_VER
  for (int x = 10; x < 80; x++) {
    for (int y = 10; y < 80; y++) {
      if (cx == x && cy == y)
        cerr << '@';
      else
        cerr << (".#c"[plain[x][y]]);
    }
    cerr << endl;
  }
#endif

  if (unbounded) {
    cout << "INF";
    return 0;
  }

  auto x_len = vector<int>();
  {
    auto itr = Sx.begin();
    auto last_x = *itr++;
    for (; itr != Sx.end(); ++itr) {
      x_len.emplace_back(*itr - last_x);
      last_x = *itr;
    }
  }
  auto y_len = vector<int>();
  {
    auto itr = Sy.begin();
    auto last_y = *itr++;
    for (; itr != Sy.end(); ++itr) {
      y_len.emplace_back(*itr - last_y);
      last_y = *itr;
    }
  }

  auto area = 0LL;
  for (int x = 11; x < Dim; x += 2) {
    for (int y = 11; y < Dim; y += 2) {
      if (plain[x][y] != 2)
        continue;

      auto i = (x - 10) / 2;
      auto j = (y - 10) / 2;
      if (i >= x_len.size() || j >= y_len.size())
        continue;

      auto xval = x_len[(x - 10) / 2];
      auto yval = y_len[(y - 10) / 2];
      area += large(xval) * yval;
    }
  }

  cout << area;
  return 0;
}


Fixed submission:
#ifdef _MSC_VER
#include <__msvc_all_public_headers.hpp>
#undef min
#undef max
#else
#include <bits/stdc++.h>
#endif

using namespace std;
using large = long long;

struct Point {
  int x, y;
};

struct Bar {
  Point a, b;
};

int main() {
  int n, m;
  cin >> n >> m;

  auto vertical_bars = vector<Bar>(n);
  auto horizontal_bars = vector<Bar>(m);

  auto Sx = set<int>();
  auto Sy = set<int>();

  Sx.insert(0); // cow position
  Sy.insert(0);

  for (int i = 0; i < n; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    vertical_bars[i].a = {a, c};
    vertical_bars[i].b = {b, c};

    Sx.insert(a);
    Sx.insert(b);
    Sy.insert(c);
  }
  for (int i = 0; i < m; i++) {
    int d, e, f;
    cin >> d >> e >> f;
    horizontal_bars[i].a = {d, e};
    horizontal_bars[i].b = {d, f};
    Sx.insert(d);
    Sy.insert(e);
    Sy.insert(f);
  }

  auto Qx_of = map<int, int>();
  auto Qy_of = map<int, int>();
  {
    auto index = 0;
    for (auto x : Sx)
      Qx_of[x] = index++;
    index = 0;
    for (auto y : Sy)
      Qy_of[y] = index++;
  }

  constexpr int Dim = 8192;
  auto plain_ptr = make_unique<array<array<char, Dim>, Dim>>();
  auto &plain = *plain_ptr;

  for (auto bar : vertical_bars) {
    auto x1 = Qx_of[bar.a.x] * 2 + 10;
    auto x2 = Qx_of[bar.b.x] * 2 + 10;
    auto y1 = Qy_of[bar.a.y] * 2 + 10;
    for (auto x = x1; x <= x2; ++x) {
      plain[x][y1] = 1;
    }
  }
  for (auto bar : horizontal_bars) {
    auto x1 = Qx_of[bar.a.x] * 2 + 10;
    auto y1 = Qy_of[bar.a.y] * 2 + 10;
    auto y2 = Qy_of[bar.b.y] * 2 + 10;
    for (auto y = y1; y <= y2; ++y) {
      plain[x1][y] = 1;
    }
  }

  // quantize cow position
  const auto cx = Qx_of[0] * 2 + 10;
  const auto cy = Qy_of[0] * 2 + 10;

#ifdef _MSC_VER
  for (int x = 10; x < 80; x++) {
    for (int y = 10; y < 80; y++) {
      if (cx == x && cy == y)
        cerr << '@';
      else
        cerr << (".#c"[plain[x][y]]);
    }
    cerr << endl;
  }
#endif

  auto q = vector<Point>();
  q.reserve(Dim * Dim);
  auto pop = size_t{0};
  auto unbounded = false;
  q.emplace_back(Point{cx, cy});
  const auto delta =
      array<Point, 4>{Point{-1, 0}, Point{0, -1}, Point{1, 0}, Point{0, 1}};
  while (pop < q.size() && !unbounded) {
    auto [px, py] = q[pop++];
    for (auto [dx, dy] : delta) {
      auto nx = px + dx;
      auto ny = py + dy;
      if (nx < 0 || nx >= Dim || ny < 0 || ny >= Dim)
        continue;
      if (plain[nx][ny] != 0)
        continue;

      plain[nx][ny] = 2;
      q.emplace_back(Point{nx, ny});

      if (nx == 0 && ny == 0) {
        unbounded = true;
        break;
      }
    }
  }

#ifdef _MSC_VER
  for (int x = 10; x < 80; x++) {
    for (int y = 10; y < 80; y++) {
      if (cx == x && cy == y)
        cerr << '@';
      else
        cerr << (".#c"[plain[x][y]]);
    }
    cerr << endl;
  }
#endif

  if (unbounded) {
    cout << "INF";
    return 0;
  }

  auto x_len = vector<int>();
  {
    auto itr = Sx.begin();
    auto last_x = *itr++;
    for (; itr != Sx.end(); ++itr) {
      x_len.emplace_back(*itr - last_x);
      last_x = *itr;
    }
  }
  auto y_len = vector<int>();
  {
    auto itr = Sy.begin();
    auto last_y = *itr++;
    for (; itr != Sy.end(); ++itr) {
      y_len.emplace_back(*itr - last_y);
      last_y = *itr;
    }
  }

  auto area = 0LL;
  for (int x = 11; x < Dim; x += 2) {
    for (int y = 11; y < Dim; y += 2) {
      if (plain[x][y] != 2)
        continue;

      auto i = (x - 10) / 2;
      auto j = (y - 10) / 2;
      if (i >= x_len.size() || j >= y_len.size())
        continue;

      auto xval = x_len[(x - 10) / 2];
      auto yval = y_len[(y - 10) / 2];
      area += large(xval) * yval;
    }
  }

  cout << area;
  return 0;
}