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

#include <bits/stdc++.h>

using namespace std;

typedef long long i64;
typedef i64 int_t;
typedef vector<int_t> vi;
typedef vector<vi> vvi;

typedef pair<int_t, int_t> pi;

#define tr(c, i) for (auto i = (c).begin(); i != (c).end(); i++)
#define pb push_back
#define sz(a) i64((a).size())
#define all(c) (c).begin(), (c).end()
#define REP(s, e, i) for (i = (s); i < (e); ++i)

void parse_lines(const vi &C, const vi &vmin, const vi &vmax, vi &coords,
                 vector<vector<pi>> &intervals) {
  map<i64, vector<pi>> L;
  i64 N = sz(C), i;
  REP(0, N, i) {
    i64 CC = C[i] * 2 + 1;
    if (L.find(CC) == L.end()) {
      L[CC] = vector<pi>();
    }
    L[CC].pb({vmin[i] * 2 + 1, vmax[i] * 2 + 1});
  }

  for (auto &l : L) {
    i64 coord = l.first;
    coords.pb(coord);

    vector<pi> iv;
    sort(all(l.second));
    i64 v0 = l.second[0].first, v1 = l.second[0].second;
    REP(1, sz(l.second), i) {
      i64 c0 = l.second[i].first, c1 = l.second[i].second;
      if (c0 <= v1) {
        v1 = max(v1, c1);
      } else {
        iv.pb({v0, v1});
        v0 = c0;
        v1 = c1;
      }
    }
    iv.pb({v0, v1});
    intervals.pb(iv);
  }
}

bool passable(const vector<pi> &intervals, i64 vmin, i64 vmax) {
  i64 LARGE = 10000000000LL;
  auto it = upper_bound(all(intervals), pi(vmin, LARGE));
  if (it == intervals.begin()) {
    return true;
  }
  it--;
  if (it->second >= vmax) {
    return false;
  }
  return true;
}

