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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define zero_pad(num) setfill('0') << std::right << setw(num)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;

vector<int> dy = {-1, 0, 1, 0};
vector<int> dx = {0, -1, 0, 1};
ll INF = 1000000000;

int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<ll, P>> a(n);
  rep(i, n) cin >> a[i].second.first >> a[i].second.second >> a[i].first;
  vector<pair<ll, P>> d(m);
  rep(i, m) cin >> d[i].first >> d[i].second.first >> d[i].second.second;
  sort(a.begin(), a.end());
  sort(d.begin(), d.end());
  set<ll> xx, yy;
  rep(i, n) {
    yy.insert(a[i].second.first);
    yy.insert(a[i].second.second);
    xx.insert(a[i].first);
  }
  rep(i, m) {
    yy.insert(d[i].first);
    xx.insert(d[i].second.first);
    xx.insert(d[i].second.second);
  }
  xx.insert(-INF);
  xx.insert(INF);
  yy.insert(-INF);
  yy.insert(INF);

  vector<P> x, y;
  int sx, sy;
  x.push_back(make_pair(-INF, -INF));
  ll pre = -INF;
  int idx = 0;
  for (ll l : xx) {
    if (l == -INF)
      continue;
    x.push_back(make_pair(pre, l));
    idx++;
    if (pre < 0 && 0 < l)
      sx = idx;
    pre = l;
    x.push_back(make_pair(l, l));
    idx++;
    if (pre == 0 && l == 0)
      sx = idx;
  }
  y.push_back(make_pair(-INF, -INF));
  pre = -INF;
  idx = 0;
  for (ll l : yy) {
    if (l == -INF)
      continue;
    y.push_back(make_pair(pre, l));
    idx++;
    if (pre < 0 && 0 < l)
      sy = idx;
    pre = l;
    y.push_back(make_pair(l, l));
    idx++;
    if (pre == 0 && l == 0)
      sy = idx;
  }

  int h = y.size();
  int w = x.size();
  vector<vector<char>> s(h, vector<char>(w, '.'));
  idx = 0;
  rep(i, n) {
    while (x[idx].first < a[i].first)
      idx++;
    rep(j, h) {
      if (a[i].second.first <= y[j].first &&
          y[j].second <= a[i].second.second) {
        s[j][idx] = '#';
      }
    }
  }
  idx = 0;
  rep(i, m) {
    while (y[idx].first < d[i].first)
      idx++;
    rep(j, w) {
      if (d[i].second.first <= x[j].first &&
          x[j].second <= d[i].second.second) {
        s[idx][j] = '#';
      }
    }
  }

  vector<vector<bool>> vis(h, vector<bool>(w, false));
  queue<P> que;
  que.push(make_pair(sy, sx));
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    int y = p.first;
    int x = p.second;
    if (vis[y][x])
      continue;
    vis[y][x] = true;
    rep(i, 4) {
      if (y + dy[i] < 0 || y + dy[i] >= h)
        continue;
      if (x + dx[i] < 0 || x + dx[i] >= w)
        continue;
      if (s[y + dy[i]][x + dx[i]] == '#')
        continue;
      if (vis[y + dy[i]][x + dx[i]])
        continue;
      que.push(make_pair(y + dy[i], x + dx[i]));
    }
  }

  rep(i, h) {
    if (vis[i][0] || vis[i][w - 1]) {
      cout << "INF" << endl;
      return 0;
    }
  }
  rep(i, w) {
    if (vis[0][i] || vis[h - 1][i]) {
      cout << "INF" << endl;
      return 0;
    }
  }

  ll ans = 0;
  rep(i, h) rep(j, w) {
    if (!vis[i][j])
      continue;
    ll area = (y[i].second - y[i].first) * (x[j].second - x[j].first);
    ans += area;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define zero_pad(num) setfill('0') << std::right << setw(num)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;

vector<int> dy = {-1, 0, 1, 0};
vector<int> dx = {0, -1, 0, 1};

int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<ll, P>> a(n);
  rep(i, n) cin >> a[i].second.first >> a[i].second.second >> a[i].first;
  vector<pair<ll, P>> d(m);
  rep(i, m) cin >> d[i].first >> d[i].second.first >> d[i].second.second;
  sort(a.begin(), a.end());
  sort(d.begin(), d.end());
  set<ll> xx, yy;
  rep(i, n) {
    xx.insert(a[i].second.first);
    xx.insert(a[i].second.second);
    yy.insert(a[i].first);
  }
  rep(i, m) {
    xx.insert(d[i].first);
    yy.insert(d[i].second.first);
    yy.insert(d[i].second.second);
  }

  vector<P> x, y;
  int sx, sy;
  ll INF = 1000000000;
  x.push_back(make_pair(-INF, -INF));
  ll pre = -INF;
  int idx = 0;
  for (ll l : xx) {
    x.push_back(make_pair(pre, l));
    idx++;
    if (pre < 0 && 0 < l)
      sx = idx;
    pre = l;
    x.push_back(make_pair(l, l));
    idx++;
    if (pre == 0 && l == 0)
      sx = idx;
  }
  x.push_back(make_pair(pre, INF));
  x.push_back(make_pair(INF, INF));
  y.push_back(make_pair(-INF, -INF));
  pre = -INF;
  idx = 0;
  for (ll l : yy) {
    y.push_back(make_pair(pre, l));
    idx++;
    if (pre < 0 && 0 < l)
      sy = idx;
    pre = l;
    y.push_back(make_pair(l, l));
    idx++;
    if (pre == 0 && l == 0)
      sy = idx;
  }
  y.push_back(make_pair(pre, INF));
  y.push_back(make_pair(INF, INF));

  int h = y.size();
  int w = x.size();
  vector<vector<char>> s(h, vector<char>(w, '.'));
  idx = 0;
  rep(i, n) {
    while (x[idx].first < a[i].first)
      idx++;
    rep(j, h) {
      if (a[i].second.first <= y[j].first &&
          y[j].second <= a[i].second.second) {
        s[j][idx] = '#';
      }
    }
  }
  idx = 0;
  rep(i, m) {
    while (y[idx].first < d[i].first)
      idx++;
    rep(j, w) {
      if (d[i].second.first <= x[j].first &&
          x[j].second <= d[i].second.second) {
        s[idx][j] = '#';
      }
    }
  }

  vector<vector<bool>> vis(h, vector<bool>(w, false));
  queue<P> que;
  que.push(make_pair(sy, sx));
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    int y = p.first;
    int x = p.second;
    if (vis[y][x])
      continue;
    vis[y][x] = true;
    rep(i, 4) {
      if (y + dy[i] < 0 || y + dy[i] >= h)
        continue;
      if (x + dx[i] < 0 || x + dx[i] >= w)
        continue;
      if (s[y + dy[i]][x + dx[i]] == '#')
        continue;
      if (vis[y + dy[i]][x + dx[i]])
        continue;
      que.push(make_pair(y + dy[i], x + dx[i]));
    }
  }

  rep(i, h) {
    if (vis[i][0] || vis[i][w - 1]) {
      cout << "INF" << endl;
      return 0;
    }
  }
  rep(i, w) {
    if (vis[0][i] || vis[h - 1][i]) {
      cout << "INF" << endl;
      return 0;
    }
  }

  ll ans = 0;
  rep(i, h) rep(j, w) {
    if (!vis[i][j])
      continue;
    ll area = (y[i].second - y[i].first) * (x[j].second - x[j].first);
    ans += area;
  }
  cout << ans << endl;
}