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

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>

#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

struct L {
  int a, b, c;
  L(int a = 0, int b = 0, int c = 0) : a(a), b(b), c(c) {}
};

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

int main() {
  int n, m;
  cin >> n >> m;
  map<int, int> mpX, mpY;
  mpX[0] = 0;
  mpY[0] = 0;

  vector<L> lh, lv;
  rep(i, n) {
    int a, b, c;
    cin >> a >> b >> c;
    mpX[a] = 0;
    mpX[b] = 0;
    mpY[c] = 0;
    lh.emplace_back(a, b, c);
  }
  rep(i, m) {
    int a, b, c;
    cin >> c >> a >> b;
    mpY[a] = 0;
    mpY[b] = 0;
    mpX[c] = 0;
    lv.emplace_back(a, b, c);
  }

  vector<int> xs, ys;
  for (auto &p : mpX) {
    p.second = xs.size();
    xs.push_back(p.first);
  }
  for (auto &p : mpY) {
    p.second = ys.size();
    ys.push_back(p.first);
  }

  int h = xs.size() * 2;
  int w = ys.size() * 2;

  vector<vector<int>> d(h, vector<int>(w));
  rep(i, n) {
    int a = mpX[lh[i].a] * 2;
    int b = mpX[lh[i].b] * 2;
    int c = mpY[lh[i].c] * 2;
    for (int x = a; x <= b; ++x)
      d[x][c] = -1;
  }
  rep(i, m) {
    int a = mpY[lv[i].a] * 2;
    int b = mpY[lv[i].b] * 2;
    int c = mpX[lv[i].c] * 2;
    for (int y = a; y <= b; ++y)
      d[c][y] = -1;
  }

  queue<P> q;
  int sx = mpX[0] * 2, sy = mpY[0] * 2;
  d[sx][sy] = 1;
  q.emplace(sx, sy);
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    rep(v, 4) {
      int nx = x + dx[v], ny = y + dy[v];
      if (nx < 0 || nx >= h)
        continue;
      if (ny < 0 || ny >= w)
        continue;
      if (d[nx][ny] != 0)
        continue;
      d[nx][ny] = 1;
      q.emplace(nx, ny);
    }
  }

  ll ans = 0;
  rep(x, h) rep(y, w) {
    if (d[x][y] != 1)
      continue;
    if (x == 0 || x == h - 1 || y == 0 || y == w - 1) {
      cout << "INF" << endl;
      return 0;
    }
    if (x % 2 == 0 || y % 2 == 0)
      continue;
    ll ex = xs[x / 2 + 1] - xs[x / 2];
    ll ey = ys[y / 2 + 1] - ys[y / 2];
    ans += ex * ey;
  }
  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
vector<P> muki = {P(1, 0), P(0, 1), P(-1, 0), P(0, -1)};
ll INF = 1e18;
map<ll, ll> m, mm;
vector<ll> gm, gmm;
ll N, K;
P A[1005];
ll C[1005];
P E[1005];
ll D[1005];
ll V[4005][4005];
stack<P> now;
ll ans;

int main() {
  cin >> N >> K;
  m[0] = 0;
  mm[0] = 0;
  for (int i = 0; i < N; i++) {
    ll a, b, c;
    cin >> a >> b >> c;
    m[a] = 0;
    m[b] = 0;
    mm[c] = 0;
    A[i] = P(a, b);
    C[i] = c;
  }
  for (int i = 0; i < K; i++) {
    ll a, b, c;
    cin >> a >> b >> c;
    mm[b] = 0;
    mm[c] = 0;
    m[a] = 0;
    D[i] = a;
    E[i] = P(b, c);
  }
  int ii = 0;
  for (auto &p : m) {
    p.second = 2 * ii + 1;
    ii++;
    gm.push_back(p.first);
  }
  int jj = 0;
  for (auto &p : mm) {
    p.second = 2 * jj + 1;
    jj++;
    gmm.push_back(p.first);
  }
  for (int i = 0; i < N; i++) {
    ll n = m[A[i].first];
    ll nn = m[A[i].second];
    ll nnn = mm[C[i]];
    for (ll j = n; j <= nn; j++) {
      V[j][nnn] = -1;
    }
  }
  for (int i = 0; i < N; i++) {
    ll n = mm[E[i].first];
    ll nn = mm[E[i].second];
    ll nnn = m[D[i]];
    for (ll j = n; j <= nn; j++) {
      V[nnn][j] = -1;
    }
  }

  now.push(P(m[0], mm[0]));
  V[m[0]][mm[0]] = 1;
  while (!now.empty()) {
    P s = now.top();
    now.pop();
    for (auto v : muki) {
      if (s.first + v.first >= 0 && s.first + v.first < 2 * ii &&
          s.second + v.second >= 0 && s.second + v.second < 2 * jj) {
        if (V[s.first + v.first][s.second + v.second] == 0) {
          now.push(P(s.first + v.first, s.second + v.second));
          V[s.first + v.first][s.second + v.second] = 1;
        }
      }
    }
  }

  for (int i = 0; i < ii; i++) {
    for (int j = 0; j < jj; j++) {
      if (V[2 * i][2 * j] == 1) {
        if (i == 0 || j == 0 || i == ii - 1 || j == jj - 1) {
          cout << "INF" << endl;
          return 0;
        } else {
          ans += (gm[i + 1] - gm[i]) * (gmm[j + 1] - gmm[j]);
        }
      }
    }
  }
  cout << ans << endl;
}