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

#include <bits/stdc++.h>
#define ll long long
#define pi pair<int, int>
#define all(xs) (xs).begin(), (xs).end()
#define F first
#define S second
#define PB push_back

using namespace std;

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

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

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

vector<int> xs({-INF, INF}), ys({-INF, INF});
queue<pi> q;

void BFS(int x, int y) {
  q.push({x, y});
  vis[x][y] = 1;
  while (!q.empty()) {
    int xi = q.front().F, yi = q.front().S;
    q.pop();
    for (int i = 0; i < 4; i++) {
      if (i == 0 and u[xi][yi])
        continue;
      if (i == 1 and dd[xi][yi])
        continue;
      if (i == 2 and r[xi][yi])
        continue;
      if (i == 3 and l[xi][yi])
        continue;
      int xii = xi + dir[i][0], yii = yi + dir[i][1];
      if (xii < 0 or yii < 0 or xii >= xs.size() - 1 or yii >= ys.size() - 1)
        continue;
      if (vis[xii][yii])
        continue;
      vis[xii][yii] = 1;
      q.push({xii, yii});
    }
  }
}

int main() {
  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(), y;
  y = lower_bound(all(ys), 0) - ys.begin();
  x--, y--;
  for (int i = 0; i < n; i++) {
    int ai = lower_bound(all(xs), a[i]) - xs.begin(), bi, ci;
    bi = lower_bound(all(xs), b[i]) - xs.begin();
    ci = lower_bound(all(ys), c[i]) - ys.begin();
    for (int j = ai; j < bi; j++) {
      u[j][ci - 1] = 1;
      dd[j][ci] = 1;
    }
  }
  for (int i = 0; i < m; i++) {
    int di = lower_bound(all(xs), d[i]) - xs.begin(), ei, fi;
    ei = lower_bound(all(ys), e[i]) - ys.begin();
    fi = lower_bound(all(ys), f[i]) - ys.begin();
    for (int j = ei; j < fi; j++) {
      r[di - 1][j] = 1;
      l[di][j] = 1;
    }
  }
  BFS(x, y);
  if (vis[0][0] or vis[xs.size() - 1][ys.size() - 1]) {
    cout << "INF"
         << "\\n";
    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 = (ans + (xs[i + 1] - xs[i]) * 1LL * (ys[j + 1] - ys[j]));
  }
  cout << ans << "\\n";
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pi pair<int, int>
#define N 1005
#define INF 1000000007
#define all(xs) (xs).begin(), (xs).end()
#define F first
#define S second
#define PB push_back

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

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

vector<int> xs({-INF, INF}), ys({-INF, INF});
queue<pi> q;

void BFS(int x, int y) {
  q.push({x, y});
  vis[x][y] = 1;
  while (!q.empty()) {
    int xi = q.front().F, yi = q.front().S;
    q.pop();
    for (int i = 0; i < 4; i++) {
      if (i == 0 and u[xi][yi])
        continue;
      if (i == 1 and dd[xi][yi])
        continue;
      if (i == 2 and r[xi][yi])
        continue;
      if (i == 3 and l[xi][yi])
        continue;
      int xii = xi + dir[i][0], yii = yi + dir[i][1];
      if (xii < 0 or yii < 0 or xii >= xs.size() - 1 or yii >= ys.size() - 1)
        continue;
      if (vis[xii][yii])
        continue;
      vis[xii][yii] = 1;
      q.push({xii, yii});
    }
  }
}

int main() {
  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(), y;
  y = lower_bound(all(ys), 0) - ys.begin();
  x--, y--;
  for (int i = 0; i < n; i++) {
    int ai = lower_bound(all(xs), a[i]) - xs.begin(), bi, ci;
    bi = lower_bound(all(xs), b[i]) - xs.begin();
    ci = lower_bound(all(ys), c[i]) - ys.begin();
    for (int j = ai; j < bi; j++) {
      u[j][ci - 1] = 1;
      dd[j][ci] = 1;
    }
  }
  for (int i = 0; i < n; i++) {
    int di = lower_bound(all(xs), d[i]) - xs.begin(), ei, fi;
    ei = lower_bound(all(ys), e[i]) - ys.begin();
    fi = lower_bound(all(ys), f[i]) - ys.begin();
    for (int j = ei; j < fi; j++) {
      r[di - 1][j] = 1;
      l[di][j] = 1;
    }
  }
  BFS(x, y);
  if (vis[0][0] or vis[xs.size() - 1][ys.size() - 1]) {
    cout << "INF"
         << "\\n";
    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 = (ans + (xs[i + 1] - xs[i]) * 1LL * (ys[j + 1] - ys[j]));
  }
  cout << ans << "\\n";
  return 0;
}