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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep2(i, s, n) for (int i = (s); i < (n); ++i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vs vector<string>
#define vc vector<char>
#define vb vector<bool>
#define vp vector<P>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &t : v) {
    is >> t;
  }
  return is;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (T t : v) {
    os << t << " ";
  }
  os << "\\n";
  return os;
}
void Yes(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YES(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int inf = 1001001001;
const ll linf = 1001001001001001001;

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n, m;
  cin >> n >> m;
  vi a(n), b(n), c(n), d(m), e(m), f(m);
  vi row, col;
  row.pb(0);
  col.pb(0);
  rep(i, n) {
    cin >> a[i] >> b[i] >> c[i];
    col.pb(c[i] - 1);
    col.pb(c[i]);
    row.pb(a[i] - 1);
    row.pb(a[i]);
    row.pb(b[i]);
    row.pb(b[i] - 1);
  }
  rep(i, m) {
    cin >> d[i] >> e[i] >> f[i];
    row.pb(d[i]);
    row.pb(d[i] - 1);
    col.pb(e[i]);
    col.pb(e[i] - 1);
    col.pb(f[i]);
    col.pb(f[i] - 1);
  }
  sort(all(row));
  sort(all(col));
  row.erase(unique(all(row)), row.end());
  col.erase(unique(all(col)), col.end());
  int h = row.size();
  int w = col.size();
  vvi ri(h, vi(w, 0));
  vvi lo(h, vi(w, 0));
  rep(i, n) {
    int na = a[i], nb = b[i], nc = c[i];
    na = lower_bound(all(row), na) - row.begin();
    nb = lower_bound(all(row), nb) - row.begin();
    nc = lower_bound(all(col), nc) - col.begin();
    nc--;
    while (na < nb) {
      ri[na][nc] = 1;
      na++;
    }
  }
  rep(i, m) {
    int nd = d[i], ne = e[i], nf = f[i];
    nd = lower_bound(all(row), nd) - row.begin();
    ne = lower_bound(all(col), ne) - col.begin();
    nf = lower_bound(all(col), nf) - col.begin();
    nd--;
    while (ne < nf) {
      lo[nd][ne] = 1;
      ne++;
    }
  }
  vvi able(h, vi(w, 0));
  vvi seen(h, vi(w, 0));
  int sh = lower_bound(all(row), 0) - row.begin();
  int sw = lower_bound(all(col), 0) - col.begin();
  able[sh][sw] = 1;
  queue<P> q;
  q.emplace(sh, sw);
  seen[sh][sw] = 1;
  while (!q.empty()) {
    int nh = q.front().first;
    int nw = q.front().second;
    q.pop();
    if (nh + 1 < h && !lo[nh][nw] && !seen[nh + 1][nw]) {
      able[nh + 1][nw] = 1;
      q.emplace(nh + 1, nw);
      seen[nh + 1][nw] = 1;
    }
    if (nw + 1 < w && !ri[nh][nw] && !seen[nh][nw + 1]) {
      able[nh][nw + 1] = 1;
      q.emplace(nh, nw + 1);
      seen[nh][nw + 1] = 1;
    }
    if (nh > 0 && !lo[nh - 1][nw] && !seen[nh - 1][nw]) {
      able[nh - 1][nw] = 1;
      q.emplace(nh - 1, nw);
      seen[nh - 1][nw] = 1;
    }
    if (nw > 0 && !ri[nh][nw - 1] && !seen[nh][nw - 1]) {
      able[nh][nw - 1] = 1;
      q.emplace(nh, nw - 1);
      seen[nh][nw - 1] = 1;
    }
  }
  ll ans = 0;
  rep(i, h) rep(j, w) {
    if (!able[i][j])
      continue;
    if (i == 0 || i == h - 1 || j == 0 || j == w - 1) {
      cout << "INF" << endl;
      return 0;
    }
    int hig = row[i + 1] - row[i];
    int wid = col[j + 1] - col[j];
    ans += (ll)hig * wid;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep2(i, s, n) for (int i = (s); i < (n); ++i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vs vector<string>
#define vc vector<char>
#define vb vector<bool>
#define vp vector<P>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &t : v) {
    is >> t;
  }
  return is;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (T t : v) {
    os << t << " ";
  }
  os << "\\n";
  return os;
}
void Yes(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YES(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int inf = 1001001001;
const ll linf = 1001001001001001001;

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n, m;
  cin >> n >> m;
  vi a(n), b(n), c(n), d(m), e(m), f(m);
  vi row, col;
  row.pb(0);
  col.pb(0);
  rep(i, n) {
    cin >> a[i] >> b[i] >> c[i];
    col.pb(c[i] - 1);
    col.pb(c[i]);
    row.pb(a[i] - 1);
    row.pb(a[i]);
    row.pb(b[i]);
    row.pb(b[i] - 1);
  }
  rep(i, m) {
    cin >> d[i] >> e[i] >> f[i];
    row.pb(d[i]);
    row.pb(d[i] - 1);
    col.pb(e[i]);
    col.pb(e[i] - 1);
    col.pb(f[i]);
    col.pb(f[i] - 1);
  }
  sort(all(row));
  sort(all(col));
  row.erase(unique(all(row)), row.end());
  col.erase(unique(all(col)), col.end());
  int h = row.size();
  int w = col.size();
  vvi ri(h, vi(w, 0));
  vvi lo(h, vi(w, 0));
  rep(i, n) {
    int na = a[i], nb = b[i], nc = c[i];
    na = lower_bound(all(row), na) - row.begin();
    nb = lower_bound(all(row), nb) - row.begin();
    nc = lower_bound(all(col), nc) - col.begin();
    nc--;
    while (na < nb) {
      ri[na][nc] = 1;
      na++;
    }
  }
  rep(i, m) {
    int nd = d[i], ne = e[i], nf = f[i];
    nd = lower_bound(all(row), nd) - row.begin();
    ne = lower_bound(all(col), ne) - col.begin();
    nf = lower_bound(all(col), nf) - col.begin();
    nd--;
    while (ne < nf) {
      lo[nd][ne] = 1;
      ne++;
    }
  }
  vvi able(h, vi(w, 0));
  vvi seen(h, vi(w, 0));
  int sh = lower_bound(all(row), 0) - row.begin();
  int sw = lower_bound(all(col), 0) - col.begin();
  able[sh][sw] = 1;
  queue<P> q;
  q.emplace(sh, sw);
  while (!q.empty()) {
    int nh = q.front().first;
    int nw = q.front().second;
    q.pop();
    seen[nh][nw] = 1;
    if (nh + 1 < h && !lo[nh][nw] && !seen[nh + 1][nw]) {
      able[nh + 1][nw] = 1;
      q.emplace(nh + 1, nw);
    }
    if (nw + 1 < w && !ri[nh][nw] && !seen[nh][nw + 1]) {
      able[nh][nw + 1] = 1;
      q.emplace(nh, nw + 1);
    }
    if (nh > 0 && !lo[nh - 1][nw] && !seen[nh - 1][nw]) {
      able[nh - 1][nw] = 1;
      q.emplace(nh - 1, nw);
    }
    if (nw > 0 && !ri[nh][nw - 1] && !seen[nh][nw - 1]) {
      able[nh][nw - 1] = 1;
      q.emplace(nh, nw - 1);
    }
  }
  ll ans = 0;
  rep(i, h) rep(j, w) {
    if (!able[i][j])
      continue;
    if (i == 0 || i == h - 1 || j == 0 || j == w - 1) {
      cout << "INF" << endl;
      return 0;
    }
    int hig = row[i + 1] - row[i];
    int wid = col[j + 1] - col[j];
    ans += (ll)hig * wid;
  }
  cout << ans << endl;
}