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

/**
 *    author:  otera
 **/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf = 1e9 + 7;
const ll INF = 2e9;
const ll mod = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(), c.end()
#define pb push_back
#define debug(x) cerr << #x << " = " << (x) << endl;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename T> vector<T> compress(vector<T> A) {
  sort(A.begin(), A.end());
  A.erase(unique(A.begin(), A.end()), A.end());
  return A;
}

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

int n, m;

void solve() {
  cin >> n >> m;
  vector<int> xs = {-INF, INF}, ys = {-INF, INF};
  auto add = [&](int x, int y) {
    xs.push_back(x);
    ys.push_back(y);
  };
  vector<int> a(n), b(n), c(n), d(m), e(m), f(m);
  rep(i, n) {
    cin >> a[i] >> b[i] >> c[i];
    add(a[i], c[i]);
    add(b[i], c[i]);
  }
  rep(i, m) {
    cin >> d[i] >> e[i] >> f[i];
    add(d[i], e[i]);
    add(d[i], f[i]);
  }
  xs = compress(xs);
  ys = compress(ys);
  int xsz = (int)xs.size(), ysz = (int)ys.size();

  auto find_x = [&](int x) -> int {
    return upper_bound(all(xs), x) - xs.begin() - 1;
  };
  auto find_y = [&](int y) -> int {
    return upper_bound(all(ys), y) - ys.begin() - 1;
  };

  static bitset<3100> above[3100], left[3100];

  rep(i, n) {
    a[i] = find_x(a[i]);
    b[i] = find_x(b[i]);
    c[i] = find_y(c[i]);
    for (int x = a[i]; x < b[i]; ++x) {
      left[x][c[i]] = 1;
    }
  }
  rep(i, m) {
    d[i] = find_x(d[i]);
    e[i] = find_y(e[i]);
    f[i] = find_y(f[i]);
    for (int y = e[i]; y < f[i]; ++y) {
      above[d[i]][y] = 1;
    }
  }
  int cowx = find_x(0), cowy = find_y(0);
  vector<vector<int>> dist(xsz, vector<int>(ysz, INF));
  dist[cowx][cowy] = 0;
  queue<P> que;
  que.push(P{cowx, cowy});
  auto check = [&](int x, int y) -> bool {
    return (0 <= x && x < xsz && 0 <= y && y < ysz);
  };
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    int x = p.fr, y = p.sc;
    // left
    if (check(x, y - 1) && !left[x][y] &&
        chmin(dist[x][y - 1], dist[x][y] + 1)) {
      que.push(P{x, y - 1});
    }
    // above
    if (check(x - 1, y) && !above[x][y] &&
        chmin(dist[x - 1][y], dist[x][y] + 1)) {
      que.push(P{x - 1, y});
    }
    // right
    if (check(x, y + 1) && !left[x][y + 1] &&
        chmin(dist[x][y + 1], dist[x][y] + 1)) {
      que.push(P{x, y + 1});
    }
    // down
    if (check(x + 1, y) && !above[x + 1][y] &&
        chmin(dist[x + 1][y], dist[x][y] + 1)) {
      que.push(P{x + 1, y});
    }
  }
  int ans = 0;
  rep(i, xsz - 1) {
    rep(j, ysz - 1) {
      int dx = xs[i + 1] - xs[i], dy = ys[j + 1] - ys[j];
      if (dist[i][j] < INF) {
        if (i == 0 || j == 0 || i == xsz - 2 || j == ysz - 2) {
          cout << "INF" << endl;
          return;
        }
        ans += dx * dy;
      }
    }
  }
  cout << ans << endl;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // cout << fixed << setprecision(10);
  // int t; cin >> t; rep(i, t)solve();
  solve();
  return 0;
}

Error: -11

Fixed submission:

/**
 *    author:  otera
 **/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf = 1e9 + 7;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(), c.end()
#define pb push_back
#define debug(x) cerr << #x << " = " << (x) << endl;
#define lob(c, x) lower_bound(all(c), (x)) - (c).begin();
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename T> vector<T> compress(vector<T> A) {
  sort(A.begin(), A.end());
  A.erase(unique(A.begin(), A.end()), A.end());
  return A;
}

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

