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

#include <bits/stdc++.h>

using namespace std;
#define all(x) (x).begin(), (x).end()
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<vector<bool>> vvb;
typedef vector<vector<int>> vvi;
typedef vector<vector<long long>> vvl;
typedef pair<int, int> pii;
typedef long long ll;
void debugF(ostream &os) { os << endl; }
template <typename Head, typename... Tail>
void debugF(ostream &os, Head H, Tail... T) {
  os << " " << H;
  debugF(os, T...);
}
#define debug(...)                                                             \\
  cout << "[" << #__VA_ARGS__ << "]:", debugF(cout, __VA_ARGS__)
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &x : v)
    is >> x;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  for (int i = 0; i < v.size(); os << (i > 0 ? " " : "") << v[i++])
    ;
  return os;
}

using t3 = tuple<int, int, int>;
const int inf = 1e9 + 10;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vi xc, yc;
  vector<t3> vl, hl;

  int i, j, a, b, c;
  for (i = 0; i < n; i++) {
    cin >> a >> b >> c;
    vl.push_back({a, b, c});
    xc.push_back(a);
    xc.push_back(b);
    yc.push_back(c);
  }
  for (i = 0; i < m; i++) {
    cin >> a >> b >> c;
    hl.push_back({a, b, c});
    xc.push_back(a);
    yc.push_back(b);
    yc.push_back(c);
  }
  xc.push_back(-inf);
  xc.push_back(0);
  // xc.push_back(inf);
  yc.push_back(-inf);
  yc.push_back(0);
  // yc.push_back(inf);
  sort(all(xc));
  sort(all(yc));
  xc.resize(unique(all(xc)) - xc.begin());
  yc.resize(unique(all(yc)) - yc.begin());
  int nr = xc.size();
  int nc = yc.size();
  vvb xw(nr, vb(nc));
  vvb yw(nr, vb(nc));

  auto getx = [&](int q) -> int {
    return lower_bound(all(xc), q) - xc.begin();
  };
  auto gety = [&](int q) -> int {
    return lower_bound(all(yc), q) - yc.begin();
  };

  for (i = 0; i < n; i++) {
    tie(a, b, c) = vl[i];
    // row row col
    a = getx(a);
    b = getx(b);
    c = gety(c);
    while (a < b) {
      xw[a][c] = true;
      a++;
    }
  }
  for (i = 0; i < m; i++) {
    tie(a, b, c) = hl[i];
    // row col col
    a = getx(a);
    b = gety(b);
    c = gety(c);
    while (b < c) {
      yw[a][b] = true;
      b++;
    }
  }
  ll ans = 0;
  vvb clr(nr, vb(nc));
  stack<pii> Q;
  a = getx(0);
  b = gety(0);
  clr[a][b] = true;
  Q.push({a, b});
  vi dx = {0, -1, 0, 1};
  vi dy = {1, 0, -1, 0};
  bool isInf = false;
  while (!Q.empty()) {
    tie(a, b) = Q.top();
    // debug(a,b);
    Q.pop();
    if (a == 0 || a == nr - 1 || b == 0 || b == nc - 1) {
      isInf = true;
      break;
    }
    ans += (ll(xc[a + 1]) - xc[a]) * (ll(yc[b + 1]) - yc[b]);
    for (i = 0; i < 4; i++) {
      if (i == 0 && xw[a][b + 1])
        continue;
      if (i == 2 && xw[a][b])
        continue;
      if (i == 1 && yw[a][b])
        continue;
      if (i == 3 && yw[a + 1][b])
        continue;
      int newa = a + dx[i];
      int newb = b + dy[i];
      if (clr[newa][newb])
        continue;
      clr[newa][newb] = true;
      Q.push({newa, newb});
    }
  }
  if (isInf) {
    cout << "INF\\n";
  } else {
    cout << ans << "\\n";
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define all(x) (x).begin(), (x).end()
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<vector<bool>> vvb;
typedef vector<vector<int>> vvi;
typedef vector<vector<long long>> vvl;
typedef pair<int, int> pii;
typedef long long ll;
void debugF(ostream &os) { os << endl; }
template <typename Head, typename... Tail>
void debugF(ostream &os, Head H, Tail... T) {
  os << " " << H;
  debugF(os, T...);
}
#define debug(...)                                                             \\
  cout << "[" << #__VA_ARGS__ << "]:", debugF(cout, __VA_ARGS__)
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &x : v)
    is >> x;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  for (int i = 0; i < v.size(); os << (i > 0 ? " " : "") << v[i++])
    ;
  return os;
}

