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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
using namespace std;
using ll = long long int;
using vec = vector<int>;
using P = pair<int, int>;

const int INF = 1e9 + 7;

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> xmp, ymp;
  xmp[0] = 0, ymp[0] = 0;

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

  vec xs, ys;
  for (auto &p : xmp) {
    p.second = xs.size();
    xs.push_back(p.first);
  }
  for (auto &p : ymp) {
    p.second = ys.size();
    ys.push_back(p.first);
  }

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

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

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

  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: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
using namespace std;
using ll = long long int;
using vec = vector<int>;
using P = pair<int, int>;
using vb = vector<bool>;

const int INF = 1e9 + 7;

int main() {
  int n, m;
  cin >> n >> m;
  map<int, vector<P>> xmp, ymp;
  rep(i, n) {
    int a, b, c;
    cin >> a >> b >> c;
    ymp[c].emplace_back(a, b);
  }
  rep(i, m) {
    int a, b, c;
    cin >> c >> a >> b;
    xmp[c].emplace_back(a, b);
  }
  vec xs, ys;
  for (auto pp : xmp) {
    xs.push_back(pp.first);
  }
  sort(all(xs));
  for (auto pp : ymp) {
    ys.push_back(pp.first);
  }
  sort(all(ys));

  vector<vb> xb(xs.size(), vb(ys.size(), false)),
      yb(xs.size(), vb(ys.size(), false));
  rep(x, xs.size()) {
    int k = 0;
    vector<P> v = xmp[xs[x]];
    sort(all(v));
    rep(y, ys.size()) {
      while (k < v.size() && ys[y] >= v[k].second)
        k++;
      if (k == v.size())
        break;
      // cout << ys[y] << v[k].first << v[k].second << endl;
      if (ys[y] >= v[k].first && ys[y] < v[k].second)
        xb[x][y] = true;
    }
  }
  rep(y, ys.size()) {
    int k = 0;
    vector<P> v = ymp[ys[y]];
    sort(all(v));
    rep(x, xs.size()) {
      while (k < v.size() && xs[x] >= v[k].second)
        k++;
      if (k == v.size())
        break;
      // cout << ys[y] << v[k].first << v[k].second << endl;
      if (xs[x] >= v[k].first && xs[x] < v[k].second)
        yb[x][y] = true;
    }
  }

  // rep(y,ys.size()-1) {
  //   cout << (xb[0][y] ? " _" : "  ");
  // }
  // cout << endl;
  // rep(x,xs.size()-1) {
  //   rep(y,ys.size()-1) {
  //     cout << (yb[x][y] ? "|" : " ");
  //     cout << (xb[x+1][y] ? "_" : " ");
  //   }
  //   cout << (yb[x+1][ys.size()-1] ? "|" : " ") << endl;
  // }

  // bfs
  queue<P> que; // P(x, y)
  vector<vb> flag(xs.size() - 1, vb(ys.size() - 1));
  int sx = upper_bound(all(xs), 0) - xs.begin() - 1;
  int sy = upper_bound(all(ys), 0) - ys.begin() - 1;
  if (sx < 0 || sy < 0) {
    cout << "INF" << endl;
    return 0;
  }
  que.push(P(sx, sy));
  flag[sx][sy] = true;
  int sum = (xs[sx + 1] - xs[sx]) * (ys[sy + 1] - ys[sy]);

  while (que.size()) {
    P p = que.front();
    que.pop();
    int x = p.first, y = p.second;
    vector<P> next;

    if (!xb[x][y])
      next.push_back(P(x - 1, y));
    if (!xb[x + 1][y])
      next.push_back(P(x + 1, y));
    if (!yb[x][y])
      next.push_back(P(x, y - 1));
    if (!yb[x][y + 1])
      next.push_back(P(x, y + 1));

    for (P np : next) {
      int nx = np.first, ny = np.second;
      if (nx == -1 || nx == xs.size() - 1 || ny == -1 || ny == ys.size() - 1) {
        cout << "INF" << endl;
        return 0;
      }
      if (!flag[nx][ny]) {
        que.push(np);
        flag[nx][ny] = true;
        sum += (xs[nx + 1] - xs[nx]) * (ys[ny + 1] - ys[ny]);
      }
    }
  }
  // bfs end
  cout << sum << endl;
  return 0;
}