Problem p02680 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(it, c)                                                             \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

struct Area {
  ll area;
  bool visited;
  bool cango[4];
  Area() {
    area = 0;
    visited = false;
    rep(i, 4) cango[i] = true;
  }
};

// down, up, right, left
int vx[4] = {1, -1, 0, 0};
int vy[4] = {0, 0, 1, -1};

Area field[2005][2005];

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<ll>> v, w;

  map<ll, int> zx, zy;
  zx[-1000000005] = 1;
  zx[0] = 1;
  zx[1000000005] = 1;
  zy[-1000000005] = 1;
  zy[0] = 1;
  zy[1000000005] = 1;

  rep(i, N) {
    ll a, b, c;
    cin >> a >> b >> c;
    v.push_back({a, b, c});
    zx[a] = 1;
    zx[b] = 1;
    zy[c] = 1;
  }
  rep(i, M) {
    ll d, e, f;
    cin >> d >> e >> f;
    w.push_back({d, e, f});
    zx[d] = 1;
    zy[e] = 1;
    zy[f] = 1;
  }

  map<int, ll> rzx, rzy;
  int height = 0, width = 0;
  {FOR(it, zx){rzx[height] = it->first;
  it->second = height++;
}
}
{
  FOR(it, zy) {
    rzy[width] = it->first;
    it->second = width++;
  }
}

rep(i, height) {
  rep(j, width) {
    if (i == 0 || j == 0 || i == height - 1 || j == width - 1) {
      field[i][j].area = -1;
    } else {
      ll x = rzx[i];
      ll y = rzy[j];
      ll xx = rzx[i + 1];
      ll yy = rzy[j + 1];
      field[i][j].area = (xx - x) * (yy - y);
    }
  }
}

rep(i, N) {
  int x = zx[v[i][0]];
  int xx = zx[v[i][1]];
  int y = zy[v[i][2]];
  REP(j, x, xx) {
    if (y - 1 >= 0)
      field[j][y - 1].cango[2] = false;
    field[j][y].cango[3] = false;
  }
}
rep(i, M) {
  int x = zx[w[i][0]];
  int y = zy[w[i][1]];
  int yy = zy[w[i][2]];
  REP(j, y, yy) {
    if (x - 1 >= 0)
      field[x - 1][j].cango[0] = false;
    field[x][j].cango[1] = false;
  }
}

queue<pair<int, int>> que;
int zero_x = zx[0], zero_y = zy[0];
que.push(make_pair(zero_x - 1, zero_y - 1));
que.push(make_pair(zero_x - 1, zero_y));
que.push(make_pair(zero_x, zero_y - 1));
que.push(make_pair(zero_x, zero_y));

ll ret = 0;
while (!que.empty()) {
  /*
  rep(i,height){
    rep(j,width){
      if(field[i][j].visited) cout << "*";
      else cout << ".";
    }
    cout << endl;
  }
  cout << endl;
    */

  pair<int, int> p = que.front();
  que.pop();
  if (p.first < 0 || p.second < 0) {
    ret = -1;
    break;
  }
  if (field[p.first][p.second].visited)
    continue;
  field[p.first][p.second].visited = true;
  if (field[p.first][p.second].area < 0) {
    ret = -1;
    break;
  }
  ret += field[p.first][p.second].area;
  rep(k, 4) {
    int nx = p.first + vx[k];
    int ny = p.second + vy[k];
    if (nx < 0 || ny < 0)
      continue;
    if (field[p.first][p.second].cango[k]) {
      if (!field[nx][ny].visited) {
        que.push(make_pair(nx, ny));
      }
    }
  }
}

if (ret < 0) {
  cout << "INF" << endl;
} else {
  cout << ret << endl;
}

return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(it, c)                                                             \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

struct Area {
  ll area;
  bool visited;
  bool cango[4];
  Area() {
    area = 0;
    visited = false;
    rep(i, 4) cango[i] = true;
  }
};

// down, up, right, left
int vx[4] = {1, -1, 0, 0};
int vy[4] = {0, 0, 1, -1};

Area field[3005][3005];

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<ll>> v, w;

  map<ll, int> zx, zy;
  zx[-1000000005] = 1;
  zx[0] = 1;
  zx[1000000005] = 1;
  zy[-1000000005] = 1;
  zy[0] = 1;
  zy[1000000005] = 1;

  rep(i, N) {
    ll a, b, c;
    cin >> a >> b >> c;
    v.push_back({a, b, c});
    zx[a] = 1;
    zx[b] = 1;
    zy[c] = 1;
  }
  rep(i, M) {
    ll d, e, f;
    cin >> d >> e >> f;
    w.push_back({d, e, f});
    zx[d] = 1;
    zy[e] = 1;
    zy[f] = 1;
  }

  map<int, ll> rzx, rzy;
  int height = 0, width = 0;
  {FOR(it, zx){rzx[height] = it->first;
  it->second = height++;
}
}
{
  FOR(it, zy) {
    rzy[width] = it->first;
    it->second = width++;
  }
}

rep(i, height) {
  rep(j, width) {
    if (i == 0 || j == 0 || i == height - 1 || j == width - 1) {
      field[i][j].area = -1;
    } else {
      ll x = rzx[i];
      ll y = rzy[j];
      ll xx = rzx[i + 1];
      ll yy = rzy[j + 1];
      field[i][j].area = (xx - x) * (yy - y);
    }
  }
}

rep(i, N) {
  int x = zx[v[i][0]];
  int xx = zx[v[i][1]];
  int y = zy[v[i][2]];
  REP(j, x, xx) {
    if (y - 1 >= 0)
      field[j][y - 1].cango[2] = false;
    field[j][y].cango[3] = false;
  }
}
rep(i, M) {
  int x = zx[w[i][0]];
  int y = zy[w[i][1]];
  int yy = zy[w[i][2]];
  REP(j, y, yy) {
    if (x - 1 >= 0)
      field[x - 1][j].cango[0] = false;
    field[x][j].cango[1] = false;
  }
}

queue<pair<int, int>> que;
int zero_x = zx[0], zero_y = zy[0];
que.push(make_pair(zero_x - 1, zero_y - 1));
que.push(make_pair(zero_x - 1, zero_y));
que.push(make_pair(zero_x, zero_y - 1));
que.push(make_pair(zero_x, zero_y));

ll ret = 0;
while (!que.empty()) {
  /*
  rep(i,height){
    rep(j,width){
      if(field[i][j].visited) cout << "*";
      else cout << ".";
    }
    cout << endl;
  }
  cout << endl;
    */

  pair<int, int> p = que.front();
  que.pop();
  if (p.first < 0 || p.second < 0) {
    ret = -1;
    break;
  }
  if (field[p.first][p.second].visited)
    continue;
  field[p.first][p.second].visited = true;
  if (field[p.first][p.second].area < 0) {
    ret = -1;
    break;
  }
  ret += field[p.first][p.second].area;
  rep(k, 4) {
    int nx = p.first + vx[k];
    int ny = p.second + vy[k];
    if (nx < 0 || ny < 0)
      continue;
    if (field[p.first][p.second].cango[k]) {
      if (!field[nx][ny].visited) {
        que.push(make_pair(nx, ny));
      }
    }
  }
}

if (ret < 0) {
  cout << "INF" << endl;
} else {
  cout << ret << endl;
}

return 0;
}