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

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

typedef long long ll;

ll N, M;
vector<tuple<ll, ll, ll>> ABC, DEF;
const ll INF = 1e9 + 1;
vector<ll> X, Y;
ll NX, NY;
unordered_map<ll, ll> IX, IY;
vector<vector<bool>> H, V;
queue<pair<ll, ll>> Q;
vector<vector<bool>> A;
ll Res;

int main() {
  cin >> N >> M;
  ABC.resize(N);
  for (ll i = 0; i < N; ++i) {
    ll a, b, c;
    cin >> a >> b >> c;
    ABC.push_back({a, b, c});
    X.push_back(a);
    X.push_back(b);
    Y.push_back(c);
  }
  DEF.resize(M);
  for (ll i = 0; i < M; ++i) {
    ll d, e, f;
    cin >> d >> e >> f;
    DEF.push_back({d, e, f});
    X.push_back(d);
    Y.push_back(e);
    Y.push_back(f);
  }
  X.push_back(0);
  X.push_back(INF);
  X.push_back(-INF);
  Y.push_back(0);
  Y.push_back(INF);
  Y.push_back(-INF);
  sort(X.begin(), X.end());
  X.erase(unique(X.begin(), X.end()), X.end());
  sort(Y.begin(), Y.end());
  Y.erase(unique(Y.begin(), Y.end()), Y.end());
  ll NX = X.size(), NY = Y.size();
  H.resize(NX);
  for (auto &Hx : H)
    Hx.resize(NY);
  V.resize(NX);
  for (auto &Vx : V)
    Vx.resize(NY);
  A.resize(NX);
  for (auto &Ax : A)
    Ax.resize(NY);
  for (ll i = 0; i < NX; ++i)
    IX[X[i]] = i;
  for (ll i = 0; i < NY; ++i)
    IY[Y[i]] = i;
  for (auto abc : ABC) {
    ll a, b, c;
    tie(a, b, c) = abc;
    a = IX[a];
    b = IX[b];
    c = IY[c];
    for (ll x = a; x < b; ++x)
      H[x][c] = true;
  }
  for (auto def : DEF) {
    ll d, e, f;
    tie(d, e, f) = def;
    d = IX[d];
    e = IY[e];
    f = IY[f];
    for (ll y = e; y < f; ++y)
      V[d][y] = true;
  }
  for (ll x = 0; x < NX - 1; ++x)
    H[x][0] = H[x][NY - 1] = true;
  for (ll y = 0; y < NY - 1; ++y)
    V[0][y] = V[NX - 1][y] = true;
  Q.push({IX[0], IY[0]});
  while (!Q.empty()) {
    ll x, y;
    tie(x, y) = Q.front();
    Q.pop();
    if (A[x][y])
      continue;
    A[x][y] = true;
    Res += (X[x + 1] - X[x]) * (Y[y + 1] - Y[y]);
    if (!H[x][y + 1])
      Q.push({x, y + 1});
    if (!H[x][y])
      Q.push({x, y - 1});
    if (!V[x + 1][y])
      Q.push({x + 1, y});
    if (!V[x][y])
      Q.push({x - 1, y});
  }
  if (A[0][0])
    cout << "INF" << endl;
  else
    cout << Res << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

typedef long long ll;

ll N, M;
vector<tuple<ll, ll, ll>> ABC, DEF;
const ll INF = 1e9 + 1;
vector<ll> X, Y;
ll NX, NY;
map<ll, ll> IX, IY;
set<pair<ll, ll>> H, V;
queue<pair<ll, ll>> Q;
set<pair<ll, ll>> A;
ll Res;

int main() {
  cin >> N >> M;
  ABC.resize(N);
  for (ll i = 0; i < N; ++i) {
    ll a, b, c;
    cin >> a >> b >> c;
    ABC.push_back({a, b, c});
    X.push_back(a);
    X.push_back(b);
    Y.push_back(c);
  }
  DEF.resize(M);
  for (ll i = 0; i < M; ++i) {
    ll d, e, f;
    cin >> d >> e >> f;
    DEF.push_back({d, e, f});
    X.push_back(d);
    Y.push_back(e);
    Y.push_back(f);
  }
  X.push_back(0);
  X.push_back(INF);
  X.push_back(-INF);
  Y.push_back(0);
  Y.push_back(INF);
  Y.push_back(-INF);
  sort(X.begin(), X.end());
  X.erase(unique(X.begin(), X.end()), X.end());
  sort(Y.begin(), Y.end());
  Y.erase(unique(Y.begin(), Y.end()), Y.end());
  ll NX = X.size(), NY = Y.size();
  for (ll i = 0; i < NX; ++i)
    IX[X[i]] = i;
  for (ll i = 0; i < NY; ++i)
    IY[Y[i]] = i;
  for (auto abc : ABC) {
    ll a, b, c;
    tie(a, b, c) = abc;
    a = IX[a];
    b = IX[b];
    c = IY[c];
    for (ll x = a; x < b; ++x)
      H.insert({x, c});
  }
  for (auto def : DEF) {
    ll d, e, f;
    tie(d, e, f) = def;
    d = IX[d];
    e = IY[e];
    f = IY[f];
    for (ll y = e; y < f; ++y)
      V.insert({d, y});
  }
  for (ll x = 0; x < NX - 1; ++x) {
    H.insert({x, 0});
    H.insert({x, NY - 1});
  }
  for (ll y = 0; y < NY - 1; ++y) {
    V.insert({0, y});
    V.insert({NX - 1, y});
  }
  Q.push({IX[0], IY[0]});
  while (!Q.empty()) {
    auto xy = Q.front();
    Q.pop();
    if (A.count(xy))
      continue;
    A.insert(xy);
    ll x, y;
    tie(x, y) = xy;
    Res += (X[x + 1] - X[x]) * (Y[y + 1] - Y[y]);
    if (!H.count({x, y + 1}))
      Q.push({x, y + 1});
    if (!H.count({x, y}))
      Q.push({x, y - 1});
    if (!V.count({x + 1, y}))
      Q.push({x + 1, y});
    if (!V.count({x, y}))
      Q.push({x - 1, y});
  }
  if (A.count({0, 0}))
    cout << "INF" << endl;
  else
    cout << Res << endl;
  return 0;
}