using t3 = tuple<int, int, int>;

const int inf = 1e9 + 10;

pii locate(int rx, int cy, vector<pii> const &rg, vector<pii> &cg) {
  auto rl = lower_bound(rg.begin(), rg.end(), make_pair(rx, rx));
  // if(rl->first > rx) rl--;
  int rans = rl - rg.begin();
  auto cl = lower_bound(cg.begin(), cg.end(), make_pair(cy, cy));
  // if(cl->first > cy) cl--;
  int cans = cl - cg.begin();
  return {rans, cans};
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  set<int> rc, cc;
  rc.insert(0);
  cc.insert(0);
  vector<t3> hl, vl;
  int i, j, a, b, c;
  for (i = 0; i < n; i++) {
    cin >> a >> b >> c;
    a *= 2;
    b *= 2;
    c *= 2;
    vl.emplace_back(a, b, c);
    rc.insert(a);
    rc.insert(b);
    cc.insert(c);
  }
  for (i = 0; i < m; i++) {
    cin >> a >> b >> c;
    a *= 2;
    b *= 2;
    c *= 2;
    hl.emplace_back(b, c, a);
    rc.insert(a);
    cc.insert(b);
    cc.insert(c);
  }
  vi rcv(all(rc));
  vi ccv(all(cc));

  vector<pii> rg;
  rg.push_back({-inf, rcv[0] - 1});
  for (i = 0; i < rcv.size(); i++) {
    rg.push_back({rcv[i], rcv[i]});
    if (i + 1 < rcv.size()) {
      rg.push_back({rcv[i] + 1, rcv[i + 1] - 1});
    }
  }
  rg.push_back({rcv.back() + 1, +inf});
  int nr = rg.size();

  vector<pii> cg;
  cg.push_back({-inf, ccv[0] - 1});
  for (i = 0; i < ccv.size(); i++) {
    cg.push_back({ccv[i], ccv[i]});
    if (i + 1 < ccv.size()) {
      cg.push_back({ccv[i] + 1, ccv[i + 1] - 1});
    }
  }
  cg.push_back({ccv.back() + 1, +inf});
  int nc = cg.size();

  vvb grid(nr, vb(nc, false));
  for (i = 0; i < n; i++) {
    tie(a, b, c) = vl[i];
    // row row col
    int rr, cc;
    tie(rr, cc) = locate(a, c, rg, cg);
    while (rg[rr].second <= b) {
      grid[rr][cc] = true;
      rr++;
    }
  }
  for (i = 0; i < m; i++) {
    tie(a, b, c) = hl[i];
    // col col row
    int rr, cc;
    tie(rr, cc) = locate(c, a, rg, cg);
    while (cg[cc].second <= b) {
      grid[rr][cc] = true;
      cc++;
    }
  }

  ll ans = 0;
  stack<pii> q;
  vvb clr(nr, vb(nc, false));
  int ri, ci;
  tie(ri, ci) = locate(0, 0, rg, cg);
  clr[ri][ci] = true;
  q.push({ri, ci});
  bool isInf = false;
  vector<pii> dir = {{0, 1},  {-1, 0},  {0, -1}, {1, 0},
                     {-1, 1}, {-1, -1}, {1, -1}, {1, 1}};

  while (!q.empty()) {
    tie(ri, ci) = q.top();
    q.pop();
    if (ri == 0 || ri == nr - 1 || ci == 0 || ci == nc - 1) {
      isInf = true;
      break;
    }
    ll h = max(ll(rg[ri].second) - rg[ri].first + 1, 1ll);
    ll w = max(ll(cg[ci].second) - cg[ci].first + 1, 1ll);
    ans += h * w;
    for (auto dd : dir) {
      int rnew = ri + dd.first;
      int cnew = ci + dd.second;
      if (rnew < 0 || rnew > nr - 1 || ci < 0 || ci > nc - 1 ||
          grid[rnew][cnew] || clr[rnew][cnew]) {
        continue;
      }
      clr[rnew][cnew] = true;
      q.push({rnew, cnew});
    }
  }

  if (isInf) {
    cout << "INF\\n";
  } else {
    cout << (ans / 2) << "\\n";
  }

  return 0;
}