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

#include <bits/stdc++.h>
#define IO_OP                                                                  \\
  std::ios::sync_with_stdio(0);                                                \\
  std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << " is " << x << endl

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;

const int INF = 1e9 + 7, N = 4010;

int a[N], b[N], c[N], d[N], e[N], f[N];
bool l[N][N], r[N][N], u[N][N], dd[N][N]; // blocked ?
bool vis[N][N];

int dir[][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

vi xs({-INF, INF}), ys({-INF, INF});

queue<pi> q;

void BFS(int x, int y) {
  vis[x][y] = 1;
  q.push({x, y});
  while (q.size()) {
    int x = q.front().F, y = q.front().S;
    q.pop();
    for (int i = 0; i < 4; i++) {
      if (i == 0 && u[x][y])
        continue;
      if (i == 1 && dd[x][y])
        continue;
      if (i == 2 && r[x][y])
        continue;
      if (i == 3 && l[x][y])
        continue;
      int xx = x + dir[i][0];
      int yy = y + dir[i][1];
      if (xx < 0 || yy < 0)
        continue;
      if (xx >= xs.size() - 1 || yy >= ys.size() - 1)
        continue;
      if (vis[xx][yy])
        continue;
      vis[xx][yy] = 1;
      q.push({xx, yy});
    }
  }
}

signed main() {
  IO_OP;

  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    xs.PB(a[i]);
    xs.PB(b[i]);
    ys.PB(c[i]);
  }
  for (int i = 0; i < m; i++) {
    cin >> d[i] >> e[i] >> f[i];
    xs.PB(d[i]);
    ys.PB(e[i]);
    ys.PB(f[i]);
  }
  sort(ALL(xs));
  sort(ALL(ys));
  xs.resize(unique(ALL(xs)) - xs.begin());
  ys.resize(unique(ALL(ys)) - ys.begin());
  int x = lower_bound(ALL(xs), 0) - xs.begin();
  int y = lower_bound(ALL(ys), 0) - ys.begin();
  x--, y--;
  for (int i = 0; i < n; i++) {
    int aa = lower_bound(ALL(xs), a[i]) - xs.begin();
    int bb = lower_bound(ALL(xs), b[i]) - xs.begin();
    int cc = lower_bound(ALL(ys), c[i]) - ys.begin();
    for (int j = aa; j < bb; j++) {
      u[j][cc - 1] = 1;
      dd[j][cc] = 1;
    }
  }
  for (int i = 0; i < m; i++) {
    int ddd = lower_bound(ALL(xs), d[i]) - xs.begin();
    int ee = lower_bound(ALL(ys), e[i]) - ys.begin();
    int ff = lower_bound(ALL(ys), f[i]) - ys.begin();
    for (int j = ee; j < ff; j++) {
      r[ddd - 1][j] = 1;
      l[ddd][j] = 1;
    }
  }
  BFS(x, y);
  if (vis[0][0] || vis[xs.size() - 1][ys.size() - 1]) {
    cout << "INF" << endl;
    return 0;
  }
  ll ans = 0;
  for (int i = 0; i < xs.size() - 1; i++) {
    for (int j = 0; j < ys.size() - 1; j++) {
      if (vis[i][j]) {
        ans += (ll)(xs[i + 1] - xs[i]) * (ys[j + 1] - ys[j]);
      }
    }
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define IO_OP                                                                  \\
  std::ios::sync_with_stdio(0);                                                \\
  std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << " is " << x << endl

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;

const int INF = 1e9 + 7, N = 4010;

int a[N], b[N], c[N], d[N], e[N], f[N];
bool l[N][N], r[N][N], u[N][N], dd[N][N]; // blocked ?
bool vis[N][N];

int dir[][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

vi xs({-INF, INF}), ys({-INF, INF});

void dfs(int x, int y) {
  vis[x][y] = 1;
  for (int i = 0; i < 4; i++) {
    if (i == 0 && u[x][y])
      continue;
    if (i == 1 && dd[x][y])
      continue;
    if (i == 2 && r[x][y])
      continue;
    if (i == 3 && l[x][y])
      continue;
    int xx = x + dir[i][0];
    int yy = y + dir[i][1];
    if (xx < 0 || yy < 0)
      continue;
    if (xx >= xs.size() - 1 || yy >= ys.size() - 1)
      continue;
    if (vis[xx][yy])
      continue;
    dfs(xx, yy);
  }
}

signed main() {
  IO_OP;

  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    xs.PB(a[i]);
    xs.PB(b[i]);
    ys.PB(c[i]);
  }
  for (int i = 0; i < m; i++) {
    cin >> d[i] >> e[i] >> f[i];
    xs.PB(d[i]);
    ys.PB(e[i]);
    ys.PB(f[i]);
  }
  sort(ALL(xs));
  sort(ALL(ys));
  xs.resize(unique(ALL(xs)) - xs.begin());
  ys.resize(unique(ALL(ys)) - ys.begin());
  int x = lower_bound(ALL(xs), 0) - xs.begin();
  int y = lower_bound(ALL(ys), 0) - ys.begin();
  x--, y--;
  for (int i = 0; i < n; i++) {
    int aa = lower_bound(ALL(xs), a[i]) - xs.begin();
    int bb = lower_bound(ALL(xs), b[i]) - xs.begin();
    int cc = lower_bound(ALL(ys), c[i]) - ys.begin();
    for (int j = aa; j < bb; j++) {
      u[j][cc - 1] = 1;
      dd[j][cc] = 1;
    }
  }
  for (int i = 0; i < m; i++) {
    int ddd = lower_bound(ALL(xs), d[i]) - xs.begin();
    int ee = lower_bound(ALL(ys), e[i]) - ys.begin();
    int ff = lower_bound(ALL(ys), f[i]) - ys.begin();
    for (int j = ee; j < ff; j++) {
      r[ddd - 1][j] = 1;
      l[ddd][j] = 1;
    }
  }
  dfs(x, y);
  if (vis[0][0] || vis[xs.size() - 1][ys.size() - 1]) {
    cout << "INF" << endl;
    return 0;
  }
  ll ans = 0;
  for (int i = 0; i < xs.size() - 1; i++) {
    for (int j = 0; j < ys.size() - 1; j++) {
      if (vis[i][j]) {
        ans += (ll)(xs[i + 1] - xs[i]) * (ys[j + 1] - ys[j]);
      }
    }
  }
  cout << ans << endl;
}