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

// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC target("avx")
#include <bits/stdc++.h>

using namespace std;

#define mp make_pair
#define pb push_back
#define makeunique(x) sort(all(x)), (x).resize(unique(all(x)) - (x).begin())
#define re return
#define fi first
#define se second
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define sqrt(x) sqrt(abs(x))
#define y0 y3487465
#define y1 y8687969
#define j0 j5743892
#define j1 j542893

typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double D;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<string> vs;
typedef vector<vi> vvi;

template <class T> T abs(T x) { re x > 0 ? x : -x; }
template <class T> T gcd(T a, T b) { re a ? gcd(b % a, a) : b; }
template <class T> T sqr(T a) { re a *a; }
template <class T> T sgn(T a) { re a > 0 ? 1 : (a < 0 ? -1 : 0); }

const double pi = acos(-1.0);

#define filename ""

int n;
int m;

const int N = 4100;
char to[N][N][4], was[N][N];

int main() {
  //  freopen (filename".in", "r", stdin);
  //  freopen (filename".out", "w", stdout);
  vi dx = {-1, 0, 1, 0};
  vi dy = {0, 1, 0, -1};
  cin >> n >> m;
  vi a(n), b(n), c(n);
  map<int, int> from_x, from_y;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    from_x[a[i]] = 0;
    from_x[b[i]] = 0;
    from_y[c[i]] = 0;
  }
  vi d(m), e(m), f(m);
  for (int i = 0; i < m; i++) {
    cin >> d[i] >> e[i] >> f[i];
    from_x[d[i]] = 0;
    from_y[e[i]] = 0;
    from_y[f[i]] = 0;
  }

  from_x[0] = 0;
  from_y[0] = 0;

  vi to_x(sz(from_x) + 10), to_y(sz(from_y) + 10);

  int kx = 1;
  for (auto &q : from_x) {
    q.se = kx;
    to_x[kx] = q.fi;
    kx++;
  }

  int ky = 1;
  for (auto &q : from_y) {
    q.se = ky;
    to_y[ky] = q.fi;
    ky++;
  }

  for (int i = 0; i < n; i++) {
    int xa = from_x[a[i]], xb = from_x[b[i]], yc = from_y[c[i]];
    for (int j = xa; j < xb; j++) {
      to[j][yc - 1][1] = -1;
      to[j][yc][3] = -1;
    }
  }
  for (int i = 0; i < m; i++) {
    int xd = from_x[d[i]], ye = from_y[e[i]], yf = from_y[f[i]];
    for (int j = ye; j < yf; j++) {
      to[xd][j][0] = -1;
      to[xd - 1][j][2] = -1;
    }
  }
  vii q = {{from_x[0], from_y[0]}};
  ll ans = 0;
  was[q[0].fi][q[0].se] = 1;
  for (int l = 0; l < sz(q); l++) {
    ii cur = q[l];
    int x = cur.fi, y = cur.se;
    if (x == 0 || y == 0 || x == kx - 1 || y == ky - 1) {
      cout << "INF" << endl;
      re 0;
    }
    ans += ((ll)to_x[x + 1] - to_x[x]) * (to_y[y + 1] - to_y[y]);
    for (int k = 0; k < 4; k++) {
      int xc = cur.fi + dx[k], yc = cur.se + dy[k];
      if (to[x][y][k] != -1 && !was[xc][yc]) {
        q.pb(mp(xc, yc));
        was[xc][yc] = 1;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC target("avx")
#include <bits/stdc++.h>

using namespace std;

#define mp make_pair
#define pb push_back
#define makeunique(x) sort(all(x)), (x).resize(unique(all(x)) - (x).begin())
#define re return
#define fi first
#define se second
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define sqrt(x) sqrt(abs(x))
#define y0 y3487465
#define y1 y8687969
#define j0 j5743892
#define j1 j542893

typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double D;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<string> vs;
typedef vector<vi> vvi;

template <class T> T abs(T x) { re x > 0 ? x : -x; }
template <class T> T gcd(T a, T b) { re a ? gcd(b % a, a) : b; }
template <class T> T sqr(T a) { re a *a; }
template <class T> T sgn(T a) { re a > 0 ? 1 : (a < 0 ? -1 : 0); }

const double pi = acos(-1.0);

#define filename ""

int n;
int m;

const int N = 3100;
char to[N][N][4], was[N][N];

int main() {
  //  freopen (filename".in", "r", stdin);
  //  freopen (filename".out", "w", stdout);
  vi dx = {-1, 0, 1, 0};
  vi dy = {0, 1, 0, -1};
  cin >> n >> m;
  vi a(n), b(n), c(n);
  map<int, int> from_x, from_y;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    from_x[a[i]] = 0;
    from_x[b[i]] = 0;
    from_y[c[i]] = 0;
  }
  vi d(m), e(m), f(m);
  for (int i = 0; i < m; i++) {
    cin >> d[i] >> e[i] >> f[i];
    from_x[d[i]] = 0;
    from_y[e[i]] = 0;
    from_y[f[i]] = 0;
  }

  from_x[0] = 0;
  from_y[0] = 0;

  vi to_x(sz(from_x)), to_y(sz(from_y));

  int kx = 1;
  for (auto &q : from_x) {
    q.se = kx;
    to_x[kx] = q.fi;
    kx++;
  }

  int ky = 1;
  for (auto &q : from_y) {
    q.se = ky;
    to_y[ky] = q.fi;
    ky++;
  }

  for (int i = 0; i < n; i++) {
    int xa = from_x[a[i]], xb = from_x[b[i]], yc = from_y[c[i]];
    for (int j = xa; j < xb; j++) {
      to[j][yc - 1][1] = -1;
      to[j][yc][3] = -1;
    }
  }
  for (int i = 0; i < m; i++) {
    int xd = from_x[d[i]], ye = from_y[e[i]], yf = from_y[f[i]];
    for (int j = ye; j < yf; j++) {
      to[xd][j][0] = -1;
      to[xd - 1][j][2] = -1;
    }
  }
  vii q = {{from_x[0], from_y[0]}};
  ll ans = 0;
  was[q[0].fi][q[0].se] = 1;
  for (int l = 0; l < sz(q); l++) {
    ii cur = q[l];
    int x = cur.fi, y = cur.se;
    if (x == 0 || y == 0 || x == kx - 1 || y == ky - 1) {
      cout << "INF" << endl;
      re 0;
    }
    ans += ((ll)to_x[x + 1] - to_x[x]) * (to_y[y + 1] - to_y[y]);
    for (int k = 0; k < 4; k++) {
      int xc = cur.fi + dx[k], yc = cur.se + dy[k];
      if (to[x][y][k] != -1 && !was[xc][yc]) {
        q.pb(mp(xc, yc));
        was[xc][yc] = 1;
      }
    }
  }
  cout << ans << endl;
  return 0;
}