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

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

template <class T1, class T2>
ostream &operator<<(ostream &out, const pair<T1, T2> p) {
  out << '(' << p.first << ',' << p.second << ')';
  return out;
}

template <class T1, class T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
  in >> p.first >> p.second;
  return in;
}

template <class T> istream &operator>>(istream &in, vector<T> &v) {
  for (T &x : v)
    in >> x;
  return in;
}

template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> &v) {
  for (const vector<T> &x : v)
    out << x << '\\n';
  return out;
}

template <class T> ostream &operator<<(ostream &out, const vector<T> &v) {
  for (const T &x : v)
    out << x << ' ';
  return out;
}

long long gcd(long long a, long long b) {
  if (b > a)
    swap(a, b);
  return (b ? gcd(b, a % b) : a);
}

using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
using tiii = pair<pair<int, int>, int>;
using vi = vector<int>;
using vl = vector<long long>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<long long>>;

#define F first
#define S second
#define First first.first
#define Second first.second
#define Third second
#define mp make_pair
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, b, a) for (int i = (b); i > (a); i--)
#define all(x) x.begin(), x.end()
#define ret(x) return cout << x, 0;
#define throwex throw runtime_error("Found the error.");

const int h = 1000000007;

signed main() {
  ios::sync_with_stdio(false);
#ifdef ONLINE_JUDGE
  cin.tie(nullptr);
  cout.tie(nullptr);
  cerr.setstate(ios::failbit);
#endif

  int n, m;
  cin >> n >> m;
  vector<tiii> vert(n), horz(m);
  cin >> vert >> horz;
  set<int> all_xs, all_ys;
  for (tiii t : vert) {
    all_xs.insert({t.First, t.Second});
    all_ys.insert(t.Third);
  }
  for (tiii t : horz) {
    all_xs.insert(t.First);
    all_ys.insert({t.Second, t.Third});
  }
  all_xs.insert({0, int(1e9) + 1, -int(1e9) - 1});
  all_ys.insert({0, int(1e9) + 1, -int(1e9) - 1});
  vi xs(all(all_xs)), ys(all(all_ys));
  vvi grid(2 * xs.size() - 1, vi(2 * ys.size() - 1));
  map<int, int> x_index, y_index;
  int in = 0;
  for (int point : xs)
    x_index[point] = in++;
  in = 0;
  for (int point : ys)
    y_index[point] = in++;
  for (tiii t : vert) {
    int si = 2 * x_index[t.First];
    int ei = 2 * x_index[t.Second];
    int j = 2 * y_index[t.Third];
    for (int i = si; i <= ei; i++)
      grid[i][j] = 1;
  }
  for (tiii t : horz) {
    int i = 2 * x_index[t.First];
    int sj = 2 * y_index[t.Second];
    int ej = 2 * y_index[t.Third];
    for (int j = sj; j <= ej; j++)
      grid[i][j] = 1;
  }
  grid[2 * x_index[0]][2 * y_index[0]] = 1;

  stack<pii> st;
  st.push({2 * x_index[0], 2 * y_index[0]});
  vector<pii> dir{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
  while (!st.empty()) {
    pii cur = st.top();
    st.pop();
    for (auto d : dir) {
      pii nex = {cur.F + d.F, cur.S + d.S};
      if (nex.F < 0 or nex.S < 0 or nex.F == grid.size() or
          nex.S == grid[0].size())
        continue;
      if (!grid[nex.F][nex.S]) {
        grid[nex.F][nex.S] = 1;
        st.push(nex);
      }
    }
  }
  rep(i, 0, grid.size()) if (grid[i][0] or grid[i][grid[0].size() - 1])
      ret("INF");
  rep(i, 0, grid[0].size()) if (grid[0][i] or grid[grid.size() - 1][i])
      ret("INF");

  ll ans = 0;
  rep(i, 0, xs.size() - 1) rep(j, 0, ys.size() - 1) {
    bool Y = true;
    for (int x = 2 * i; x <= 2 * i + 2; x++)
      for (int y = 2 * j; y <= 2 * j + 2; y++)
        if (!grid[x][y])
          Y = false;
    if (Y)
      ans += ll(xs[i + 1] - xs[i]) * (ys[j + 1] - ys[j]);
  }

  cerr << grid << '\\n';
  cout << ans;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

template <class T1, class T2>
ostream &operator<<(ostream &out, const pair<T1, T2> p) {
  out << '(' << p.first << ',' << p.second << ')';
  return out;
}

template <class T1, class T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
  in >> p.first >> p.second;
  return in;
}

template <class T> istream &operator>>(istream &in, vector<T> &v) {
  for (T &x : v)
    in >> x;
  return in;
}

template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> &v) {
  for (const vector<T> &x : v)
    out << x << '\\n';
  return out;
}

