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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
using P = pair<int, int>;
using ll = long long;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(n), c(n), d(m), e(m), f(m);
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> d[i] >> e[i] >> f[i];
  }
  vector<int> x, y;
  for (int i = 0; i < n; i++) {
    x.push_back(a[i]);
    x.push_back(b[i]);
    y.push_back(c[i]);
  }
  for (int i = 0; i < m; i++) {
    x.push_back(d[i]);
    y.push_back(e[i]);
    y.push_back(f[i]);
  }
  sort(x.begin(), x.end());
  sort(y.begin(), y.end());
  x.erase(unique(x.begin(), x.end()), x.end());
  y.erase(unique(y.begin(), y.end()), y.end());
  vector<ll> xp(x.size()), yp(y.size());
  for (int i = 0; i < n; i++) {
    xp[find(x.begin(), x.end(), a[i]) - x.begin()] = a[i];
    xp[find(x.begin(), x.end(), b[i]) - x.begin()] = b[i];
    yp[find(y.begin(), y.end(), c[i]) - y.begin()] = c[i];
  }
  for (int i = 0; i < m; i++) {
    xp[find(x.begin(), x.end(), d[i]) - x.begin()] = d[i];
    yp[find(y.begin(), y.end(), e[i]) - y.begin()] = e[i];
    yp[find(y.begin(), y.end(), f[i]) - y.begin()] = f[i];
  }
  vector<vector<bool>> ywall(y.size() - 1, vector<bool>(x.size(), false)),
      xwall(y.size(), vector<bool>(x.size() - 1, false));
  for (int i = 0; i < n; i++) {
    int xs = lower_bound(xp.begin(), xp.end(), a[i]) - xp.begin(),
        xg = lower_bound(xp.begin(), xp.end(), b[i]) - xp.begin(),
        yy = lower_bound(yp.begin(), yp.end(), c[i]) - yp.begin();
    for (int j = xs; j < xg; j++) {
      xwall[yy][j] = true;
    }
  }
  for (int i = 0; i < m; i++) {
    int ys = lower_bound(yp.begin(), yp.end(), e[i]) - yp.begin(),
        yg = lower_bound(yp.begin(), yp.end(), f[i]) - yp.begin(),
        xx = lower_bound(xp.begin(), xp.end(), d[i]) - xp.begin();
    for (int j = ys; j < yg; j++) {
      ywall[j][xx] = true;
    }
  }
  int sy = lower_bound(yp.begin(), yp.end(), 0) - yp.begin() - 1,
      sx = lower_bound(xp.begin(), xp.end(), 0) - xp.begin() - 1;
  if (sy == -1 || sx == -1 || sy == yp.size() - 1 || sx == xp.size() - 1) {
    cout << "INF" << endl;
    return 0;
  }
  vector<vector<bool>> reached(yp.size() - 1,
                               vector<bool>(xp.size() - 1, false));
  queue<P> task;
  task.push(P(sy, sx));
  ll ret = 0;
  while (!task.empty()) {
    int nx = task.front().second, ny = task.front().first;
    task.pop();
    if (nx < 0 || xp.size() - 1 <= nx || ny < 0 || yp.size() - 1 <= ny) {
      cout << "INF" << endl;
      return 0;
    }
    if (reached[ny][nx])
      continue;
    reached[ny][nx] = true;
    ret += (yp[ny + 1] - yp[ny]) * (xp[nx + 1] - xp[nx]);
    if (xwall[ny][nx] == false)
      task.push(P(ny - 1, nx));
    if (xwall[ny + 1][nx] == false)
      task.push(P(ny + 1, nx));
    if (ywall[ny][nx] == false)
      task.push(P(ny, nx - 1));
    if (ywall[ny][nx + 1] == false)
      task.push(P(ny, nx + 1));
  }
  cout << ret << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
#define int long long
using P = pair<int, int>;
using ll = long long;

signed main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(n), c(n), d(m), e(m), f(m);
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> d[i] >> e[i] >> f[i];
  }
  vector<int> x, y;
  for (int i = 0; i < n; i++) {
    x.push_back(a[i]);
    x.push_back(b[i]);
    y.push_back(c[i]);
  }
  for (int i = 0; i < m; i++) {
    x.push_back(d[i]);
    y.push_back(e[i]);
    y.push_back(f[i]);
  }
  sort(x.begin(), x.end());
  sort(y.begin(), y.end());
  x.erase(unique(x.begin(), x.end()), x.end());
  y.erase(unique(y.begin(), y.end()), y.end());
  vector<ll> xp(x.size()), yp(y.size());
  for (int i = 0; i < n; i++) {
    xp[find(x.begin(), x.end(), a[i]) - x.begin()] = a[i];
    xp[find(x.begin(), x.end(), b[i]) - x.begin()] = b[i];
    yp[find(y.begin(), y.end(), c[i]) - y.begin()] = c[i];
  }
  for (int i = 0; i < m; i++) {
    xp[find(x.begin(), x.end(), d[i]) - x.begin()] = d[i];
    yp[find(y.begin(), y.end(), e[i]) - y.begin()] = e[i];
    yp[find(y.begin(), y.end(), f[i]) - y.begin()] = f[i];
  }
  vector<vector<bool>> ywall(y.size() - 1, vector<bool>(x.size(), false)),
      xwall(y.size(), vector<bool>(x.size() - 1, false));
  for (int i = 0; i < n; i++) {
    int xs = lower_bound(xp.begin(), xp.end(), a[i]) - xp.begin(),
        xg = lower_bound(xp.begin(), xp.end(), b[i]) - xp.begin(),
        yy = lower_bound(yp.begin(), yp.end(), c[i]) - yp.begin();
    for (int j = xs; j < xg; j++) {
      xwall[yy][j] = true;
    }
  }
  for (int i = 0; i < m; i++) {
    int ys = lower_bound(yp.begin(), yp.end(), e[i]) - yp.begin(),
        yg = lower_bound(yp.begin(), yp.end(), f[i]) - yp.begin(),
        xx = lower_bound(xp.begin(), xp.end(), d[i]) - xp.begin();
    for (int j = ys; j < yg; j++) {
      ywall[j][xx] = true;
    }
  }
  int sy = lower_bound(yp.begin(), yp.end(), 0) - yp.begin() - 1,
      sx = lower_bound(xp.begin(), xp.end(), 0) - xp.begin() - 1;
  if (sy == -1 || sx == -1 || sy == yp.size() - 1 || sx == xp.size() - 1) {
    cout << "INF" << endl;
    return 0;
  }
  vector<vector<bool>> reached(yp.size() - 1,
                               vector<bool>(xp.size() - 1, false));
  queue<P> task;
  task.push(P(sy, sx));
  ll ret = 0;
  while (!task.empty()) {
    int nx = task.front().second, ny = task.front().first;
    task.pop();
    if (nx < 0 || xp.size() - 1 <= nx || ny < 0 || yp.size() - 1 <= ny) {
      cout << "INF" << endl;
      return 0;
    }
    if (reached[nx][ny])
      continue;
    reached[nx][ny] = true;
    ret += (yp[ny + 1] - yp[ny]) * (xp[nx + 1] - xp[nx]);
    if (xwall[ny][nx] == false)
      task.push(P(ny - 1, nx));
    if (xwall[ny + 1][nx] == false)
      task.push(P(ny + 1, nx));
    if (ywall[ny][nx] == false)
      task.push(P(ny, nx - 1));
    if (ywall[ny][nx + 1] == false)
      task.push(P(ny, nx + 1));
  }
  cout << ret << endl;
}