Problem p02680 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef queue<pii> qp;

const int X = 1e9 + 1;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  int n, m;
  cin >> n >> m;
  int a[n], b[n], c[n];
  vi mx, my;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    mx.push_back(a[i]);
    mx.push_back(b[i]);
    my.push_back(c[i]);
  }
  int d[m], e[m], f[m];
  for (int i = 0; i < m; i++) {
    cin >> d[i] >> e[i] >> f[i];
    mx.push_back(d[i]);
    my.push_back(e[i]);
    my.push_back(f[i]);
  }

  mx.push_back(-X);
  mx.push_back(0);
  mx.push_back(X);
  my.push_back(-X);
  my.push_back(0);
  my.push_back(X);

  sort(mx.begin(), mx.end());
  mx.resize(unique(mx.begin(), mx.end()) - mx.begin());
  sort(my.begin(), my.end());
  my.resize(unique(my.begin(), my.end()) - my.begin());

  int xsz = mx.size() - 1, ysz = my.size() - 1;
  bool s[xsz][ysz], down[xsz][ysz], right[xsz][ysz];
  fill(s[0], s[xsz], false);
  fill(down[0], down[xsz], false);
  fill(right[0], right[xsz], false);

  for (int i = 0; i < n; i++) {
    int xa = lower_bound(mx.begin(), mx.end(), a[i]) - mx.begin();
    int xb = lower_bound(mx.begin(), mx.end(), b[i]) - mx.begin();
    int yc = lower_bound(my.begin(), my.end(), c[i]) - my.begin();

    for (int j = xa; j < xb; j++) {
      right[j][yc - 1] = true;
    }
  }

  for (int i = 0; i < m; i++) {
    int xd = lower_bound(mx.begin(), mx.end(), d[i]) - mx.begin();
    int ye = lower_bound(my.begin(), my.end(), e[i]) - my.begin();
    int yf = lower_bound(my.begin(), my.end(), f[i]) - my.begin();

    for (int j = ye; j < yf; j++) {
      down[xd - 1][j] = true;
    }
  }

  int x0 = lower_bound(mx.begin(), mx.end(), 0) - mx.begin();
  int y0 = lower_bound(my.begin(), my.end(), 0) - my.begin();

  qp q;
  q.emplace(x0, y0);
  s[x0][y0] = true;
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();

    if (x < xsz - 1 && !down[x][y] && !s[x + 1][y]) {
      s[x + 1][y] = true;
      q.emplace(x + 1, y);
    }
    if (x >= 1 && !down[x - 1][y] && !s[x - 1][y]) {
      s[x - 1][y] = true;
      q.emplace(x - 1, y);
    }
    if (y < ysz - 1 && !right[x][y] && !s[x][y + 1]) {
      s[x][y + 1] = true;
      q.emplace(x, y + 1);
    }
    if (y >= 1 && !right[x][y - 1] && !s[x][y - 1]) {
      s[x][y - 1] = true;
      q.emplace(x, y - 1);
    }
  }

  ll ans = 0;
  for (int i = 0; i < xsz; i++) {
    for (int j = 0; j < ysz; j++) {
      if (s[i][j]) {
        if (i == 0 || i == xsz - 1 || j == 0 || j == ysz - 1) {
          cout << "INF" << endl;
          return 0;
        }

        ans += 1ll * (mx[i + 1] - mx[i]) * (my[j + 1] - my[j]);
      }
    }
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vb> v2b;

const int X = 1e9 + 1;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, -1, 0, 1};

void ff(const v2b &down, const v2b &right, v2b &s, int x, int y) {
  if (s[x][y]) {
    return;
  }

  s[x][y] = true;

  if (x < s.size() - 1 && !down[x][y]) {
    ff(down, right, s, x + 1, y);
  }
  if (x >= 1 && !down[x - 1][y]) {
    ff(down, right, s, x - 1, y);
  }
  if (y < s[0].size() - 1 && !right[x][y]) {
    ff(down, right, s, x, y + 1);
  }
  if (y >= 1 && !right[x][y - 1]) {
    ff(down, right, s, x, y - 1);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  int n, m;
  cin >> n >> m;
  int a[n], b[n], c[n];
  vi mx, my;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    mx.push_back(a[i]);
    mx.push_back(b[i]);
    my.push_back(c[i]);
  }
  int d[m], e[m], f[m];
  for (int i = 0; i < m; i++) {
    cin >> d[i] >> e[i] >> f[i];
    mx.push_back(d[i]);
    my.push_back(e[i]);
    my.push_back(f[i]);
  }

  mx.push_back(-X);
  mx.push_back(0);
  mx.push_back(X);
  my.push_back(-X);
  my.push_back(0);
  my.push_back(X);

  sort(mx.begin(), mx.end());
  mx.resize(unique(mx.begin(), mx.end()) - mx.begin());
  sort(my.begin(), my.end());
  my.resize(unique(my.begin(), my.end()) - my.begin());

  int xsz = mx.size() - 1, ysz = my.size() - 1;
  v2b s(xsz, vb(ysz)), down(xsz, vb(ysz)), right(xsz, vb(ysz));

  for (int i = 0; i < n; i++) {
    int xa = lower_bound(mx.begin(), mx.end(), a[i]) - mx.begin();
    int xb = lower_bound(mx.begin(), mx.end(), b[i]) - mx.begin();
    int yc = lower_bound(my.begin(), my.end(), c[i]) - my.begin();

    for (int j = xa; j < xb; j++) {
      down[j][yc - 1] = true;
    }
  }

  for (int i = 0; i < m; i++) {
    int xd = lower_bound(mx.begin(), mx.end(), d[i]) - mx.begin();
    int ye = lower_bound(my.begin(), my.end(), e[i]) - my.begin();
    int yf = lower_bound(my.begin(), my.end(), f[i]) - my.begin();

    for (int j = ye; j < yf; j++) {
      right[xd - 1][j] = true;
    }
  }

  int x0 = lower_bound(mx.begin(), mx.end(), 0) - mx.begin();
  int y0 = lower_bound(my.begin(), my.end(), 0) - my.begin();
  ff(right, down, s, x0, y0);

  ll ans = 0;
  for (int i = 0; i < xsz; i++) {
    for (int j = 0; j < ysz; j++) {
      if (s[i][j]) {
        if (i == 0 || i == xsz - 1 || j == 0 || j == ysz - 1) {
          cout << "INF" << endl;
          return 0;
        }

        ans += 1ll * (mx[i + 1] - mx[i]) * (my[j + 1] - my[j]);
      }
    }
  }

  cout << ans << endl;

  return 0;
}