template <class T> ostream &operator<<(ostream &out, const vector<T> &v) {
  for (const T &x : v)
    out << x << ' ';
  return out;
}

long long gcd(long long a, long long b) {
  if (b > a)
    swap(a, b);
  return (b ? gcd(b, a % b) : a);
}

using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
using tiii = pair<pair<int, int>, int>;
using vi = vector<int>;
using vl = vector<long long>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<long long>>;

#define F first
#define S second
#define First first.first
#define Second first.second
#define Third second
#define mp make_pair
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, b, a) for (int i = (b); i > (a); i--)
#define all(x) x.begin(), x.end()
#define ret(x) return cout << x, 0;
#define throwex throw runtime_error("Found the error.");

const int h = 1000000007;

signed main() {
  ios::sync_with_stdio(false);
#ifdef ONLINE_JUDGE
  cin.tie(nullptr);
  cout.tie(nullptr);
  cerr.setstate(ios::failbit);
#endif

  int n, m;
  cin >> n >> m;
  vector<tiii> vert(n), horz(m);
  cin >> vert >> horz;
  set<int> all_points;
  for (tiii t : vert)
    all_points.insert({t.First, t.Second, t.Third});
  for (tiii t : horz)
    all_points.insert({t.First, t.Second, t.Third});
  all_points.insert(0);
  all_points.insert(int(1e9) + 1);
  all_points.insert(-int(1e9) - 1);
  vi points(all(all_points));
  vvi grid(2 * points.size() - 1, vi(2 * points.size() - 1));
  map<int, int> index;
  int in = 0;
  for (int point : points)
    index[point] = in++;
  for (tiii t : vert) {
    int si = 2 * index[t.First];
    int ei = 2 * index[t.Second];
    int j = 2 * index[t.Third];
    for (int i = si; i <= ei; i++)
      grid[i][j] = 1;
  }
  for (tiii t : horz) {
    int i = 2 * index[t.First];
    int sj = 2 * index[t.Second];
    int ej = 2 * index[t.Third];
    for (int j = sj; j <= ej; j++)
      grid[i][j] = 1;
  }
  grid[2 * index[0]][2 * index[0]] = 1;

  stack<pii> st;
  st.push({2 * index[0], 2 * index[0]});
  vector<pii> dir{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
  while (!st.empty()) {
    pii cur = st.top();
    st.pop();
    for (auto d : dir) {
      pii nex = {cur.F + d.F, cur.S + d.S};
      if (nex.F < 0 or nex.S < 0 or nex.F == grid.size() or
          nex.S == grid.size())
        continue;
      if (!grid[nex.F][nex.S]) {
        grid[nex.F][nex.S] = 1;
        st.push(nex);
      }
    }
  }
  rep(i, 0, grid.size()) if (grid[0][i] or grid[grid.size() - 1][0] or
                             grid[i][0] or grid[0][grid.size() - 1]) ret("INF");

  ll ans = 0;
  rep(i, 0, points.size() - 1) rep(j, 0, points.size() - 1) {
    bool Y = true;
    for (int x = 2 * i; x <= 2 * i + 2; x++)
      for (int y = 2 * j; y <= 2 * j + 2; y++)
        if (!grid[x][y])
          Y = false;
    if (Y)
      ans += ll(points[i + 1] - points[i]) * (points[j + 1] - points[j]);
  }

  cout << ans;
}