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

// Sky's the limit :)
#include <bits/stdc++.h>
using namespace std;
#define int long long

const int N = 4005;
const int INF = 1e10 + 5;

int n, m;
int a[N], b[N], c[N], d[N], e[N], f[N];
vector<int> vx{-INF, INF}, vy{-INF, INF};
bool bu[N][N], bd[N][N], bl[N][N], br[N][N], vis[N][N];

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

int nx, ny;

bool check(int x, int y) {
  return x >= 0 && x < nx - 1 && y >= 0 && y < ny - 1 && !vis[x][y];
}

void bfs(int x, int y) {
  queue<pair<int, int>> q;
  vis[x][y] = true;
  q.push({x, y});
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    int x = p.first;
    int y = p.second;

    // cout << x << ' ' << y << ' ' << bu[x][y] << ' ' << bd[x][y] << ' ' <<
    // br[x][y] << ' ' << bl[x][y] << '\\n';

    for (int i = 0; i < 4; i++) {
      if ((i == 0 && bu[x][y]) || (i == 1 && bd[x][y]) ||
          (i == 2 && br[x][y]) || (i == 3 && bl[x][y]))
        continue;

      int xn = x + dx[i];
      int yn = y + dy[i];
      if (check(xn, yn)) {
        vis[xn][yn] = true;
        q.push({xn, yn});
      }
    }
  }
}

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int T = 1;
  // cin >> T;
  while (T--) {
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
      cin >> a[i] >> b[i] >> c[i];
      vx.push_back(a[i]);
      vx.push_back(b[i]);
      vy.push_back(c[i]);
    }
    for (int i = 0; i < m; i++) {
      cin >> d[i] >> e[i] >> f[i];
      vx.push_back(d[i]);
      vy.push_back(e[i]);
      vy.push_back(f[i]);
    }

    sort(vx.begin(), vx.end());
    sort(vy.begin(), vy.end());
    vx.resize(unique(vx.begin(), vx.end()) - vx.begin());
    vy.resize(unique(vy.begin(), vy.end()) - vy.begin());

    // for(auto x : vx)
    //     cout << x << ' ';
    // cout << '\\n';
    // for(auto y : vy)
    //     cout << y << ' ';
    // cout << '\\n';

    for (int i = 0; i < n; i++) {
      int la = lower_bound(vx.begin(), vx.end(), a[i]) - vx.begin();
      int lb = lower_bound(vx.begin(), vx.end(), b[i]) - vx.begin();
      int lc = lower_bound(vy.begin(), vy.end(), c[i]) - vy.begin();
      // cout << i << ": " << la << ' ' << lb << ' ' << lc << '\\n';
      for (int j = la; j < lb; j++)
        bu[j][lc - 1] = bd[j][lc] = true;
    }
    for (int i = 0; i < m; i++) {
      int ld = lower_bound(vx.begin(), vx.end(), d[i]) - vx.begin();
      int le = lower_bound(vy.begin(), vy.end(), e[i]) - vy.begin();
      int lf = lower_bound(vy.begin(), vy.end(), f[i]) - vy.begin();
      // cout << i << ": " << ld << ' ' << le << ' ' << lf << '\\n';
      for (int j = le; j < lf; j++)
        br[ld - 1][j] = bl[ld][j] = true;
    }

    nx = vx.size();
    ny = vy.size();
    // cout << nx << ' ' << ny << '\\n';
    int x = --lower_bound(vx.begin(), vx.end(), 0) - vx.begin();
    int y = --lower_bound(vy.begin(), vy.end(), 0) - vy.begin();

    bfs(x, y);
    // cout << x << ' ' << y << '\\n';

    if (vis[0][0] || vis[nx - 1][ny - 1])
      return cout << "INF\\n", 0;

    int ans = 0;
    for (int i = 0; i < nx - 1; i++) {
      for (int j = 0; j < ny - 1; j++) {
        if (vis[i][j])
          ans += (vx[i + 1] - vx[i]) * (vy[j + 1] - vy[j]);
      }
    }
    cout << ans << '\\n';
  }

  return 0;
}


Error: WA

Fixed submission:

// Sky's the limit :)
#include <bits/stdc++.h>
using namespace std;
#define int long long

const int N = 2005;
const int INF = 1e10 + 5;

int n, m;
int a[N], b[N], c[N], d[N], e[N], f[N];
vector<int> vx{-INF, INF}, vy{-INF, INF};
bool bu[N][N], bd[N][N], bl[N][N], br[N][N], vis[N][N];

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

bool check(int x, int y) {
  return x >= 0 && x < n && y >= 0 && y < n && !vis[x][y];
}

void bfs(int x, int y) {
  queue<pair<int, int>> q;
  vis[x][y] = true;
  q.push({x, y});
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    int x = p.first;
    int y = p.second;

    // cout << x << ' ' << y << '\\n';

    for (int i = 0; i < 4; i++) {
      if ((i == 0 && bu[x][y]) || (i == 1 && bd[x][y]) ||
          (i == 2 && br[x][y]) || (i == 3 && bl[x][y]))
        continue;

      int xn = x + dx[i];
      int yn = y + dy[i];
      if (check(xn, yn)) {
        vis[xn][yn] = true;
        q.push({xn, yn});
      }
    }
  }
}

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int T = 1;
  // cin >> T;
  while (T--) {
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
      cin >> a[i] >> b[i] >> c[i];
      vx.push_back(a[i]);
      vx.push_back(b[i]);
      vy.push_back(c[i]);
    }
    for (int i = 0; i < m; i++) {
      cin >> d[i] >> e[i] >> f[i];
      vx.push_back(d[i]);
      vy.push_back(e[i]);
      vy.push_back(f[i]);
    }

    sort(vx.begin(), vx.end());
    sort(vy.begin(), vy.end());
    vx.resize(unique(vx.begin(), vx.end()) - vx.begin());
    vy.resize(unique(vy.begin(), vy.end()) - vy.begin());

    for (int i = 0; i < n; i++) {
      int la = lower_bound(vx.begin(), vx.end(), a[i]) - vx.begin();
      int lb = lower_bound(vx.begin(), vx.end(), b[i]) - vx.begin();
      int lc = lower_bound(vy.begin(), vy.end(), c[i]) - vy.begin();
      for (int j = la; j < lb; j++)
        bu[j][lc - 1] = bd[j][lc] = true;
    }
    for (int i = 0; i < m; i++) {
      int ld = lower_bound(vx.begin(), vx.end(), d[i]) - vx.begin();
      int le = lower_bound(vy.begin(), vy.end(), e[i]) - vy.begin();
      int lf = lower_bound(vy.begin(), vy.end(), f[i]) - vy.begin();
      for (int j = le; j < lf; j++)
        br[ld - 1][j] = bd[ld][j] = true;
    }

    int x = --lower_bound(vx.begin(), vx.end(), 0) - vx.begin();
    int y = --lower_bound(vy.begin(), vy.end(), 0) - vy.begin();
    bfs(x, y);
    cerr << x << ' ' << y << '\\n';

    int nx = vx.size();
    int ny = vy.size();
    cerr << nx << ' ' << ny << '\\n';
    if (vis[0][0] || vis[nx - 1][ny - 1])
      return cout << "INF\\n", 0;

    int ans = 0;
    for (int i = 0; i < nx - 1; i++) {
      for (int j = 0; j < ny - 1; j++) {
        if (vis[i][j])
          ans += (vx[i + 1] - vx[i]) * (vy[j + 1] - vy[j]);
      }
    }
    cout << ans << '\\n';
  }

  return 0;
}