Problem p02680 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for (int i = (int)(s - 1); i >= (int)(e); i--)
#define pb push_back
#define all(r) r.begin(), r.end()
#define rall(r) r.rbegin(), r.rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const double EPS = 1e-8;

template <typename T> T chmax(T &a, const T &b) { return a = (a > b ? a : b); }
template <typename T> T chmin(T &a, const T &b) { return a = (a < b ? a : b); }

#define DEBUG_MODE
#ifdef DEBUG_MODE
#define dump(x) cout << #x << " : " << x << " "
#define dumpL(x) cout << #x << " : " << x << '\\n'
#define LINE cout << "line : " << __LINE__ << " "
#define LINEL cout << "line : " << __LINE__ << '\\n'
#define dumpV(v)                                                               \\
  cout << #v << " : [";                                                        \\
  for (auto &t : v)                                                            \\
    cout << t << ", ";                                                         \\
  cout << "]"                                                                  \\
       << " "
#define dumpVL(v)                                                              \\
  cout << #v << " : [";                                                        \\
  for (auto &t : v)                                                            \\
    cout << t << ", ";                                                         \\
  cout << "]" << endl
#define STOP assert(false)
#else
#define dump(x)
#define dumpL(x)
#define LINE
#define LINEL
#define dumpV(v)
#define dumpVL(v)
#define STOP assert(false)
#endif
#define mp make_pair

namespace std {
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &a) {
  out << '(' << a.fi << ", " << a.se << ')';
  return out;
}
} // namespace std

struct UF { // O(loga(n))
  int n;
  int m;
  vi d, r;
  UF(int n) : n(n), m(n), d(n, -1), r(n, 0){};
  int root(int i) {
    if (d[i] < 0)
      return i;
    return d[i] = root(d[i]);
  }
  bool same(int x, int y) { return root(x) == root(y); }
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;

    if (r[x] < r[y])
      swap(x, y);
    else if (r[x] == r[y])
      r[x]++;
    d[x] += d[y];
    d[y] = x;
    --m;
    return true;
  }
  int size(int i) { return -d[root(i)]; }
  int size() { return m; }
};

int main() {
  int n, m;
  cin >> n >> m;
  vi a(n), b(n), c(n), d(m), e(m), f(m);
  rep(i, n) cin >> a[i] >> b[i] >> c[i];
  rep(i, m) cin >> d[i] >> e[i] >> f[i];
  const int INF = 1e9 + 10;
  map<int, int> mx, my;
  mx[0] = 1;
  my[0] = 1;
  mx[-INF] = 1;
  mx[INF] = 1;
  mx[-INF - 1] = 1;
  mx[INF + 1] = 1;
  my[-INF] = 1;
  my[INF] = 1;
  my[-INF - 1] = 1;
  my[INF + 1] = 1;
  rep(i, n) {
    mx[a[i]] = 1;
    mx[b[i]] = 1;
    my[c[i]] = 1;
  }
  rep(i, m) {
    mx[d[i]] = 1;
    my[e[i]] = 1;
    my[f[i]] = 1;
  }
  {
    int cnt = 0;
    for (auto &p : mx)
      p.se = cnt++;
  }
  {
    int cnt = 0;
    for (auto &p : my)
      p.se = cnt++;
  }
  using B = bitset<4>;
  vector<vector<B>> nxt(my.size() - 1, vector<B>(mx.size() - 1));
  rep(i, nxt.size()) rep(j, nxt[i].size()) nxt[i][j].set();

  int dx[] = {-1, 0, 1, 0};
  int dy[] = {0, -1, 0, 1};

  rep(i, n) {
    auto y = my[c[i]];
    for (auto it = mx.lower_bound(a[i]); it->fi != b[i]; ++it) {
      auto x = it->se;
      nxt[y - 1][x].reset(3);
      nxt[y][x].reset(1);
    }
  }
  rep(i, m) {
    auto x = mx[d[i]];
    for (auto it = my.lower_bound(e[i]); it->fi != f[i]; ++it) {
      auto y = it->se;
      nxt[y][x - 1].reset(2);
      nxt[y][x].reset(0);
    }
  }

  const int sx = mx[-INF], tx = mx[INF];
  const int sy = my[-INF], ty = my[INF];

  vl rx(mx.size()), ry(my.size());
  for (auto &&p : mx)
    rx[p.se] = p.fi;
  for (auto &&p : my)
    ry[p.se] = p.fi;

  vector<vl> area(my.size() - 1, vl(mx.size() - 1));
  queue<pii> q;
  q.push({my[0], mx[0]});
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    int y = p.fi, x = p.se;
    if (area[y][x] != 0)
      continue;
    if (y == sy || y == ty || x == sx || x == tx) {
      cout << "INF" << '\\n';
      return 0;
    }
    { area[y][x] = (ry[y + 1] - ry[y]) * (rx[x + 1] - rx[x]); }
    rep(k, 4) if (nxt[y][x][k]) {
      int ny = y + dy[k], nx = x + dx[k];
      if (area[ny][nx] == 0)
        q.push({ny, nx});
    }
  }
  ll ans = 0LL;
  for (auto &&low : area)
    ans += accumulate(all(low), 0LL);
  cout << ans << '\\n';
  // for(auto&& v: area) {dumpVL(v);};
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for (int i = (int)(s - 1); i >= (int)(e); i--)
#define pb push_back
#define all(r) r.begin(), r.end()
#define rall(r) r.rbegin(), r.rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const double EPS = 1e-8;

