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

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define reps(i, n) for (int i = 1; i <= (n); ++i)
#define repd(i, n) for (int i = (n)-1; i >= 0; --i)
#define repds(i, n) for (int i = (n); i > 0; --i)
#define loop(i, x, n) for (int i = (x); i < (n); ++i)
#define loops(i, x, n) for (int i = (x); i <= (n); ++i)
#define loopd(i, x, n) for (int i = (x); i > (n); --i)
#define loopds(i, x, n) for (int i = (x); i >= (n); --i)
#define itrep(i, a) for (auto i = begin(a); i != end(a); ++i)
#define itrepd(i, a) for (auto i = rbegin(a); i != rend(a); ++i)
#define each(i, x) for (auto &&i : x)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(a) ((int)a.size())
#define pow2(n) (n * n)
#define int ll
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template <class T>
using priority_queuel = priority_queue<T, vector<T>, greater<T>>;
constexpr ll inf = static_cast<ll>(1e18);
constexpr int iinf = static_cast<int>(1e9);
constexpr double dinf = 1e10;
constexpr ld ldinf = 1e18;
constexpr double eps = 1e-9;
ll mod = static_cast<ll>(1e9 + 7);
template <class T, class... Args> constexpr void mins(T &a, Args... args) {
  a = min<T>({a, args...});
}
template <class T, class... Args> constexpr void maxs(T &a, Args... args) {
  a = max<T>({a, args...});
}
template <class F> class FixPoint : private F {
public:
  explicit constexpr FixPoint(F &&f) : F(std::forward<F>(f)) {}
  template <class... Args>
  constexpr decltype(auto) operator()(Args &&...args) const {
    return F::operator()(*this, std::forward<Args>(args)...);
  }
};
template <class F> constexpr decltype(auto) funcs(F &&f) {
  return FixPoint<F>{std::forward<F>(f)};
}
template <class T> std::vector<T> vecs(int n, T &&val) {
  return std::vector<T>(std::forward<int>(n), std::forward<T>(val));
}
template <class... Args> decltype(auto) vecs(int n, Args &&...args) {
  return std::vector<decltype(vecs(std::forward<Args>(args)...))>(
      std::forward<int>(n), vecs(std::forward<Args>(args)...));
}
template <class V, class T> void fill(V &x, const T &val) { x = val; }
template <class V, class T> void fill(vector<V> &vec, const T &val) {
  for (auto &&v : vec)
    fill(v, val);
}

void solve() {
  int n, m;
  cin >> n >> m;
  vector<tuple<int, int, int>> xe(n), ye(m);
  map<int, int> mx, my;
  mx[0] = 0;
  my[0] = 0;
  for (auto &[a, b, c] : xe) {
    cin >> a >> b >> c;
    mx[a] = 0;
    mx[b] = 0;
    my[c] = 0;
  }

  for (auto &[a, b, c] : ye) {
    cin >> a >> b >> c;
    mx[a] = 0;
    my[b] = 0;
    my[c] = 0;
  }

  vector<int> xs, ys;
  each(val, mx) {
    val.second = sz(xs) * 2;
    xs.emplace_back(val.first);
  }
  each(val, my) {
    val.second = sz(ys) * 2;
    ys.emplace_back(val.first);
  }

  int h = sz(xs) * 2;
  int w = sz(ys) * 2;
  auto state = vecs(h, w, int(0));
  for (auto &[a, b, c] : xe) {
    auto d = mx[a];
    auto e = mx[b];
    auto f = my[c];
    loops(x, d, e) state[x][f] = 1;
  }
  for (auto &[a, b, c] : ye) {
    auto d = mx[a];
    auto e = my[b];
    auto f = my[c];
    loops(y, e, f) state[d][y] = 1;
  }

  int sx = mx[0], sy = my[0];
  queue<pair<int, int>> que;
  que.emplace(sx, sy);
  const int dx[] = {1, 0, -1, 0};
  const int dy[] = {0, 1, 0, -1};
  state[sx][sy] = 2;
  while (!que.empty()) {
    auto p = que.front();
    que.pop();
    auto x = p.first;
    auto y = p.second;
    rep(i, 4) {
      auto nx = x + dx[i];
      auto ny = y + dy[i];
      if (nx < 0 || nx >= h || ny < 0 || ny >= w)
        continue;
      if (state[nx][ny])
        continue;
      state[nx][ny] = 2;
      que.emplace(nx, ny);
    }
  }

  int ans = 0;
  rep(i, h) rep(j, w) {
    if (state[i][j] != 2)
      continue;
    if (i == 0 || i == h - 1 || j == 0 || j == w - 1) {
      cout << "INF" << endl;
      return;
    }
    if (i % 2 == 0 || j % 2 == 0)
      continue;
    ans += (xs[i / 2 + 1] - xs[i / 2]) * (ys[j / 2 + 1] - ys[j / 2]);
  }
  cout << ans << endl;
}

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
  cout.flush();
  return 0;
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define reps(i, n) for (int i = 1; i <= (n); ++i)
#define repd(i, n) for (int i = (n)-1; i >= 0; --i)
#define repds(i, n) for (int i = (n); i > 0; --i)
#define loop(i, x, n) for (int i = (x); i < (n); ++i)
#define loops(i, x, n) for (int i = (x); i <= (n); ++i)
#define loopd(i, x, n) for (int i = (x); i > (n); --i)
#define loopds(i, x, n) for (int i = (x); i >= (n); --i)
#define itrep(i, a) for (auto i = begin(a); i != end(a); ++i)
#define itrepd(i, a) for (auto i = rbegin(a); i != rend(a); ++i)
#define each(i, x) for (auto &&i : x)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(a) ((int)a.size())
#define pow2(n) (n * n)
#define int ll
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template <class T>
using priority_queuel = priority_queue<T, vector<T>, greater<T>>;
constexpr ll inf = static_cast<ll>(1e18);
constexpr int iinf = static_cast<int>(1e9);
constexpr double dinf = 1e10;
constexpr ld ldinf = 1e18;
constexpr double eps = 1e-9;
ll mod = static_cast<ll>(1e9 + 7);
template <class T, class... Args> constexpr void mins(T &a, Args... args) {
  a = min<T>({a, args...});
}
template <class T, class... Args> constexpr void maxs(T &a, Args... args) {
  a = max<T>({a, args...});
}
template <class F> class FixPoint : private F {
public:
  explicit constexpr FixPoint(F &&f) : F(std::forward<F>(f)) {}
  template <class... Args>
  constexpr decltype(auto) operator()(Args &&...args) const {
    return F::operator()(*this, std::forward<Args>(args)...);
  }
};
template <class F> constexpr decltype(auto) funcs(F &&f) {
  return FixPoint<F>{std::forward<F>(f)};
}
template <class T> std::vector<T> vecs(int n, T &&val) {
  return std::vector<T>(std::forward<int>(n), std::forward<T>(val));
}
template <class... Args> decltype(auto) vecs(int n, Args &&...args) {
  return std::vector<decltype(vecs(std::forward<Args>(args)...))>(
      std::forward<int>(n), vecs(std::forward<Args>(args)...));
}
template <class V, class T> void fill(V &x, const T &val) { x = val; }
template <class V, class T> void fill(vector<V> &vec, const T &val) {
  for (auto &&v : vec)
    fill(v, val);
}