int main(int argc, char *argv[]) {
  i64 N, M, i;
  cin >> N >> M;
  vi A(N), B(N), C(N), D(M), E(M), F(M);
  REP(0, N, i) { cin >> A[i] >> B[i] >> C[i]; }
  REP(0, M, i) { cin >> D[i] >> E[i] >> F[i]; }

  vi xs, ys;
  vector<vector<pi>> x_intervals, y_intervals;
  parse_lines(C, A, B, xs, x_intervals);
  parse_lines(D, E, F, ys, y_intervals);

  if (N == 0 || M == 0) {
    cout << "INF" << endl;
    return 0;
  }

  i64 NX = sz(xs), NY = sz(ys);
  i64 x0 = lower_bound(all(xs), 0LL) - xs.begin();
  i64 y0 = lower_bound(all(ys), 0LL) - ys.begin();
  pi p0(x0, y0);
  stack<pi> st;
  st.push(p0);
  i64 ans = 0;
  vvi visited(NX + 1, vi(NY + 1, 0));
  visited[x0][y0] = 1;
  while (!st.empty()) {
    pi cur = st.top();
    st.pop();

    if (cur.first == 0 || cur.first == NX || cur.second == 0 ||
        cur.second == NY) {
      cout << "INF" << endl;
      return 0;
    }

    // cout << "visit " << cur.first << " " << cur.second << endl;

    i64 xmin = xs[cur.first - 1], xmax = xs[cur.first];
    i64 ymin = ys[cur.second - 1], ymax = ys[cur.second];
    ans += ((xmax - xmin) / 2) * ((ymax - ymin) / 2);

    // left
    if (passable(x_intervals[cur.first - 1], ymin, ymax) &&
        visited[cur.first - 1][cur.second] == 0) {

      st.push(pi(cur.first - 1, cur.second));
      visited[cur.first - 1][cur.second] = 1;
    }
    // right
    if (passable(x_intervals[cur.first], ymin, ymax) &&
        visited[cur.first + 1][cur.second] == 0) {
      st.push(pi(cur.first + 1, cur.second));
      visited[cur.first + 1][cur.second] = 1;
    }
    // up
    if (passable(y_intervals[cur.second - 1], xmin, xmax) &&
        visited[cur.first][cur.second - 1] == 0) {
      st.push(pi(cur.first, cur.second - 1));
      visited[cur.first][cur.second - 1] = 1;
    }
    // down
    if (passable(y_intervals[cur.second], xmin, xmax) &&
        visited[cur.first][cur.second + 1] == 0) {
      st.push(pi(cur.first, cur.second + 1));
      visited[cur.first][cur.second + 1] = 1;
    }
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long i64;
typedef i64 int_t;
typedef vector<int_t> vi;
typedef vector<vi> vvi;

typedef pair<int_t, int_t> pi;

#define tr(c, i) for (auto i = (c).begin(); i != (c).end(); i++)
#define pb push_back
#define sz(a) i64((a).size())
#define all(c) (c).begin(), (c).end()
#define REP(s, e, i) for (i = (s); i < (e); ++i)

void parse_lines(const vi &C, const vi &vmin, const vi &vmax, vi &coords,
                 vector<vector<pi>> &intervals) {
  map<i64, vector<pi>> L;
  i64 N = sz(C), i;
  REP(0, N, i) {
    i64 CC = C[i] * 2 + 1;
    if (L.find(CC) == L.end()) {
      L[CC] = vector<pi>();
    }
    L[CC].pb({vmin[i] * 2 + 1, vmax[i] * 2 + 1});
  }

  for (auto &l : L) {
    i64 coord = l.first;
    coords.pb(coord);

    vector<pi> iv;
    sort(all(l.second));
    i64 v0 = l.second[0].first, v1 = l.second[0].second;
    REP(1, sz(l.second), i) {
      i64 c0 = l.second[i].first, c1 = l.second[i].second;
      if (c0 <= v1) {
        v1 = c1;
      } else {
        iv.pb({v0, v1});
        v0 = c0;
        v1 = c1;
      }
    }
    iv.pb({v0, v1});
    intervals.pb(iv);
  }
}

bool passable(const vector<pi> &intervals, i64 vmin, i64 vmax) {
  i64 LARGE = 10000;
  auto it = upper_bound(all(intervals), pi(vmin, LARGE));
  if (it == intervals.begin()) {
    return true;
  }
  it--;
  if (it->second >= vmax) {
    return false;
  }
  return true;
}

int main(int argc, char *argv[]) {
  i64 N, M, i;
  cin >> N >> M;
  vi A(N), B(N), C(N), D(M), E(M), F(M);
  REP(0, N, i) { cin >> A[i] >> B[i] >> C[i]; }
  REP(0, M, i) { cin >> D[i] >> E[i] >> F[i]; }

  vi xs, ys;
  vector<vector<pi>> x_intervals, y_intervals;
  parse_lines(C, A, B, xs, x_intervals);
  parse_lines(D, E, F, ys, y_intervals);

  if (N == 0 || M == 0) {
    cout << "INF" << endl;
    return 0;
  }

  i64 NX = sz(xs), NY = sz(ys);
  i64 x0 = lower_bound(all(xs), 0LL) - xs.begin();
  i64 y0 = lower_bound(all(ys), 0LL) - ys.begin();
  pi p0(x0, y0);
  stack<pi> st;
  st.push(p0);
  i64 ans = 0;
  vvi visited(NX, vi(NY, 0));
  visited[x0][y0] = 1;
  while (!st.empty()) {
    pi cur = st.top();
    st.pop();

    if (cur.first == 0 || cur.first == NX || cur.second == 0 ||
        cur.second == NY) {
      cout << "INF" << endl;
      return 0;
    }

    // cout << "visit " << cur.first << " " << cur.second << endl;

    i64 xmin = xs[cur.first - 1], xmax = xs[cur.first];
    i64 ymin = ys[cur.second - 1], ymax = ys[cur.second];
    ans += (xmax - xmin) * (ymax - ymin) / 4;

    // left
    if (passable(x_intervals[cur.first - 1], ymin, ymax) &&
        visited[cur.first - 1][cur.second] == 0) {

      st.push(pi(cur.first - 1, cur.second));
      visited[cur.first - 1][cur.second] = 1;
    }
    // right
    if (passable(x_intervals[cur.first], ymin, ymax) &&
        visited[cur.first + 1][cur.second] == 0) {
      st.push(pi(cur.first + 1, cur.second));
      visited[cur.first + 1][cur.second] = 1;
    }
    // up
    if (passable(y_intervals[cur.second - 1], xmin, xmax) &&
        visited[cur.first][cur.second - 1] == 0) {
      st.push(pi(cur.first, cur.second - 1));
      visited[cur.first][cur.second - 1] = 1;
    }
    // down
    if (passable(y_intervals[cur.second], xmin, xmax) &&
        visited[cur.first][cur.second + 1] == 0) {
      st.push(pi(cur.first, cur.second + 1));
      visited[cur.first][cur.second + 1] = 1;
    }
  }

  cout << ans << endl;

  return 0;
}