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

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define FOR(i, a, b) for (int i = a; i <= (b); i++)
#define ROF(i, a, b) for (int i = a; i >= (b); i--)

using pii = pair<int, int>;
using vpii = vector<pii>;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vll = vector<ll>;
using vvll = vector<vll>;

pii ch[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

int main() {
  int n, m;
  cin >> n >> m;
  vi a(n + 1), b(n + 1), c(n + 1), d(m + 1), e(m + 1), f(m + 1);
  vll x, y;

  const int MX = 1e9;
  x.pb(-(MX + 5));
  x.pb(MX + 5);
  y.pb(-(MX + 5));
  y.pb(MX + 5);

  FOR(i, 1, n) {
    cin >> a[i] >> b[i] >> c[i];
    x.pb(a[i]);
    x.pb(b[i]);
    y.pb(c[i]);
  }

  FOR(i, 1, m) {
    cin >> d[i] >> e[i] >> f[i];
    x.pb(d[i]);
    y.pb(e[i]);
    y.pb(f[i]);
  }

  sort(all(x));
  sort(all(y));
  auto xt = unique(all(x));
  auto yt = unique(all(y));
  x.erase(xt, x.end());
  y.erase(yt, y.end());

  int sx = lower_bound(all(x), 0) - x.begin(),
      sy = lower_bound(all(y), 0) - y.begin();
  int xs = x.size() - 1, ys = y.size() - 1;

  // cout << xs << ' ' << ys << endl;
  // for (int i : x) cout << i << ' ';
  // cout << endl;
  // for (int i : y) cout << i << ' ';
  // cout << endl;
  // return 0;

  using vb = vector<bool>;
  using vvb = vector<vb>;
  vvb vis(ys + 1, vb(xs + 1));
  vector<vvb> can(4, vvb(ys + 1, vb(xs + 1, 1)));

  FOR(i, 1, n) {
    int al = lower_bound(all(x), a[i]) - x.begin();
    int bl = lower_bound(all(x), b[i]) - x.begin();
    int cl = lower_bound(all(y), c[i]) - y.begin();
    // cout << al << ' ' << bl << ' ' << cl << endl;
    FOR(j, al + 1, bl)
    can[0][cl][j] = can[1][cl + 1][j] = 0;
  }
  FOR(i, 1, m) {
    int dl = lower_bound(all(x), d[i]) - x.begin();
    int el = lower_bound(all(y), e[i]) - y.begin();
    int fl = lower_bound(all(y), f[i]) - y.begin();
    // cout << dl << ' ' << el << ' ' << fl << endl;
    FOR(j, el + 1, fl)
    can[2][j][dl] = can[3][j][dl + 1] = 0;
  }

  // FOR(k, 0, 3) {
  //     FOR(i, 1, ys) FOR(j, 1, xs) {
  //         cout << can[k][i][j];
  //         if (j == xs) cout << '\\n';
  //         else cout << ' ';
  //     }
  //     cout << endl;
  // }
  // cout << sx << ' ' << sy << endl;

  queue<pii> q;
  q.push({sy, sx});
  vis[sy][sx] = 1;
  while (!q.empty()) {
    pii u = q.front();
    q.pop();
    FOR(i, 0, 3) {
      pii v = {u.fi + ch[i].fi, u.se + ch[i].se};
      if (!can[i][u.fi][u.se] || v.fi < 1 || v.fi > ys || v.se < 1 || v.se > xs)
        continue;
      if (!vis[v.fi][v.se]) {
        vis[v.fi][v.se] = 1;
        q.push(v);
      }
    }
  }

  bool inf = 0;
  FOR(i, 1, ys)
  if (vis[i][1] || vis[i][xs])
    inf = 1;
  FOR(i, 1, xs)
  if (vis[1][i] || vis[ys][i])
    inf = 1;
  if (inf)
    return cout << "INF\\n", 0;

  ll ans = 0;
  FOR(i, 1, ys) FOR(j, 1, xs) {
    if (vis[i][j]) {
      ans += (y[i] - y[i - 1]) * (x[j] - x[j - 1]);
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define FOR(i, a, b) for (int i = a; i <= (b); i++)
#define ROF(i, a, b) for (int i = a; i >= (b); i--)

using pii = pair<int, int>;
using vpii = vector<pii>;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vll = vector<ll>;
using vvll = vector<vll>;

pii ch[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

int main() {
  int n, m;
  cin >> n >> m;
  vi a(n + 1), b(n + 1), c(n + 1), d(m + 1), e(m + 1), f(m + 1);
  vll x, y;

  const int MX = 1e9;
  x.pb(-(MX + 5));
  x.pb(MX + 5);
  y.pb(-(MX + 5));
  y.pb(MX + 5);

  FOR(i, 1, n) {
    cin >> a[i] >> b[i] >> c[i];
    x.pb(a[i]);
    x.pb(b[i]);
    y.pb(c[i]);
  }

  FOR(i, 1, m) {
    cin >> d[i] >> e[i] >> f[i];
    x.pb(d[i]);
    y.pb(e[i]);
    y.pb(f[i]);
  }

  sort(all(x));
  sort(all(y));
  auto xt = unique(all(x));
  auto yt = unique(all(y));
  x.erase(xt, x.end());
  y.erase(yt, y.end());

  int sx = lower_bound(all(x), 0) - x.begin(),
      sy = lower_bound(all(y), 0) - y.begin();
  int xs = x.size() - 1, ys = y.size() - 1;

  // cout << xs << ' ' << ys << endl;
  // for (int i : x) cout << i << ' ';
  // cout << endl;
  // for (int i : y) cout << i << ' ';
  // cout << endl;
  // return 0;

  using vb = vector<bool>;
  using vvb = vector<vb>;
  vvb vis(xs + 1, vb(ys + 1));
  vector<vvb> can(4, vvb(xs + 1, vb(ys + 1, 1)));

  FOR(i, 1, n) {
    int al = lower_bound(all(x), a[i]) - x.begin();
    int bl = lower_bound(all(x), b[i]) - x.begin();
    int cl = lower_bound(all(y), c[i]) - y.begin();
    FOR(j, al, bl - 1)
    can[2][j][cl - 1] = can[3][j][cl] = 0;
  }
  FOR(i, 1, m) {
    int dl = lower_bound(all(x), d[i]) - x.begin();
    int el = lower_bound(all(y), e[i]) - y.begin();
    int fl = lower_bound(all(y), f[i]) - y.begin();
    // cout << dl << ' ' << el << ' ' << fl << endl;
    FOR(j, el, fl - 1)
    can[0][dl - 1][j] = can[1][dl][j] = 0;
  }

  // FOR(k, 0, 3) {
  //     FOR(i, 1, ys) FOR(j, 1, xs) {
  //         cout << can[k][i][j];
  //         if (j == xs) cout << '\\n';
  //         else cout << ' ';
  //     }
  //     cout << endl;
  // }
  // cout << sx << ' ' << sy << endl;

  queue<pii> q;
  q.push({sx, sy});
  vis[sx][sy] = 1;
  while (!q.empty()) {
    pii u = q.front();
    q.pop();
    FOR(i, 0, 3) {
      pii v = {u.fi + ch[i].fi, u.se + ch[i].se};
      if (!can[i][u.fi][u.se] || v.fi < 1 || v.fi > xs || v.se < 1 || v.se > ys)
        continue;
      if (!vis[v.fi][v.se]) {
        vis[v.fi][v.se] = 1;
        q.push(v);
      }
    }
  }

  // FOR(i, 1, ys) FOR(j, 1, xs) {
  //     cout << vis[i][j];
  //     if (j == xs) cout << endl;
  //     else cout << ' ';
  // }
  bool inf = 0;
  FOR(i, 1, xs)
  if (vis[i][1] || vis[i][ys])
    inf = 1;
  FOR(i, 1, ys)
  if (vis[1][i] || vis[xs][i])
    inf = 1;
  if (inf)
    return cout << "INF\\n", 0;

  ll ans = 0;
  FOR(i, 1, ys) FOR(j, 1, xs) {
    if (vis[i][j])
      ans += (y[i] - y[i - 1]) * (x[j] - x[j - 1]);
  }
  cout << ans << endl;
}