void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(n), c(n);
  vector<int> d(m), e(m), f(m);
  vector<int> X, Y;
  rep(i, n) {
    cin >> a[i] >> b[i] >> c[i];
    X.push_back(a[i]);
    X.push_back(b[i]);
    Y.push_back(c[i]);
  }
  rep(i, m) {
    cin >> d[i] >> e[i] >> f[i];
    X.push_back(d[i]);
    X.push_back(e[i]);
    Y.push_back(f[i]);
  }
  X.push_back(0);
  Y.push_back(0);
  X.push_back(INF);
  Y.push_back(INF);
  X.push_back(-INF);
  Y.push_back(-INF);
  sort(all(X)), sort(all(Y));
  auto comx = compress(X);
  auto comy = compress(Y);
  int xsz = (int)comx.size(), ysz = (int)comy.size();
  vector<vector<int>> mv0(ysz + 1, vector<int>(xsz + 1, 0)),
      mv1(xsz + 1, vector<int>(ysz + 1, 0)), mv2(ysz + 1, vector<int>(xsz + 1)),
      mv3(xsz + 1, vector<int>(ysz + 1, 0));
  rep(i, n) {
    int cy = lob(comy, c[i]);
    int ax = lob(comx, a[i]);
    int bx = lob(comx, b[i]);
    mv3[ax][cy]++, mv3[bx][cy]--;
    mv1[ax][cy - 1]++, mv1[bx][cy - 1]--;
  }
  rep(i, m) {
    int dxx = lob(comx, d[i]);
    int ey = lob(comy, e[i]);
    int fy = lob(comy, f[i]);
    mv2[dxx][ey]++;
    mv2[dxx][fy]--;
    mv0[dxx - 1][ey]++;
    mv0[dxx - 1][fy]--;
  }
  rep(i, xsz) {
    rep(j, ysz + 1) {
      mv1[i + 1][j] += mv1[i][j];
      mv3[i + 1][j] += mv3[i][j];
    }
  }
  rep(i, xsz + 1) {
    rep(j, ysz) {
      mv0[i][j + 1] += mv0[i][j];
      mv2[i][j + 1] += mv2[i][j];
    }
  }
  queue<P> que;
  map<P, int> dist;
  rep(i, xsz) {
    rep(j, ysz) { dist[{i, j}] = INF; }
  }
  int stx = lob(comx, 0);
  int sty = lob(comy, 0);
  dist[{stx, sty}] = 0;
  dist[{stx - 1, sty}] = 0;
  dist[{stx, sty - 1}] = 0;
  dist[{stx - 1, sty - 1}] = 0;
  que.push({stx, sty});
  que.push({stx - 1, sty});
  que.push({stx, sty - 1});
  que.push({stx - 1, sty - 1});
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    int x = p.fr, y = p.sc;
    int nx0 = x + dx[0], ny0 = y + dy[0];
    if (0 <= nx0 && nx0 < xsz && 0 <= ny0 && ny0 < ysz) {
      if (chmin(dist[{nx0, ny0}], dist[{x, y}] + 1) && !mv0[nx0][ny0]) {
        que.push({nx0, ny0});
      }
    }
    int nx1 = x + dx[1], ny1 = y + dy[1];
    if (0 <= nx1 && nx1 < xsz && 0 <= ny1 && ny1 < ysz && !mv1[nx1][ny1]) {
      if (chmin(dist[{nx1, ny1}], dist[{x, y}] + 1)) {
        que.push({nx1, ny1});
      }
    }
    int nx2 = x + dx[2], ny2 = y + dy[2];
    if (0 <= nx2 && nx2 < xsz && 0 <= ny2 && ny2 < ysz && !mv2[nx2][ny2]) {
      if (chmin(dist[{nx2, ny2}], dist[{x, y}] + 1)) {
        que.push({nx2, ny2});
      }
    }
    int nx3 = x + dx[3], ny3 = y + dy[3];
    if (0 <= nx3 && nx3 < xsz && 0 <= ny3 && ny3 < ysz && !mv3[nx3][ny3]) {
      if (chmin(dist[{nx0, ny0}], dist[{x, y}] + 1)) {
        que.push({nx0, ny0});
      }
    }
  }
  int ans = 0;
  rep(i, xsz) {
    rep(j, ysz) {
      if (i == 0 || j == 0 || i == xsz - 1 || j == ysz - 1) {
        if (dist[{i, j}] < INF) {
          cout << "INF" << endl;
          return;
        }
      } else {
        if (dist[{i, j}] < INF) {
          ans += (X[i + 1] - X[i]) * (Y[j + 1] - Y[j]);
        }
      }
    }
  }
  cout << ans << endl;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // cout << fixed << setprecision(10);
  // int t; cin >> t; rep(i, t)solve();
  solve();
  return 0;
}