void solve() {
  int n, m;
  cin >> n >> m;
  vector<tuple<int, int, int>> xe(n), ye(m);
  map<int, int> mx, my;
  mx[0] = 0;
  my[0] = 0;
  for (auto &[a, b, c] : xe) {
    cin >> a >> b >> c;
    mx[a] = 0;
    mx[b] = 0;
    my[c] = 0;
  }

  for (auto &&[a, b, c] : ye) {
    cin >> a >> b >> c;
    mx[a] = 0;
    my[b] = 0;
    my[c] = 0;
  }

  vector<int> xs, ys;
  each(val, mx) {
    val.second = sz(xs) * 2;
    xs.emplace_back(val.first);
  }
  each(val, my) {
    val.second = sz(ys) * 2;
    ys.emplace_back(val.first);
  }

  int h = sz(xs) * 2;
  int w = sz(ys) * 2;
  auto state = vecs(h, w, int(0));
  for (auto &&[a, b, c] : xe) {
    auto d = mx[a];
    auto e = mx[b];
    auto f = my[c];
    loops(x, d, e) state[x][f] = 1;
  }
  for (auto &[a, b, c] : ye) {
    auto d = mx[a];
    auto e = my[b];
    auto f = my[c];
    loops(y, e, f) state[d][y] = 1;
  }

  queue<pair<int, int>> que;
  que.emplace(mx[0], my[0]);
  const int dx[] = {1, 0, -1, 0};
  const int dy[] = {0, 1, 0, -1};
  while (!que.empty()) {
    auto p = que.front();
    que.pop();
    auto x = p.first;
    auto y = p.second;
    state[x][y] = 2;
    rep(i, 4) {
      auto nx = x + dx[i];
      auto ny = y + dy[i];
      if (nx < 0 || nx >= h || ny < 0 || ny >= w)
        continue;
      if (!state[nx][ny])
        que.emplace(nx, ny);
    }
  }

  int ans = 0;
  rep(i, h) rep(j, w) {
    if (state[i][j] != 2)
      continue;
    if (i == 0 || i == h - 1 || j == 0 || j == w - 1) {
      cout << "INF" << endl;
      return;
    }
    if (i % 2 == 0 || j % 2 == 0)
      continue;
    ans += (xs[i / 2 + 1] - xs[i / 2]) * (ys[j / 2 + 1] - ys[j / 2]);
  }
  cout << ans << endl;
}

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
  cout.flush();
  return 0;
}