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


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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T, typename U>
using ordered_map =
    tree<T, U, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<str> vstr;

#define FOR(i, j, k, in) for (int i = (j); i < (k); i += in)
#define FORD(i, j, k, in) for (int i = (j); i >= (k); i -= in)
#define REP(i, b) FOR(i, 0, b, 1)
#define REPD(i, b) FORD(i, b, 0, 1)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) begin(x), end(x)
#define rsz resize

const double EPS = 1e-9;
const int MOD = 1e9 + 7; // 998244353;
const ll INFF = 1e18;
const int INF = 1e9;
const ld PI = acos((ld)-1);
const vi dy = {1, 0, -1, 0, -1, 1, 1, -1};
const vi dx = {0, 1, 0, -1, -1, 1, -1, 1};

#ifdef DEBUG
#define DBG if (1)
#else
#define DBG if (0)
#endif

#define dbg(x) cout << "(" << #x << " : " << x << ")" << endl;
// ostreams
template <class T, class U>
ostream &operator<<(ostream &out, const pair<T, U> &par) {
  out << "[" << par.first << ";" << par.second << "]";
  return out;
}
template <class T> ostream &operator<<(ostream &out, const set<T> &cont) {
  out << "{";
  for (const auto &x : cont)
    out << x << ", ";
  out << "}";
  return out;
}
template <class T, class U>
ostream &operator<<(ostream &out, const map<T, U> &cont) {
  out << "{";
  for (const auto &x : cont)
    out << x << ", ";
  out << "}";
  return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << "[";
  REP(i, v.size()) out << v[i] << ", ";
  out << "]";
  return out;
}
// istreams
template <class T> istream &operator>>(istream &in, vector<T> &v) {
  for (auto &x : v)
    in >> x;
  return in;
}
template <class T, class U> istream &operator>>(istream &in, pair<T, U> &p) {
  in >> p.ff >> p.ss;
  return in;
}
// searches
template <typename T, typename U> T bsl(T lo, T hi, U f) {
  hi++;
  T mid;
  while (lo < hi) {
    mid = (lo + hi) / 2;
    f(mid) ? hi = mid : lo = mid + 1;
  }
  return lo;
}
template <typename U> double bsld(double lo, double hi, U f, double p = 1e-9) {
  int r = 3 + (int)log2((hi - lo) / p);
  double mid;
  while (r--) {
    mid = (lo + hi) / 2;
    f(mid) ? hi = mid : lo = mid;
  }
  return (lo + hi) / 2;
}
template <typename T, typename U> T bsh(T lo, T hi, U f) {
  lo--;
  T mid;
  while (lo < hi) {
    mid = (lo + hi + 1) / 2;
    f(mid) ? lo = mid : hi = mid - 1;
  }
  return lo;
}
template <typename U> double bshd(double lo, double hi, U f, double p = 1e-9) {
  int r = 3 + (int)log2((hi - lo) / p);
  double mid;
  while (r--) {
    mid = (lo + hi) / 2;
    f(mid) ? lo = mid : hi = mid;
  }
  return (lo + hi) / 2;
}
// some more utility functions
template <typename T> pair<T, int> get_min(vector<T> &v) {
  typename vector<T>::iterator it = min_element(v.begin(), v.end());
  return mp(*it, it - v.begin());
}
template <typename T> pair<T, int> get_max(vector<T> &v) {
  typename vector<T>::iterator it = max_element(v.begin(), v.end());
  return mp(*it, it - v.begin());
}
template <typename T> bool ckmin(T &a, const T &b) {
  return b < a ? a = b, true : false;
}
template <typename T> bool ckmax(T &a, const T &b) {
  return b > a ? a = b, true : false;
}

