Problem p02680 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <stdio.h>
#include <vector>

using ll = long long int;
using P = std::pair<int, int>;
const int INF = (1 << 30);
const ll INFLL = (1ll << 60);
const ll MOD = (ll)(1e9 + 7);

#define l_ength size

void mul_mod(ll &a, ll b) {
  a *= b;
  a %= MOD;
}

void add_mod(ll &a, ll b) {
  a = (a < MOD) ? a : (a - MOD);
  b = (b < MOD) ? b : (b - MOD);
  a += b;
  a = (a < MOD) ? a : (a - MOD);
}

bool visited[4444][4444], obs[4444][4444];
int obsn[4444][4444];
std::set<ll> x, y;
std::vector<ll> u, v;
ll a[1111], b[1111], c[1111], d[1111], e[1111], f[1111];
std::queue<P> que;

int main(void) {
  int n, m, i, j, k, s = -1, t = -1, p, q, dx[4] = {-1, 0, 0, 1},
                     dy[4] = {0, -1, 1, 0};
  ll ans = 0ll;
  u.push_back(INF);
  u.push_back(-INF);
  u.push_back(INF);
  u.push_back(-INF);
  v.push_back(INF);
  v.push_back(-INF);
  v.push_back(INF);
  v.push_back(-INF);
  std::cin >> n >> m;
  for (i = 0; i < n; ++i) {
    std::cin >> a[i] >> b[i] >> c[i];
    if (!x.count(a[i])) {
      x.insert(a[i]);
      u.push_back(a[i]);
      u.push_back(a[i]);
    }
    if (!x.count(b[i])) {
      x.insert(b[i]);
      u.push_back(b[i]);
      u.push_back(b[i]);
    }
    if (!y.count(c[i])) {
      y.insert(c[i]);
      v.push_back(c[i]);
      v.push_back(c[i]);
    }
  }
  for (j = 0; j < m; ++j) {
    std::cin >> d[j] >> e[j] >> f[j];
    if (!x.count(d[j])) {
      x.insert(d[j]);
      u.push_back(d[j]);
      u.push_back(d[j]);
    }
    if (!y.count(e[j])) {
      y.insert(e[j]);
      v.push_back(e[j]);
      v.push_back(e[j]);
    }
    if (!y.count(f[j])) {
      y.insert(f[j]);
      v.push_back(f[j]);
      v.push_back(f[j]);
    }
  }
  std::sort(u.begin(), u.end());
  std::sort(v.begin(), v.end());
  s += u.l_ength();
  t += v.l_ength();
  for (k = 0; k < n; ++k) {
    p = (int)(std::lower_bound(u.begin(), u.end(), b[k]) - u.begin());
    q = (int)(std::lower_bound(v.begin(), v.end(), c[k]) - v.begin());
    for (i = (int)(std::lower_bound(u.begin(), u.end(), a[k]) - u.begin());
         i <= p; ++i) {
      obs[i][q] = true;
      obsn[i][q] = k;
    }
  }
  for (k = 0; k < m; ++k) {
    p = (int)(std::lower_bound(u.begin(), u.end(), d[k]) - u.begin());
    q = (int)(std::lower_bound(v.begin(), v.end(), f[k]) - v.begin());
    for (j = (int)(std::lower_bound(v.begin(), v.end(), e[k]) - v.begin());
         j <= q; ++j) {
      obs[p][j] = true;
      obsn[p][j] = k;
    }
  }
  for (i = 0; i < s; ++i) {
    if (!(u[i] <= 0 && 0 <= u[i + 1])) {
      continue;
    }
    for (j = 0; j < t; ++j) {
      if (v[j] <= 0 && 0 <= v[j + 1]) {
        que.push(P(i, j));
        while (!que.empty()) {
          p = que.front().first;
          q = que.front().second;
          que.pop();
          if (visited[p][q]) {
            continue;
          }
          visited[p][q] = true;
          for (k = 0; k < 4; ++k) {
            if (!(0 <= p + dx[k] && p + dx[k] < s)) {
              continue;
            }
            if (!(0 <= q + dy[k] && q + dy[k] < t)) {
              continue;
            }
            if (obs[p + dx[k]][q + dy[k]]) {
              continue;
            }
            if (visited[p + dx[k]][q + dy[k]]) {
              continue;
            }
            que.push(P(p + dx[k], q + dy[k]));
          }
        }
        break;
      }
    }
  }
  /*
  for(i=0; i<s; ++i){
          for(j=0; j<t; ++j){
                  if(obs[i][j]){
                          std::cout << obsn[i][j];
                  }else if(visited[i][j]){
                          std::cout << "#";
                  }else{
                          std::cout << " ";
                  }
          }
          std::cout << std::endl;
  }
  */
  for (i = 0; i < t; ++i) {
    if (visited[0][i] || visited[s - 1][i]) {
      std::cout << "INF" << std::endl;
      return 0;
    }
  }
  for (i = 0; i < s; ++i) {
    if (visited[i][0] || visited[i][t - 1]) {
      std::cout << "INF" << std::endl;
      return 0;
    }
  }
  for (i = 0; i < s; ++i) {
    for (j = 0; j < t; ++j) {
      if (visited[i][j]) {
        ans += (u[i + 1] - u[i]) * (v[j + 1] - v[j]);
      }
    }
  }
  std::cout << ans << std::endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <stdio.h>
#include <vector>

using ll = long long int;
using P = std::pair<int, int>;
const int INF = (1 << 30);
const ll INFLL = (1ll << 60);
const ll MOD = (ll)(1e9 + 7);

#define l_ength size

void mul_mod(ll &a, ll b) {
  a *= b;
  a %= MOD;
}

void add_mod(ll &a, ll b) {
  a = (a < MOD) ? a : (a - MOD);
  b = (b < MOD) ? b : (b - MOD);
  a += b;
  a = (a < MOD) ? a : (a - MOD);
}

bool visited[8888][8888], obs[8888][8888];
int obsn[8888][8888];
std::set<ll> x, y;
std::vector<ll> u, v;
ll a[1111], b[1111], c[1111], d[1111], e[1111], f[1111];
std::queue<P> que;

int main(void) {
  int n, m, i, j, k, s = -1, t = -1, p, q, dx[4] = {-1, 0, 0, 1},
                     dy[4] = {0, -1, 1, 0};
  ll ans = 0ll;
  u.push_back(INF);
  u.push_back(-INF);
  u.push_back(INF);
  u.push_back(-INF);
  v.push_back(INF);
  v.push_back(-INF);
  v.push_back(INF);
  v.push_back(-INF);
  std::cin >> n >> m;
  for (i = 0; i < n; ++i) {
    std::cin >> a[i] >> b[i] >> c[i];
    if (!x.count(a[i])) {
      x.insert(a[i]);
      u.push_back(a[i]);
      u.push_back(a[i]);
    }
    if (!x.count(b[i])) {
      x.insert(b[i]);
      u.push_back(b[i]);
      u.push_back(b[i]);
    }
    if (!y.count(c[i])) {
      y.insert(c[i]);
      v.push_back(c[i]);
      v.push_back(c[i]);
    }
  }
  for (j = 0; j < m; ++j) {
    std::cin >> d[j] >> e[j] >> f[j];
    if (!x.count(d[j])) {
      x.insert(d[j]);
      u.push_back(d[j]);
      u.push_back(d[j]);
    }
    if (!y.count(e[j])) {
      y.insert(e[j]);
      v.push_back(e[j]);
      v.push_back(e[j]);
    }
    if (!y.count(f[j])) {
      y.insert(f[j]);
      v.push_back(f[j]);
      v.push_back(f[j]);
    }
  }
  std::sort(u.begin(), u.end());
  std::sort(v.begin(), v.end());
  s += u.l_ength();
  t += v.l_ength();
  for (k = 0; k < n; ++k) {
    p = (int)(std::lower_bound(u.begin(), u.end(), b[k]) - u.begin());
    q = (int)(std::lower_bound(v.begin(), v.end(), c[k]) - v.begin());
    for (i = (int)(std::lower_bound(u.begin(), u.end(), a[k]) - u.begin());
         i <= p; ++i) {
      obs[i][q] = true;
      obsn[i][q] = k;
    }
  }
  for (k = 0; k < m; ++k) {
    p = (int)(std::lower_bound(u.begin(), u.end(), d[k]) - u.begin());
    q = (int)(std::lower_bound(v.begin(), v.end(), f[k]) - v.begin());
    for (j = (int)(std::lower_bound(v.begin(), v.end(), e[k]) - v.begin());
         j <= q; ++j) {
      obs[p][j] = true;
      obsn[p][j] = k;
    }
  }
  for (i = 0; i < s; ++i) {
    if (!(u[i] <= 0 && 0 <= u[i + 1])) {
      continue;
    }
    for (j = 0; j < t; ++j) {
      if (v[j] <= 0 && 0 <= v[j + 1]) {
        que.push(P(i, j));
        while (!que.empty()) {
          p = que.front().first;
          q = que.front().second;
          que.pop();
          if (visited[p][q]) {
            continue;
          }
          visited[p][q] = true;
          for (k = 0; k < 4; ++k) {
            if (!(0 <= p + dx[k] && p + dx[k] < s)) {
              continue;
            }
            if (!(0 <= q + dy[k] && q + dy[k] < t)) {
              continue;
            }
            if (obs[p + dx[k]][q + dy[k]]) {
              continue;
            }
            if (visited[p + dx[k]][q + dy[k]]) {
              continue;
            }
            que.push(P(p + dx[k], q + dy[k]));
          }
        }
        break;
      }
    }
  }
  /*
  for(i=0; i<s; ++i){
          for(j=0; j<t; ++j){
                  if(obs[i][j]){
                          std::cout << obsn[i][j];
                  }else if(visited[i][j]){
                          std::cout << "#";
                  }else{
                          std::cout << " ";
                  }
          }
          std::cout << std::endl;
  }
  */
  for (i = 0; i < t; ++i) {
    if (visited[0][i] || visited[s - 1][i]) {
      std::cout << "INF" << std::endl;
      return 0;
    }
  }
  for (i = 0; i < s; ++i) {
    if (visited[i][0] || visited[i][t - 1]) {
      std::cout << "INF" << std::endl;
      return 0;
    }
  }
  for (i = 0; i < s; ++i) {
    for (j = 0; j < t; ++j) {
      if (visited[i][j]) {
        ans += (u[i + 1] - u[i]) * (v[j + 1] - v[j]);
      }
    }
  }
  std::cout << ans << std::endl;
  return 0;
}