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

#include <bits/stdc++.h>
#define all(X) (X).begin(), (X).end()
#define rall(X) (X).rbegin(), (X).rend()
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define ff first
#define ss second
#define P 1000000007
#define CASET                                                                  \\
  int ___T;                                                                    \\
  scanf("%d", &___T);                                                          \\
  for (int cs = 1; cs <= ___T; cs++)

using namespace std;
using ll = long long;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

int MAXN = 4010, INF = 1e9 + 7;

int n, m;
vi a, b, c, d, e, f, xs, ys;
vector<vi> u(MAXN, vi(MAXN, 0)), dd(MAXN, vi(MAXN, 0)), r(MAXN, vi(MAXN, 0)),
    l(MAXN, vi(MAXN, 0)), v(MAXN, vi(MAXN, 0));

// bool check(int x, int y) {
//	return (x >= 0 && y >= 0 && x < xs.size() - 1 && y < ys.size() - 1);
// }
//
// void bfs(int x, int y) {
//	v[x][y] = 1;
//	queue<ii> q;
//	q.push({x, y});
//	while(!q.empty()) {
//		int xn = q.front().ff, yn = q.front().ss; q.pop();
//		if(!u[xn][yn] && check(xn + 1 ,yn) && !v[xn + 1][yn]) {
//			v[xn + 1][yn] = 1;
//			q.push({xn + 1, yn});
//		}
//
//		if(!r[xn][yn] && check(xn, yn + 1) && !v[xn][yn + 1]) {
//			v[xn][yn + 1] = 1;
//			q.push({xn, yn + 1});
//		}
//
//		if(!l[xn][yn] && check(xn, yn - 1) && !v[xn][yn - 1]) {
//			v[xn][yn - 1] = 1;
//			q.push({xn, yn - 1});
//		}
//
//		if(!dd[xn][yn] && check(xn - 1, yn) && !v[xn - 1][yn]) {
//			v[xn - 1][yn] = 1;
//			q.push({xn - 1, yn});
//		}
//	}
// }

