Problem p02680 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for (llint(x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for (llint(x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define mod 1000000007

using namespace std;
typedef pair<llint, llint> P;

llint n, m;
llint u[1005], d[1005], x[1005];
llint l[1005], r[1005], y[1005];
llint h, w;
bool wall[6005][6005], used[6005][6005];
vector<llint> compX, compY;
llint dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

void comp(vector<llint> &vec) {
  sort(vec.begin(), vec.end());
  vec.erase(unique(vec.begin(), vec.end()), vec.end());
}

llint get(vector<llint> &vec, llint x) {
  return lower_bound(vec.begin(), vec.end(), x) - vec.begin();
}

void dfs(int x, int y) {
  used[x][y] = true;
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx < 0 || nx >= w || ny < 0 || ny >= h)
      continue;
    if (used[nx][ny] || wall[nx][ny])
      continue;
    dfs(nx, ny);
  }
}

int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    cin >> u[i] >> d[i] >> x[i];
  for (int i = 1; i <= m; i++)
    cin >> y[i] >> l[i] >> r[i];

  compX.push_back(-2e9);
  compX.push_back(2e9);
  compY.push_back(-2e9);
  compY.push_back(2e9);

  for (int i = 1; i <= n; i++) {
    compX.push_back(x[i]);
    compY.push_back(u[i]);
    compY.push_back(d[i]);
  }
  for (int i = 1; i <= m; i++) {
    compY.push_back(y[i]);
    compX.push_back(l[i]);
    compX.push_back(r[i]);
  }
  comp(compX), comp(compY);
  w = compX.size() * 2, h = compY.size() * 2;

  for (int i = 1; i <= n; i++) {
    llint U = get(compY, u[i]) * 2, D = get(compY, d[i]) * 2,
          X = get(compX, x[i]) * 2;
    for (int j = U; j <= D; j++)
      wall[X][j] = true;
  }

  for (int i = 1; i <= m; i++) {
    llint L = get(compX, l[i]) * 2, R = get(compX, r[i]) * 2,
          Y = get(compY, y[i]) * 2;
    for (int j = L; j <= R; j++)
      wall[j][Y] = true;
  }

  /*for(int y = 0; y < h; y++){
          for(int x = 0; x < w; x++){
                  cout << wall[x][y];
          }
          cout << endl;
  }*/

  llint sx = get(compX, 0) * 2 - 1, sy = get(compY, 0) * 2 - 1;

  dfs(sx, sy);

  llint ans = 0;
  for (int x = 0; x < w; x++) {
    for (int y = 0; y < h; y++) {
      if (!used[x][y])
        continue;
      if (x == 0 || x >= w - 2 || y == 0 || y >= h - 2) {
        cout << "INF" << endl;
        return 0;
      }
      if (x % 2 == 0 || y % 2 == 0)
        continue;
      llint X = x / 2, Y = y / 2;
      if (X + 1 >= compX.size() || Y + 1 >= compY.size()) {
        cout << "INF" << endl;
        return 0;
      }
      ans += (compX[X + 1] - compX[X]) * (compY[Y + 1] - compY[Y]);
    }
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for (llint(x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for (llint(x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define mod 1000000007

using namespace std;
typedef pair<llint, llint> P;

llint n, m;
llint u[1005], d[1005], x[1005];
llint l[1005], r[1005], y[1005];
llint h, w;
bool wall[6005][6005], used[6005][6005];
vector<llint> compX, compY;
llint dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

void comp(vector<llint> &vec) {
  sort(vec.begin(), vec.end());
  vec.erase(unique(vec.begin(), vec.end()), vec.end());
}

llint get(vector<llint> &vec, llint x) {
  return lower_bound(vec.begin(), vec.end(), x) - vec.begin();
}

void dfs(int x, int y) {
  used[x][y] = true;
  queue<P> Q;
  Q.push(P(x, y));

  while (Q.size()) {
    x = Q.front().first, y = Q.front().second;
    Q.pop();
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx < 0 || nx >= w || ny < 0 || ny >= h)
        continue;
      if (used[nx][ny] || wall[nx][ny])
        continue;
      used[nx][ny] = true;
      Q.push(P(nx, ny));
    }
  }
}

int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    cin >> u[i] >> d[i] >> x[i];
  for (int i = 1; i <= m; i++)
    cin >> y[i] >> l[i] >> r[i];

  compX.push_back(-2e9);
  compX.push_back(2e9);
  compY.push_back(-2e9);
  compY.push_back(2e9);

  for (int i = 1; i <= n; i++) {
    compX.push_back(x[i]);
    compY.push_back(u[i]);
    compY.push_back(d[i]);
  }
  for (int i = 1; i <= m; i++) {
    compY.push_back(y[i]);
    compX.push_back(l[i]);
    compX.push_back(r[i]);
  }
  comp(compX), comp(compY);
  w = compX.size() * 2, h = compY.size() * 2;

  for (int i = 1; i <= n; i++) {
    llint U = get(compY, u[i]) * 2, D = get(compY, d[i]) * 2,
          X = get(compX, x[i]) * 2;
    for (int j = U; j <= D; j++)
      wall[X][j] = true;
  }

  for (int i = 1; i <= m; i++) {
    llint L = get(compX, l[i]) * 2, R = get(compX, r[i]) * 2,
          Y = get(compY, y[i]) * 2;
    for (int j = L; j <= R; j++)
      wall[j][Y] = true;
  }

  /*for(int y = 0; y < h; y++){
          for(int x = 0; x < w; x++){
                  cout << wall[x][y];
          }
          cout << endl;
  }*/

  llint sx = get(compX, 0) * 2 - 1, sy = get(compY, 0) * 2 - 1;

  dfs(sx, sy);

  llint ans = 0;
  for (int x = 0; x < w; x++) {
    for (int y = 0; y < h; y++) {
      if (!used[x][y])
        continue;
      if (x == 0 || x >= w - 2 || y == 0 || y >= h - 2) {
        cout << "INF" << endl;
        return 0;
      }
      if (x % 2 == 0 || y % 2 == 0)
        continue;
      llint X = x / 2, Y = y / 2;
      if (X + 1 >= compX.size() || Y + 1 >= compY.size()) {
        cout << "INF" << endl;
        return 0;
      }
      ans += (compX[X + 1] - compX[X]) * (compY[Y + 1] - compY[Y]);
    }
  }
  cout << ans << endl;

  return 0;
}