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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
typedef long long int lli;
typedef pair<int, int> pii;

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<lli> xs, ys;
  vector<lli> a(n), b(n), c(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    xs.push_back(a[i]);
    xs.push_back(b[i]);
    ys.push_back(c[i]);
  }
  vector<lli> d(m), e(m), f(m);
  for (int i = 0; i < m; i++) {
    cin >> d[i] >> e[i] >> f[i];
    xs.push_back(d[i]);
    ys.push_back(e[i]);
    ys.push_back(f[i]);
  }

  xs.push_back(0);
  ys.push_back(0);
  sort(xs.begin(), xs.end());
  xs.erase(unique(xs.begin(), xs.end()), xs.end());
  sort(ys.begin(), ys.end());
  ys.erase(unique(ys.begin(), ys.end()), ys.end());
  map<lli, lli> xconv;
  for (int i = 0; i < (int)xs.size(); i++) {
    xconv[xs[i]] = i;
  }
  map<lli, lli> yconv;
  for (int i = 0; i < (int)ys.size(); i++) {
    yconv[ys[i]] = i;
  }
  vector<vector<int>> grid(xs.size() * 2 + 3,
                           vector<int>(ys.size() * 2 + 3, 0));
  for (int i = 0; i < n; i++) {
    int y = 2 * yconv[c[i]] + 1;
    int xfrom = 2 * xconv[a[i]] + 1;
    int xto = 2 * xconv[b[i]] + 1;
    for (int x = xfrom; x <= xto; x++) {
      grid[x][y] = 1;
    }
  }
  for (int i = 0; i < m; i++) {
    int x = 2 * xconv[d[i]] + 1;
    int yfrom = 2 * yconv[e[i]] + 1;
    int yto = 2 * yconv[f[i]] + 1;
    for (int y = yfrom; y <= yto; y++) {
      grid[x][y] = 1;
    }
  }

  queue<pii> wait;
  wait.push(pii(2 * xconv[0] + 1, 2 * yconv[0] + 1));
  while (!wait.empty()) {
    int x = wait.front().first;
    int y = wait.front().second;
    wait.pop();
    for (int d = 0; d < 4; d++) {
      int nx = x + dx[d];
      int ny = y + dy[d];
      if (nx < 0 or nx >= (int)grid.size())
        continue;
      if (ny < 0 or ny >= (int)grid[0].size())
        continue;
      if (grid[nx][ny] == 0) {
        grid[nx][ny] = 2;
        wait.push(pii(nx, ny));
      }
    }
  }
  if (grid[0][0] == 2) {
    cout << "INF" << endl;
    return 0;
  }

  lli ans = 0;
  for (int i = 0; i < (int)xs.size() - 1; i++) {
    lli w = xs[i + 1] - xs[i];
    for (int j = 0; j < (int)ys.size() - 1; j++) {
      lli h = ys[j + 1] - ys[j];
      if (grid[2 * i + 2][2 * j + 2] == 2) {
        ans += w * h;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
typedef long long int lli;

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
void color(int x, int y, vector<vector<int>> &g) {
  if (x < 0 or x >= (int)g.size())
    return;
  if (y < 0 or y >= (int)g[0].size())
    return;
  if (g[x][y] == 0) {
    g[x][y] = 2;
    for (int d = 0; d < 4; d++) {
      color(x + dx[d], y + dy[d], g);
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<lli> xs, ys;
  vector<lli> a(n), b(n), c(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    xs.push_back(a[i]);
    xs.push_back(b[i]);
    ys.push_back(c[i]);
  }
  vector<lli> d(m), e(m), f(m);
  for (int i = 0; i < m; i++) {
    cin >> d[i] >> e[i] >> f[i];
    xs.push_back(d[i]);
    ys.push_back(e[i]);
    ys.push_back(f[i]);
  }

  xs.push_back(0);
  ys.push_back(0);
  sort(xs.begin(), xs.end());
  xs.erase(unique(xs.begin(), xs.end()), xs.end());
  sort(ys.begin(), ys.end());
  ys.erase(unique(ys.begin(), ys.end()), ys.end());
  map<lli, lli> xconv;
  for (int i = 0; i < (int)xs.size(); i++) {
    xconv[xs[i]] = i;
  }
  map<lli, lli> yconv;
  for (int i = 0; i < (int)ys.size(); i++) {
    yconv[ys[i]] = i;
  }
  vector<vector<int>> grid(xs.size() * 2 + 2,
                           vector<int>(ys.size() * 2 + 2, 0));
  for (int i = 0; i < n; i++) {
    int y = 2 * yconv[c[i]] + 1;
    int xfrom = 2 * xconv[a[i]] + 1;
    int xto = 2 * xconv[b[i]] + 1;
    for (int x = xfrom; x <= xto; x++) {
      grid[x][y] = 1;
    }
  }
  for (int i = 0; i < m; i++) {
    int x = 2 * xconv[d[i]] + 1;
    int yfrom = 2 * yconv[e[i]] + 1;
    int yto = 2 * yconv[f[i]] + 1;
    for (int y = yfrom; y <= yto; y++) {
      grid[x][y] = 1;
    }
  }
  color(2 * xconv[0] + 1, 2 * yconv[0] + 1, grid);
  if (grid[0][0] == 2) {
    cout << "INF" << endl;
    return 0;
  }

  lli ans = 0;
  for (int i = 0; i < (int)xs.size() - 1; i++) {
    lli w = xs[i + 1] - xs[i];
    for (int j = 0; j < (int)ys.size() - 1; j++) {
      lli h = ys[j + 1] - ys[j];
      if (grid[2 * i + 2][2 * j + 2] == 2) {
        ans += w * h;
      }
    }
  }
  cout << ans << endl;
  return 0;
}