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

#pragma GCC optimize("Ofast")
#include <algorithm>
#include <queue>
#include <stdio.h>
#include <utility>
#include <vector>
using namespace std;
long long a[1005], b[1005], c[1005], d[1005], e[1005], f[1005];
bool used[3005][3005] = {0}, up[3005][3005] = {0}, down[3005][3005] = {0},
     left[3005][3005] = {0}, right[3005][3005] = {0};
vector<long long> x, y;
queue<pair<long long, long long>> que;
long long id_x(long long v) {
  return lower_bound(x.begin(), x.end(), v) - x.begin();
}
long long id_y(long long v) {
  return lower_bound(y.begin(), y.end(), v) - y.begin();
}
int dir[][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int main() {
  x.emplace_back(100000000000000000);
  x.emplace_back(-100000000000000000);
  y.emplace_back(100000000000000000);
  y.emplace_back(-100000000000000000);
  long long i, j, n, m;
  scanf("%lld%lld", &n, &m);
  for (i = 0; i < n; i++) {
    scanf("%lld%lld%lld", &a[i], &b[i], &c[i]);
    x.emplace_back(a[i]);
    x.emplace_back(b[i]);
    y.emplace_back(c[i]);
  }
  for (i = 0; i < m; i++) {
    scanf("%lld%lld%lld", &d[i], &e[i], &f[i]);
    x.emplace_back(d[i]);
    y.emplace_back(e[i]);
    y.emplace_back(f[i]);
  }
  sort(x.begin(), x.end());
  sort(y.begin(), y.end());
  x.erase(unique(x.begin(), x.end()), x.end());
  y.erase(unique(y.begin(), y.end()), y.end());
  // printf("%d %d\\n",id_x(0),id_y(0));
  //	printf("%d %d\\n",x.size(),y.size());
  for (i = 0; i < n; i++) {
    a[i] = id_x(a[i]);
    b[i] = id_x(b[i]);
    c[i] = id_y(c[i]);
    for (j = a[i]; j < b[i]; j++) {
      up[j][c[i] - 1] = 1;
      down[j][c[i]] = 1;
    }
  }
  for (i = 0; i < m; i++) {
    d[i] = id_x(d[i]);
    e[i] = id_y(e[i]);
    f[i] = id_y(f[i]);
    for (j = e[i]; j < f[i]; j++) {
      right[d[i] - 1][j] = 1;
      left[d[i]][j] = 1;
    }
  }
  que.push({id_x(0) - 1, id_y(0) - 1});
  used[id_x(0) - 1][id_y(0) - 1] = 1;
  while (que.size()) {
    auto z = que.front();
    que.pop();
    //	printf("**%lld %lld\\n",z.first,z.second);
    for (i = 0; i < 4; i++) {
      if (i == 0 && up[z.first][z.second])
        continue;
      if (i == 1 && down[z.first][z.second])
        continue;
      if (i == 2 && right[z.first][z.second])
        continue;
      if (i == 3 && left[z.first][z.second])
        continue;
      long long r = z.first + dir[i][0], c = z.second + dir[i][1];
      //	printf("%lld %lld %lld %lld
      //%lld\\n",r,c,x.size(),y.size(),used[r][c]);
      if (r < 0 || c < 0 || r >= (int)x.size() - 1 || c >= (int)y.size() - 1 ||
          used[r][c])
        continue;
      //	printf("%d %d\\n",r,c);
      used[r][c] = 1;
      que.push({r, c});
    }
  }
  for (i = 0; i < (int)x.size(); i++) {
    if (used[i][0] || used[i][y.size() - 1]) {
      puts("INF");
      return 0;
    }
  }
  for (i = 0; i < (int)y.size(); i++) {
    if (used[0][i] || used[x.size() - 1][i]) {
      puts("INF");
    }
  }
  long long ans = 0;
  for (i = 0; i < (int)x.size() - 1; i++) {
    for (j = 0; j < (int)y.size() - 1; j++) {
      //	printf("%d %d %d\\n",i,j,used[i][j]);
      //	printf("%lld %lld %lld %lld\\n",x[i],x[i+1],y[j],y[j+1]);
      //	printf("%d")
      if (used[i][j]) {
        ans += (x[i + 1] - x[i]) * (y[j + 1] - y[j]);
        //		printf("%lld\\n",(x[i+1]-x[i])*(y[j+1]-y[j]));
        // printf("%lld %lld\\n",i,j);
      }
    }
  }
  printf("%lld\\n", ans);
}


Error: WA

Fixed submission:

#pragma GCC optimize("Ofast")
#include <algorithm>
#include <queue>
#include <stdio.h>
#include <utility>
#include <vector>
using namespace std;
long long a[1005], b[1005], c[1005], d[1005], e[1005], f[1005],
    up[1005][1005] = {0}, down[1005][1005] = {0}, left[1005][1005] = {0},
    right[1005][1005] = {0}, dir[5] = {-1, 0, 1, 0, -1};
bool used[1005][1005] = {0};
vector<long long> x, y;
queue<pair<long long, long long>> que;
long long id_x(long long v) {
  return lower_bound(x.begin(), x.end(), v) - x.begin() + 1;
}
long long id_y(long long v) {
  return lower_bound(y.begin(), y.end(), v) - y.begin() + 1;
}
int main() {
  long long i, j, n, m;
  scanf("%lld%lld", &n, &m);
  for (i = 0; i < n; i++) {
    scanf("%lld%lld%lld", &a[i], &b[i], &c[i]);
    x.emplace_back(a[i]);
    x.emplace_back(b[i]);
    y.emplace_back(c[i]);
  }
  for (i = 0; i < m; i++) {
    scanf("%lld%lld%lld", &d[i], &e[i], &f[i]);
    x.emplace_back(d[i]);
    y.emplace_back(e[i]);
    y.emplace_back(f[i]);
  }
  x.emplace_back(0);
  y.emplace_back(0);
  sort(x.begin(), x.end());
  sort(y.begin(), y.end());
  x.erase(unique(x.begin(), x.end()), x.end());
  y.erase(unique(y.begin(), y.end()), y.end());
  for (i = 0; i < n; i++) {
    a[i] = id_x(a[i]);
    b[i] = id_x(b[i]);
    c[i] = id_y(c[i]);
    for (j = a[i]; j < b[i]; j++) {
      up[j][c[i] - 1] = 1;
      down[j][c[i]] = 1;
    }
  }
  for (i = 0; i < m; i++) {
    d[i] = id_x(d[i]);
    e[i] = id_y(e[i]);
    f[i] = id_y(f[i]);
    for (j = e[i]; j < f[i]; j++) {
      right[d[i] - 1][j] = 1;
      left[d[i]][j] = 1;
    }
  }
  que.push({id_x(0), id_y(0)});
  used[id_x(0)][id_y(0)] = 1;
  while (que.size()) {
    auto z = que.front();
    que.pop();
    for (i = 0; i < 4; i++) {
      long long r = z.first + dir[i], c = z.second + dir[i + 1];
      //	printf("%lld %lld %lld %lld
      //%lld\\n",r,c,x.size(),y.size(),used[r][c]);
      if (r <= 0 || c <= 0 || r > (int)x.size() || c > (int)y.size() ||
          used[r][c] || (!i && left[r][c]) || (i == 1 && up[r][c]) ||
          (i == 2 && right[r][c]) || (i == 3 && down[r][c]))
        continue;
      used[r][c] = 1;
      que.push({r, c});
    }
  }
  for (i = 1; i <= (int)x.size(); i++) {
    if (used[i][1] || used[i][y.size()]) {
      puts("INF");
      return 0;
    }
  }
  for (i = 1; i <= (int)y.size(); i++) {
    if (used[1][i] || used[x.size()][i]) {
      puts("INF");
      return 0;
    }
  }
  long long ans = 0;
  for (i = 1; i < (int)x.size(); i++) {
    for (j = 1; j < (int)y.size(); j++) {
      //	printf("%d")
      if (used[i][j]) {
        ans += (x[i + 1] - x[i]) * (y[j + 1] - y[j]);
        // printf("%lld %lld\\n",i,j);
      }
    }
  }
  printf("%lld\\n", ans);
}