struct AcAutomaton {
  int n, m;
  vi aa, bb, cc, dd, ee, ff;
  void read_in() {
    cin >> n >> m;
    aa.rsz(n);
    bb.rsz(n);
    cc.rsz(n);
    dd.rsz(m);
    ee.rsz(m);
    ff.rsz(m);
    REP(i, n) cin >> aa[i] >> bb[i] >> cc[i];
    REP(i, m) cin >> dd[i] >> ee[i] >> ff[i];
  }
  void solve() {
    vector<vi> arr(6020, vi(6020, 0));
    vi x, y;
    x.pb(0);
    y.pb(0);
    REP(i, n) {
      y.pb(aa[i]);
      y.pb(bb[i]);
      x.pb(cc[i]);
    }
    REP(i, m) {
      y.pb(dd[i]);
      x.pb(ee[i]);
      x.pb(ff[i]);
    }
    sort(all(x));
    sort(all(y));
    x.erase(unique(all(x)), x.end());
    y.erase(unique(all(y)), y.end());
    REP(i, n) {
      int c = lower_bound(all(x), cc[i]) - x.begin();
      int a = lower_bound(all(y), aa[i]) - y.begin();
      int b = lower_bound(all(y), bb[i]) - y.begin();
      c = 2 * c + 1;
      a = 2 * a + 1;
      b = 2 * b + 1;
      FOR(j, a, b + 1, 1) arr[j][c] = -1;
    }
    REP(i, m) {
      int d = lower_bound(all(y), dd[i]) - y.begin();
      int e = lower_bound(all(x), ee[i]) - x.begin();
      int f = lower_bound(all(x), ff[i]) - x.begin();
      d = 2 * d + 1;
      e = 2 * e + 1;
      f = 2 * f + 1;
      FOR(j, e, f + 1, 1) arr[d][j] = -1;
    }
    auto bfs = [&](pii s) {
      queue<pii> q;
      q.push(s);
      arr[s.ff][s.ss] = 1;
      while (!q.empty()) {
        pii v = q.front();
        q.pop();
        REP(sm, 4) {
          int ny = v.ff + dy[sm];
          int nx = v.ss + dx[sm];
          if (ny > 2 * y.size() || ny < 0 || nx > 2 * x.size() || nx < 0)
            continue;
          if (arr[ny][nx] == 0) {
            arr[ny][nx] = 1;
            q.push(mp(ny, nx));
          }
        }
      }
    };
    int sy = 2 * (lower_bound(all(y), 0) - y.begin()) + 1;
    int sx = 2 * (lower_bound(all(x), 0) - x.begin()) + 1;
    bfs(mp(sy, sx));
    /* REP(i, pos.size()){ */
    /*     REP(j, pos[i].size()){ */
    /*         cout << (!visited[i][j] ? ".": "*"); */
    /*     } */
    /*     cout << endl; */
    /* } */
    bool infinite = false;
    REP(i, 2 * x.size() + 1) {
      if (arr[0][i] == 1 || arr[2 * y.size()][i] == 1)
        infinite = true;
    }
    REP(i, 2 * y.size() + 1) {
      if (arr[i][0] == 1 || arr[i][2 * x.size()] == 1)
        infinite = true;
    }
    ll ans = 0;
    if (infinite) {
      cout << "INF" << endl;
      return;
    }
    REP(i, y.size() - 1) {
      REP(j, x.size() - 1) {
        if (arr[2 * i + 2][2 * j + 2] == 1) {
          ll dyy = y[i + 1] - y[i];
          ll dxx = x[j + 1] - x[j];
          ans += dyy * dxx;
        }
      }
    }
    cout << ans << endl;
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int tcase = 1;
  while (tcase--) {
    AcAutomaton solver;
    solver.read_in();
    solver.solve();
  }
  return 0;
}

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


Fixed submission:


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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T, typename U>
using ordered_map =
    tree<T, U, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<str> vstr;

#define FOR(i, j, k, in) for (int i = (j); i < (k); i += in)
#define FORD(i, j, k, in) for (int i = (j); i >= (k); i -= in)
#define REP(i, b) FOR(i, 0, b, 1)
#define REPD(i, b) FORD(i, b, 0, 1)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) begin(x), end(x)
#define rsz resize

const double EPS = 1e-9;
const int MOD = 1e9 + 7; // 998244353;
const ll INFF = 1e18;
const int INF = 1e9;
const ld PI = acos((ld)-1);
const vi dy = {1, 0, -1, 0, -1, 1, 1, -1};
const vi dx = {0, 1, 0, -1, -1, 1, -1, 1};

#ifdef DEBUG
#define DBG if (1)
#else
#define DBG if (0)
#endif

#define dbg(x) cout << "(" << #x << " : " << x << ")" << endl;
// ostreams
template <class T, class U>
ostream &operator<<(ostream &out, const pair<T, U> &par) {
  out << "[" << par.first << ";" << par.second << "]";
  return out;
}
template <class T> ostream &operator<<(ostream &out, const set<T> &cont) {
  out << "{";
  for (const auto &x : cont)
    out << x << ", ";
  out << "}";
  return out;
}
template <class T, class U>
ostream &operator<<(ostream &out, const map<T, U> &cont) {
  out << "{";
  for (const auto &x : cont)
    out << x << ", ";
  out << "}";
  return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << "[";
  REP(i, v.size()) out << v[i] << ", ";
  out << "]";
  return out;
}
// istreams
template <class T> istream &operator>>(istream &in, vector<T> &v) {
  for (auto &x : v)
    in >> x;
  return in;
}
template <class T, class U> istream &operator>>(istream &in, pair<T, U> &p) {
  in >> p.ff >> p.ss;
  return in;
}
// searches
template <typename T, typename U> T bsl(T lo, T hi, U f) {
  hi++;
  T mid;
  while (lo < hi) {
    mid = (lo + hi) / 2;
    f(mid) ? hi = mid : lo = mid + 1;
  }
  return lo;
}
template <typename U> double bsld(double lo, double hi, U f, double p = 1e-9) {
  int r = 3 + (int)log2((hi - lo) / p);
  double mid;
  while (r--) {
    mid = (lo + hi) / 2;
    f(mid) ? hi = mid : lo = mid;
  }
  return (lo + hi) / 2;
}
template <typename T, typename U> T bsh(T lo, T hi, U f) {
  lo--;
  T mid;
  while (lo < hi) {
    mid = (lo + hi + 1) / 2;
    f(mid) ? lo = mid : hi = mid - 1;
  }
  return lo;
}
template <typename U> double bshd(double lo, double hi, U f, double p = 1e-9) {
  int r = 3 + (int)log2((hi - lo) / p);
  double mid;
  while (r--) {
    mid = (lo + hi) / 2;
    f(mid) ? lo = mid : hi = mid;
  }
  return (lo + hi) / 2;
}
// some more utility functions
template <typename T> pair<T, int> get_min(vector<T> &v) {
  typename vector<T>::iterator it = min_element(v.begin(), v.end());
  return mp(*it, it - v.begin());
}
template <typename T> pair<T, int> get_max(vector<T> &v) {
  typename vector<T>::iterator it = max_element(v.begin(), v.end());
  return mp(*it, it - v.begin());
}
template <typename T> bool ckmin(T &a, const T &b) {
  return b < a ? a = b, true : false;
}
template <typename T> bool ckmax(T &a, const T &b) {
  return b > a ? a = b, true : false;
}

struct AcAutomaton {
  int n, m;
  vector<vi> vert;
  vector<vi> hor;
  void read_in() {
    cin >> n >> m;
    vert.rsz(n, vi(3));
    hor.rsz(m, vi(3));
    cin >> vert >> hor;
  }
  void solve() {
    vector<vi> arr(6000, vi(6000, 0));
    vi y;
    y.pb(0);
    REP(i, n) {
      y.pb(vert[i][0]);
      y.pb(vert[i][1]);
    }
    REP(i, m) y.pb(hor[i][0]);
    vi x;
    x.pb(0);
    REP(i, m) {
      x.pb(hor[i][1]);
      x.pb(hor[i][2]);
    }
    REP(i, n) x.pb(vert[i][2]);
    sort(all(x));
    sort(all(y));
    x.erase(unique(all(x)), x.end());
    y.erase(unique(all(y)), y.end());
    REP(i, vert.size()) {
      int c = lower_bound(all(x), vert[i][2]) - x.begin();
      int a = lower_bound(all(y), vert[i][0]) - y.begin();
      int b = lower_bound(all(y), vert[i][1]) - y.begin();
      c = 2 * c + 1;
      a = 2 * a + 1;
      b = 2 * b + 1;
      FOR(j, a, b + 1, 1) arr[j][c] = -1;
    }
    REP(i, hor.size()) {
      int d = lower_bound(all(y), hor[i][0]) - y.begin();
      int e = lower_bound(all(x), hor[i][1]) - x.begin();
      int f = lower_bound(all(x), hor[i][2]) - x.begin();
      d = 2 * d + 1;
      e = 2 * e + 1;
      f = 2 * f + 1;
      FOR(j, e, f + 1, 1) arr[d][j] = -1;
    }
    auto bfs = [&](pii s) {
      queue<pii> q;
      q.push(s);
      arr[s.ff][s.ss] = 1;
      while (!q.empty()) {
        pii v = q.front();
        q.pop();
        REP(sm, 4) {
          int ny = v.ff + dy[sm];
          int nx = v.ss + dx[sm];
          if (ny > 2 * y.size() || ny < 0 || nx > 2 * x.size() || nx < 0)
            continue;
          if (arr[ny][nx] == 0) {
            arr[ny][nx] = 1;
            q.push(mp(ny, nx));
          }
        }
      }
    };
    int sy = 2 * (lower_bound(all(y), 0) - y.begin());
    int sx = 2 * (lower_bound(all(x), 0) - x.begin());
    bfs(mp(sy, sx));
    /* REP(i, pos.size()){ */
    /*     REP(j, pos[i].size()){ */
    /*         cout << (!visited[i][j] ? ".": "*"); */
    /*     } */
    /*     cout << endl; */
    /* } */
    bool infinite = false;
    REP(i, 2 * x.size() + 1) {
      if (arr[0][i] == 1 || arr[2 * y.size()][i] == 1)
        infinite = true;
    }
    REP(i, 2 * y.size() + 1) {
      if (arr[i][0] == 1 || arr[i][2 * x.size()] == 1)
        infinite = true;
    }
    ll ans = 0;
    if (infinite) {
      cout << "INF" << endl;
      return;
    }
    REP(i, y.size() - 1) {
      REP(j, x.size() - 1) {
        if (arr[2 * i + 2][2 * j + 2] == 1) {
          ll dyy = y[i + 1] - y[i];
          ll dxx = x[i + 1] - x[i];
          ans += dyy * dxx;
        }
      }
    }
    cout << ans << endl;
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int tcase = 1;
  while (tcase--) {
    AcAutomaton solver;
    solver.read_in();
    solver.solve();
  }
  return 0;
}