int dir[][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

void bfs(int x, int y) {
  v[x][y] = 1;
  queue<ii> q;
  q.push({x, y});
  while (q.size()) {
    int x = q.front().ff, y = q.front().ss;
    q.pop();
    for (int i = 0; i < 4; i++) {
      if (i == 0 && u[x][y])
        continue;
      if (i == 1 && dd[x][y])
        continue;
      if (i == 2 && r[x][y])
        continue;
      if (i == 3 && l[x][y])
        continue;
      int xx = x + dir[i][0];
      int yy = y + dir[i][1];
      if (xx < 0 || yy < 0)
        continue;
      if (xx >= xs.size() - 1 || yy >= ys.size() - 1)
        continue;
      if (v[xx][yy])
        continue;
      v[xx][yy] = 1;
      q.push({xx, yy});
    }
  }
}

void solve() {
  cin >> n >> m;

  xs.pub(-INF);
  xs.pub(INF);
  ys.pub(-INF);
  ys.pub(INF);
  for (int i = 0; i < n; i++) {
    int aa, bb, cc;
    cin >> aa >> bb >> cc;
    a.pub(aa);
    b.pub(bb);
    c.pub(cc);
    xs.pub(a[i]);
    xs.pub(b[i]);
    ys.pub(c[i]);
  }

  for (int i = 0; i < m; i++) {
    int dd, ee, ff;
    cin >> dd >> ee >> ff;
    d.pub(dd);
    e.pub(ee);
    f.pub(ff);
    xs.pub(d[i]);
    ys.pub(e[i]);
    ys.pub(f[i]);
  }

  sort(all(xs));
  sort(all(ys));

  xs.resize(unique(all(xs)) - xs.begin());
  ys.resize(unique(all(ys)) - ys.begin());

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

  for (int i = 0; i < n; i++) {
    int aa = lower_bound(all(xs), a[i]) - xs.begin();
    int bb = lower_bound(all(xs), b[i]) - xs.begin();
    int cc = lower_bound(all(ys), c[i]) - ys.begin();
    for (int j = aa; j < bb; j++) {
      u[j][cc - 1] = 1;
      dd[j][cc] = 1;
    }
  }
  for (int i = 0; i < m; i++) {
    int ddd = lower_bound(all(xs), d[i]) - xs.begin();
    int ee = lower_bound(all(ys), e[i]) - ys.begin();
    int ff = lower_bound(all(ys), f[i]) - ys.begin();
    for (int j = ee; j < ff; j++) {
      r[ddd - 1][j] = 1;
      l[ddd][j] = 1;
    }
  }
  //	for(int i = 0; i < n; i++) {
  //		int aa = lower_bound(all(xs), a[i]) - xs.begin();
  //		int bb = lower_bound(all(xs), b[i]) - xs.begin();
  //		int cc = lower_bound(all(ys), c[i]) - ys.begin();
  //		for(int j = aa; j < bb; j++) {
  //			u[j][cc - 1] = 1;
  //			dd[j][cc] = 1;
  //		}
  //	}
  //
  //	for(int i = 0; i < n; i++) {
  //		int ddd = lower_bound(all(xs), d[i]) - xs.begin();
  //		int ee = lower_bound(all(ys), e[i]) - ys.begin();
  //		int ff = lower_bound(all(ys), f[i]) - ys.begin();
  //		for(int j = ee; j < ff; j++) {
  //			r[ddd - 1][j] = 1;
  //			l[ddd][j] = 1;
  //		}
  //	}

  bfs(x, y);

  if (v[0][0] || v[xs.size() - 1][ys.size() - 1]) {
    cout << "INF" << endl;
    return;
  }

  ll ans = 0;
  for (int i = 0; i < xs.size() - 1; i++) {
    for (int j = 0; j < ys.size() - 1; j++) {
      if (v[i][j]) {
        ans += (ll)(xs[i + 1] - xs[i]) * (ys[j + 1] - ys[j]);
      }
    }
  }

  cout << ans << endl;
}

int main() {
#define MULTITEST 0
#if MULTITEST

  CASET { solve(); }

#else

  solve();

#endif

  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
#define all(X) (X).begin(), (X).end()
#define rall(X) (X).rbegin(), (X).rend()
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define ff first
#define ss second
#define P 1000000007
#define CASET                                                                  \\
  int ___T;                                                                    \\
  scanf("%d", &___T);                                                          \\
  for (int cs = 1; cs <= ___T; cs++)

using namespace std;
using ll = long long;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;

int MAXN = 4010, INF = 1e9 + 7;

int n, m;
vi a, b, c, d, e, f, xs, ys;
vector<vi> u(MAXN, vi(MAXN, 0)), dd(MAXN, vi(MAXN, 0)), r(MAXN, vi(MAXN, 0)),
    l(MAXN, vi(MAXN, 0)), v(MAXN, vi(MAXN, 0));

// bool check(int x, int y) {
//	return (x >= 0 && y >= 0 && x < xs.size() - 1 && y < ys.size() - 1);
// }
//
// void bfs(int x, int y) {
//	v[x][y] = 1;
//	queue<ii> q;
//	q.push({x, y});
//	while(!q.empty()) {
//		int xn = q.front().ff, yn = q.front().ss; q.pop();
//		if(!u[xn][yn] && check(xn + 1 ,yn) && !v[xn + 1][yn]) {
//			v[xn + 1][yn] = 1;
//			q.push({xn + 1, yn});
//		}
//
//		if(!r[xn][yn] && check(xn, yn + 1) && !v[xn][yn + 1]) {
//			v[xn][yn + 1] = 1;
//			q.push({xn, yn + 1});
//		}
//
//		if(!l[xn][yn] && check(xn, yn - 1) && !v[xn][yn - 1]) {
//			v[xn][yn - 1] = 1;
//			q.push({xn, yn - 1});
//		}
//
//		if(!dd[xn][yn] && check(xn - 1, yn) && !v[xn - 1][yn]) {
//			v[xn - 1][yn] = 1;
//			q.push({xn - 1, yn});
//		}
//	}
// }

int dir[][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

void bfs(int x, int y) {
  v[x][y] = 1;
  queue<ii> q;
  q.push({x, y});
  while (q.size()) {
    int x = q.front().ff, y = q.front().ss;
    q.pop();
    for (int i = 0; i < 4; i++) {
      if (i == 0 && u[x][y])
        continue;
      if (i == 1 && dd[x][y])
        continue;
      if (i == 2 && r[x][y])
        continue;
      if (i == 3 && l[x][y])
        continue;
      int xx = x + dir[i][0];
      int yy = y + dir[i][1];
      if (xx < 0 || yy < 0)
        continue;
      if (xx >= xs.size() - 1 || yy >= ys.size() - 1)
        continue;
      if (v[xx][yy])
        continue;
      v[xx][yy] = 1;
      q.push({xx, yy});
    }
  }
}

void solve() {
  cin >> n >> m;

  xs.pub(-INF);
  xs.pub(INF);
  ys.pub(-INF);
  ys.pub(INF);
  for (int i = 0; i < n; i++) {
    int aa, bb, cc;
    cin >> aa >> bb >> cc;
    a.pub(aa);
    b.pub(bb);
    c.pub(cc);
    xs.pub(a[i]);
    xs.pub(b[i]);
    ys.pub(c[i]);
  }

  for (int i = 0; i < m; i++) {
    int dd, ee, ff;
    cin >> dd >> ee >> ff;
    d.pub(dd);
    e.pub(ee);
    f.pub(ff);
    xs.pub(d[i]);
    ys.pub(e[i]);
    ys.pub(f[i]);
  }

  sort(all(xs));
  sort(all(ys));

  xs.resize(unique(all(xs)) - xs.begin());
  ys.resize(unique(all(ys)) - ys.begin());

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

  for (int i = 0; i < n; i++) {
    int aa = lower_bound(all(xs), a[i]) - xs.begin();
    int bb = lower_bound(all(xs), b[i]) - xs.begin();
    int cc = lower_bound(all(ys), c[i]) - ys.begin();
    for (int j = aa; j < bb; j++) {
      u[j][cc - 1] = 1;
      dd[j][cc] = 1;
    }
  }

  for (int i = 0; i < n; i++) {
    int ddd = lower_bound(all(xs), d[i]) - xs.begin();
    int ee = lower_bound(all(ys), e[i]) - ys.begin();
    int ff = lower_bound(all(ys), f[i]) - ys.begin();
    for (int j = ee; j < ff; j++) {
      r[ddd - 1][j] = 1;
      l[ddd][j] = 1;
    }
  }

  bfs(x, y);

  if (v[0][0] || v[xs.size() - 1][ys.size() - 1]) {
    cout << "INF" << endl;
    return;
  }

  ll ans = 0;
  for (int i = 0; i < xs.size() - 1; i++) {
    for (int j = 0; j < ys.size() - 1; j++) {
      if (v[i][j]) {
        ans += (ll)(xs[i + 1] - xs[i]) * (ys[j + 1] - ys[j]);
      }
    }
  }

  cout << ans << endl;
}

int main() {
#define MULTITEST 0
#if MULTITEST

  CASET { solve(); }

#else

  solve();

#endif

  return 0;
}