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

// READ & UNDERSTAND
// ll, int overflow, array bounds, memset(0)
// special cases (n=1?), n+1 (1-index)
// do smth instead of nothing & stay organized
// WRITE STUFF DOWN

#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define MOD 1000000007

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef long double ld;

ll INF = LLONG_MAX;

int const mxn = 4444;
int n, m;
int a[mxn], b[mxn], c[mxn], d[mxn], e[mxn], f[mxn];
bool vis[mxn][mxn], up[mxn][mxn], dw[mxn][mxn], le[mxn][mxn],
    ri[mxn][mxn]; // blockage

int const dx[] = {-1, 0, 1, 0};
int const dy[] = {0, -1, 0, 1};
vi xx = {-MOD, MOD}, yy = {-MOD, MOD};

void bfs(int x, int y) {
  queue<ii> q;
  q.emplace(x, y);
  vis[x][y] = 1;
  while (!q.empty()) {
    x = q.front().fi;
    y = q.front().se;
    q.pop();
    for (int i = 0; i < 4; i++) {
      if (i == 0 && le[x][y])
        continue;
      if (i == 1 && dw[x][y])
        continue;
      if (i == 2 && ri[x][y])
        continue;
      if (i == 3 && up[x][y])
        continue;
      int X = x + dx[i];
      int Y = y + dy[i];
      if (X < 0 || X >= (int)xx.size() - 1)
        continue;
      if (Y < 0 || Y >= (int)yy.size() - 1)
        continue;
      if (vis[X][Y])
        continue;
      vis[X][Y] = 1;
      q.emplace(X, Y);
    }
  }
  return;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
  cin >> n >> m;
  memset(vis, 0, sizeof(vis));
  memset(up, 0, sizeof(up));
  memset(dw, 0, sizeof(dw));
  memset(le, 0, sizeof(le));
  memset(ri, 0, sizeof(ri));
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    xx.pb(a[i]);
    xx.pb(b[i]);
    yy.pb(c[i]);
  }
  for (int i = 0; i < m; i++) {
    cin >> d[i] >> e[i] >> f[i];
    xx.pb(d[i]);
    yy.pb(e[i]);
    yy.pb(f[i]);
  }
  sort(all(xx));
  xx.erase(unique(all(xx)), xx.end());
  sort(all(yy));
  yy.erase(unique(all(yy)), yy.end());
  // setting up blockage
  for (int i = 0; i < n; i++) {
    int x1 = lower_bound(all(xx), a[i]) - xx.begin();
    int x2 = lower_bound(all(xx), b[i]) - xx.begin();
    int y = lower_bound(all(yy), c[i]) - yy.begin();
    // at y x1 --- x2
    for (int j = x1; j < x2; j++) {
      up[j][y - 1] = 1;
      dw[j][y] = 1;
    }
  }
  for (int i = 0; i < m; i++) {
    int x = lower_bound(all(xx), d[i]) - xx.begin();
    int y1 = lower_bound(all(yy), e[i]) - yy.begin();
    int y2 = lower_bound(all(yy), f[i]) - yy.begin();
    /* at x
            y1
            |
            |
            |
            y2
    */
    for (int j = y1; j < y2; j++) {
      ri[x - 1][j] = 1;
      le[x][j] = 1;
    }
  }
  int x = lower_bound(all(xx), 0) - xx.begin() - 1;
  int y = lower_bound(all(yy), 0) - yy.begin() - 1;
  bfs(x, y);
  if (vis[0][0] || vis[xx.size() - 1][yy.size() - 1]) {
    cout << "INF" << endl;
    return 0;
  }
  ll ans = 0LL;
  for (int i = 0; i < (int)xx.size() - 1; i++)
    for (int j = 0; j < (int)yy.size() - 1; j++)
      if (vis[i][j])
        ans += ll(xx[i + 1] - xx[i]) * (yy[j + 1] - yy[j]);
  cout << ans << endl;
}


Error: 0

