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

#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
  t = 0;
  char ch = getchar();
  int f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  do {
    (t *= 10) += ch - '0';
    ch = getchar();
  } while ('0' <= ch && ch <= '9');
  t *= f;
}
typedef long long ll;
const int INF = (1e9) + 10;
int N, M, n, m, dx[3010], dy[3010];
int a[1010], b[1010], c[1010], d[1010], e[1010], f[1010];
ll ans;
bool vis[3010][3010], heng[3010][3010], shu[3010][3010];
int Fx(int x) { return lower_bound(dx + 1, dx + n + 1, x) - dx; }
int Fy(int y) { return lower_bound(dy + 1, dy + m + 1, y) - dy; }
struct node {
  int x, y;
};
queue<node> q;
void insert(int x, int y) {
  if (vis[x][y])
    return;
  if (x == 1 || x == n || y == 1 || y == m) {
    printf("INF\\n");
    exit(0);
  }
  q.push((node){x, y});
  vis[x][y] = 1;
}
int main() {
  // freopen("1.txt","r",stdin);
  read(N);
  read(M);
  dx[++n] = 0, dy[++m] = 0;
  dx[++n] = -INF, dx[++n] = INF;
  dy[++m] = -INF, dy[++m] = INF;
  for (int i = 1; i <= N; i++) {
    read(a[i]);
    read(b[i]);
    read(c[i]);
    dx[++n] = a[i], dx[++n] = b[i], dy[++m] = c[i];
  }
  for (int i = 1; i <= M; i++) {
    read(d[i]);
    read(e[i]);
    read(f[i]);
    dx[++n] = d[i], dy[++m] = e[i], dy[++m] = f[i];
  }
  sort(dx + 1, dx + n + 1);
  sort(dy + 1, dy + m + 1);
  n = unique(dx + 1, dx + n + 1) - dx - 1;
  m = unique(dy + 1, dy + m + 1) - dy - 1;
  //	printf("%d %d\\n",n,m);
  for (int i = 1; i <= N; i++) {
    a[i] = Fx(a[i]), b[i] = Fx(b[i]), c[i] = Fy(c[i]);
    for (int j = a[i]; j < b[i]; j++)
      heng[j][c[i]] = 1;
  }
  for (int i = 1; i <= M; i++) {
    d[i] = Fx(d[i]), e[i] = Fy(e[i]), f[i] = Fy(f[i]);
    for (int j = e[i] + 1; j <= f[i]; j++)
      shu[d[i]][j] = 1;
  }
  int x = Fx(0), y = Fy(0);
  insert(x, y);
  while (!q.empty()) {
    x = q.front().x, y = q.front().y;
    q.pop();
    ans += (ll)(dx[x + 1] - dx[x]) * (dy[y] - dy[y - 1]);
    if (!heng[x][y])
      insert(x, y + 1);
    if (!heng[x][y - 1])
      insert(x, y - 1);
    if (!shu[x][y])
      insert(x - 1, y);
    if (!shu[x + 1][y])
      insert(x + 1, y);
  }
  printf("%lld\\n", ans);
  return 0;
}
/*
  0. Enough array size? Enough array size? Enough array size? Interger overflow?

  1. Think TWICE, Code ONCE!
  Are there any counterexamples to your algo?

  2. Be careful about the BOUNDARIES!
  N=1? P=1? Something about 0?

  3. Do not make STUPID MISTAKES!
  Time complexity? Memory usage? Precision error?
*/

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
  t = 0;
  char ch = getchar();
  int f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  do {
    (t *= 10) += ch - '0';
    ch = getchar();
  } while ('0' <= ch && ch <= '9');
  t *= f;
}
typedef long long ll;
const int INF = (1e9) + 10;
int N, M, n, m, dx[3010], dy[3010];
int a[1010], b[1010], c[1010], d[1010], e[1010], f[1010];
ll ans;
bool vis[3010][3010], heng[3010][3010], shu[3010][3010];
int Fx(int x) { return lower_bound(dx + 1, dx + n + 1, x) - dx; }
int Fy(int y) { return lower_bound(dy + 1, dy + m + 1, y) - dy; }
void dfs(int x, int y) {
  if (x <= 2 || x == n || y <= 2 || y == m) {
    printf("INF\\n");
    exit(0);
  }
  if (vis[x][y])
    return;
  vis[x][y] = 1;
  ans += (ll)(dx[x] - dx[x - 1]) * (dy[y] - dy[y - 1]);
  if (!heng[x][y])
    dfs(x, y + 1);
  if (!heng[x][y - 1])
    dfs(x, y - 1);
  if (!shu[x][y])
    dfs(x - 1, y);
  if (!shu[x + 1][y])
    dfs(x + 1, y);
}
int main() {
  // freopen("1.txt","r",stdin);
  read(N);
  read(M);
  dx[++n] = 0, dy[++m] = 0;
  dx[++n] = -INF, dx[++n] = INF;
  dy[++m] = -INF, dy[++m] = INF;
  for (int i = 1; i <= N; i++) {
    read(a[i]);
    read(b[i]);
    read(c[i]);
    dx[++n] = a[i], dx[++n] = b[i], dy[++m] = c[i];
  }
  for (int i = 1; i <= M; i++) {
    read(d[i]);
    read(e[i]);
    read(f[i]);
    dx[++n] = d[i], dy[++m] = e[i], dy[++m] = f[i];
  }
  sort(dx + 1, dx + n + 1);
  sort(dy + 1, dy + m + 1);
  n = unique(dx + 1, dx + n + 1) - dx - 1;
  m = unique(dy + 1, dy + m + 1) - dy - 1;
  for (int i = 1; i <= N; i++) {
    a[i] = Fx(a[i]), b[i] = Fx(b[i]), c[i] = Fy(c[i]);
    for (int j = a[i]; j < b[i]; j++)
      heng[j][c[i]] = 1;
  }
  for (int i = 1; i <= M; i++) {
    d[i] = Fx(d[i]), e[i] = Fy(e[i]), f[i] = Fy(f[i]);
    for (int j = e[i] + 1; j <= f[i]; j++)
      shu[d[i]][j] = 1;
  }
  int x = Fx(0), y = Fy(0);
  dfs(x, y);
  printf("%lld\\n", ans);
  return 0;
}
/*
  0. Enough array size? Enough array size? Enough array size? Interger overflow?

  1. Think TWICE, Code ONCE!
  Are there any counterexamples to your algo?

  2. Be careful about the BOUNDARIES!
  N=1? P=1? Something about 0?

  3. Do not make STUPID MISTAKES!
  Time complexity? Memory usage? Precision error?
*/