template <typename T> T chmax(T &a, const T &b) { return a = (a > b ? a : b); }
template <typename T> T chmin(T &a, const T &b) { return a = (a < b ? a : b); }

#define DEBUG_MODE
#ifdef DEBUG_MODE
#define dump(x) cout << #x << " : " << x << " "
#define dumpL(x) cout << #x << " : " << x << '\\n'
#define LINE cout << "line : " << __LINE__ << " "
#define LINEL cout << "line : " << __LINE__ << '\\n'
#define dumpV(v)                                                               \\
  cout << #v << " : [";                                                        \\
  for (auto &t : v)                                                            \\
    cout << t << ", ";                                                         \\
  cout << "]"                                                                  \\
       << " "
#define dumpVL(v)                                                              \\
  cout << #v << " : [";                                                        \\
  for (auto &t : v)                                                            \\
    cout << t << ", ";                                                         \\
  cout << "]" << endl
#define STOP assert(false)
#else
#define dump(x)
#define dumpL(x)
#define LINE
#define LINEL
#define dumpV(v)
#define dumpVL(v)
#define STOP assert(false)
#endif
#define mp make_pair

namespace std {
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &a) {
  out << '(' << a.fi << ", " << a.se << ')';
  return out;
}
} // namespace std

struct UF { // O(loga(n))
  int n;
  int m;
  vi d, r;
  UF(int n) : n(n), m(n), d(n, -1), r(n, 0){};
  int root(int i) {
    if (d[i] < 0)
      return i;
    return d[i] = root(d[i]);
  }
  bool same(int x, int y) { return root(x) == root(y); }
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;

    if (r[x] < r[y])
      swap(x, y);
    else if (r[x] == r[y])
      r[x]++;
    d[x] += d[y];
    d[y] = x;
    --m;
    return true;
  }
  int size(int i) { return -d[root(i)]; }
  int size() { return m; }
};

int main() {
  int n, m;
  cin >> n >> m;
  vi a(n), b(n), c(n), d(m), e(m), f(m);
  rep(i, n) cin >> a[i] >> b[i] >> c[i];
  rep(i, m) cin >> d[i] >> e[i] >> f[i];
  const int INF = 1e9 + 10;
  map<int, int> mx, my;
  mx[0] = 1;
  my[0] = 1;
  mx[-INF] = 1;
  mx[INF] = 1;
  mx[-INF - 1] = 1;
  mx[INF + 1] = 1;
  my[-INF] = 1;
  my[INF] = 1;
  my[-INF - 1] = 1;
  my[INF + 1] = 1;
  rep(i, n) {
    mx[a[i]] = 1;
    mx[b[i]] = 1;
    my[c[i]] = 1;
  }
  rep(i, m) {
    mx[d[i]] = 1;
    my[e[i]] = 1;
    my[f[i]] = 1;
  }
  {
    int cnt = 0;
    for (auto &p : mx)
      p.se = cnt++;
  }
  {
    int cnt = 0;
    for (auto &p : my)
      p.se = cnt++;
  }
  using B = bitset<4>;
  vector<vector<B>> nxt(my.size() - 1, vector<B>(mx.size() - 1));
  rep(i, nxt.size()) rep(j, nxt[i].size()) nxt[i][j].set();

  int dx[] = {-1, 0, 1, 0};
  int dy[] = {0, -1, 0, 1};

  rep(i, n) {
    auto y = my[c[i]];
    for (auto it = mx.lower_bound(a[i]); it->fi != b[i]; ++it) {
      auto x = it->se;
      nxt[y - 1][x].reset(3);
      nxt[y][x].reset(1);
    }
  }
  rep(i, m) {
    auto x = mx[d[i]];
    for (auto it = my.lower_bound(e[i]); it->fi != f[i]; ++it) {
      auto y = it->se;
      nxt[y][x - 1].reset(2);
      nxt[y][x].reset(0);
    }
  }

  const int sx = mx[-INF], tx = mx[INF];
  const int sy = my[-INF], ty = my[INF];

  vl rx(mx.size()), ry(my.size());
  for (auto &&p : mx)
    rx[p.se] = p.fi;
  for (auto &&p : my)
    ry[p.se] = p.fi;

  vector<vl> area(my.size() - 1, vl(mx.size() - 1));
  queue<pii> q;
  q.push({my[0], mx[0]});
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    int y = p.fi, x = p.se;
    if (y == sy || y == ty || x == sx || x == tx) {
      cout << "INF" << '\\n';
      return 0;
    }
    { area[y][x] = (ry[y + 1] - ry[y]) * (rx[x + 1] - rx[x]); }
    rep(k, 4) if (nxt[y][x][k]) {
      int ny = y + dy[k], nx = x + dx[k];
      if (area[ny][nx] == 0)
        q.push({ny, nx});
    }
  }
  ll ans = 0LL;
  for (auto &&low : area)
    ans += accumulate(all(low), 0LL);
  cout << ans << '\\n';
  // for(auto&& v: area) {dumpVL(v);};
  return 0;
}