Fixed submission:

// READ & UNDERSTAND
// ll, int overflow, array bounds, memset(0)
// special cases (n=1?), n+1 (1-index)
// do smth instead of nothing & stay organized
// WRITE STUFF DOWN

#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define MOD 1000000007

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef long double ld;

ll INF = LLONG_MAX;

int const mxn = 1111;
int n, m;
int a[mxn], b[mxn], c[mxn], d[mxn], e[mxn], f[mxn];
bool vis[mxn][mxn], up[mxn][mxn], dw[mxn][mxn], le[mxn][mxn],
    ri[mxn][mxn]; // blockage

int const dx[] = {-1, 0, 1, 0};
int const dy[] = {0, -1, 0, 1};
vi xx = {-MOD, MOD}, yy = {-MOD, MOD};

void bfs(int x, int y) {
  queue<ii> q;
  q.emplace(x, y);
  vis[x][y] = 1;
  while (!q.empty()) {
    x = q.front().fi;
    y = q.front().se;
    q.pop();
    for (int i = 0; i < 4; i++) {
      if (i == 0 && le[x][y])
        continue;
      if (i == 1 && dw[x][y])
        continue;
      if (i == 2 && ri[x][y])
        continue;
      if (i == 3 && up[x][y])
        continue;
      int X = x + dx[i];
      int Y = y + dy[i];
      if (X < 0 || X >= (int)xx.size() - 1)
        continue;
      if (Y < 0 || Y >= (int)yy.size() - 1)
        continue;
      if (vis[X][Y])
        continue;
      vis[X][Y] = 1;
      q.emplace(X, Y);
    }
  }
  return;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
  cin >> n >> m;
  memset(vis, 0, sizeof(vis));
  memset(up, 0, sizeof(up));
  memset(dw, 0, sizeof(dw));
  memset(le, 0, sizeof(le));
  memset(ri, 0, sizeof(ri));
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    xx.pb(a[i]);
    xx.pb(b[i]);
    yy.pb(c[i]);
  }
  for (int i = 0; i < m; i++) {
    cin >> d[i] >> e[i] >> f[i];
    xx.pb(d[i]);
    yy.pb(e[i]);
    yy.pb(f[i]);
  }
  sort(all(xx));
  xx.erase(unique(all(xx)), xx.end());
  sort(all(yy));
  yy.erase(unique(all(yy)), yy.end());
  // setting up blockage
  for (int i = 0; i < n; i++) {
    int x1 = lower_bound(all(xx), a[i]) - xx.begin();
    int x2 = lower_bound(all(xx), b[i]) - xx.begin();
    int y = lower_bound(all(yy), c[i]) - yy.begin();
    // at y x1 --- x2
    for (int j = x1; j < x2; j++) {
      up[j][y - 1] = 1;
      dw[j][y] = 1;
    }
  }
  for (int i = 0; i < m; i++) {
    int x = lower_bound(all(xx), d[i]) - xx.begin();
    int y1 = lower_bound(all(yy), e[i]) - yy.begin();
    int y2 = lower_bound(all(yy), f[i]) - yy.begin();
    /* at x
            y1
            |
            |
            |
            y2
    */
    for (int j = y1; j < y2; j++) {
      ri[x - 1][j] = 1;
      le[x][j] = 1;
    }
  }
  int x = lower_bound(all(xx), 0) - xx.begin() - 1;
  int y = lower_bound(all(yy), 0) - yy.begin() - 1;
  bfs(x, y);
  if (vis[0][0] || vis[xx.size() - 1][yy.size() - 1]) {
    cout << "INF" << endl;
    return 0;
  }
  ll ans = 0LL;
  for (int i = 0; i < (int)xx.size() - 1; i++)
    for (int j = 0; j < (int)yy.size() - 1; j++)
      if (vis[i][j])
        ans += ll(xx[i + 1] - xx[i]) * (yy[j + 1] - yy[j]);
  cout << ans << endl;
}