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

/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define printa(x, n)                                                           \\
  for (ll i = 0; i < n; i++) {                                                 \\
    cout << (x[i]) << " \\n"[i == n - 1];                                       \\
  };
void print() {}
template <class H, class... T> void print(H &&h, T &&...t) {
  cout << h << " \\n"[sizeof...(t) == 0];
  print(forward<T>(t)...);
}
bool check[3200][3200] = {};
bool line_yoko[3010][3010] = {};
bool line_tate[3010][3010] = {};

void solve() {
  ll N, M;
  cin >> N >> M;
  ll A[1010], B[1010], C[1010];
  ll D[1010], E[1010], F[1010];

  // 座標圧縮
  vector<ll> x, y;
  x.push_back(-INF);
  x.push_back(INF);
  y.push_back(-INF);
  y.push_back(INF);
  x.push_back(0);
  y.push_back(0);
  rep(i, 0, N) {
    cin >> A[i] >> B[i] >> C[i];
    y.push_back(C[i]);
    x.push_back(A[i]);
    x.push_back(B[i]);
  }
  rep(i, 0, M) {
    cin >> D[i] >> E[i] >> F[i];
    x.push_back(D[i]);
    y.push_back(E[i]);
    y.push_back(F[i]);
  }
  sort(x.begin(), x.end());
  sort(y.begin(), y.end());
  x.erase(unique(x.begin(), x.end()), x.end());
  y.erase(unique(y.begin(), y.end()), y.end());
  vector<ll> diff_x, diff_y;
  ll sz_x = x.size();
  rep(i, 0, sz_x - 1) { diff_x.push_back(x[i + 1] - x[i]); }
  ll sz_y = y.size();
  rep(i, 0, sz_y - 1) { diff_y.push_back(y[i + 1] - y[i]); }
  rep(i, 0, N) {
    A[i] = lower_bound(x.begin(), x.end(), A[i]) - x.begin();
    B[i] = lower_bound(x.begin(), x.end(), B[i]) - x.begin();
    C[i] = lower_bound(y.begin(), y.end(), C[i]) - y.begin();
  }
  rep(i, 0, M) {
    D[i] = lower_bound(x.begin(), x.end(), D[i]) - x.begin();
    E[i] = lower_bound(y.begin(), y.end(), E[i]) - y.begin();
    F[i] = lower_bound(y.begin(), y.end(), F[i]) - y.begin();
  }

  // printa(diff_x, sz_x-1);
  // printa(diff_y, sz_y-1);

  rep(i, 0, N) {
    rep(j, A[i], B[i]) { line_tate[j][C[i]] = true; }
  }

  rep(i, 0, M) {
    rep(j, E[i], F[i]) { line_yoko[D[i]][j] = true; }
  }

  queue<lpair> que;
  ll x0 = lower_bound(x.begin(), x.end(), 0) - x.begin();
  ll y0 = lower_bound(y.begin(), y.end(), 0) - y.begin();
  que.push({x0, y0});
  bool ok = true;

  while (not que.empty()) {
    lpair l1 = que.front();
    que.pop();
    ll x = l1.first, y = l1.second;
    if (x > 3100 || y > 3100) {
      ok = false;
      break;
    }

    if (x < 0 || y < 0)
      continue;
    if (check[x][y])
      continue;
    check[x][y] = true;
    if (not line_yoko[x + 1][y]) {
      if (not check[x + 1][y])
        que.push({x + 1, y});
    }
    if (not line_yoko[x][y]) {
      if (not check[x - 1][y])
        que.push({x - 1, y});
    }
    if (not line_tate[x][y + 1]) {
      if (not check[x][y + 1])
        que.push({x, y + 1});
    }
    if (not line_tate[x][y]) {
      if (not check[x][y - 1])
        que.push({x, y - 1});
    }
  }
  if (not ok) {
    print("INF");
    return;
  }
  ll ans = 0;
  rep(i, 0, 3010) {
    rep(j, 0, 3010) {
      if (check[i][j]) {
        ans += diff_x[i] * diff_y[j];
      }
    }
  }
  print(ans);
  // rep(i,0,10){
  //     rep(j,0,10){
  //         cout << check[i][j];
  //     }
  //     cout << endl;
  // }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
}

Error: TLE

Fixed submission:

/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define printa(x, n)                                                           \\
  for (ll i = 0; i < n; i++) {                                                 \\
    cout << (x[i]) << " \\n"[i == n - 1];                                       \\
  };
void print() {}
template <class H, class... T> void print(H &&h, T &&...t) {
  cout << h << " \\n"[sizeof...(t) == 0];
  print(forward<T>(t)...);
}
ll get(ll x, ll y) { return x * 2000 + y; }
bool check[2010][2010] = {};

void solve() {
  ll N, M;
  cin >> N >> M;
  ll thre = 100;
  ll A[1010], B[1010], C[1010];
  ll D[1010], E[1010], F[1010];
  map<lpair, bool> is_line;

  rep(i, 0, N) {
    cin >> A[i] >> B[i] >> C[i];
    A[i] += thre;
    B[i] += thre;
    C[i] += thre;
    rep(j, A[i], B[i]) {
      ll v1 = get(j, C[i]), v2 = get(j + 1, C[i]);
      is_line[{v1, v2}] = true;
      is_line[{v2, v1}] = true;
    }
  }

  rep(i, 0, M) {
    cin >> D[i] >> E[i] >> F[i];
    D[i] += thre;
    E[i] += thre;
    F[i] += thre;
    rep(j, E[i], F[i]) {
      ll v1 = get(D[i], j), v2 = get(D[i], j + 1);
      is_line[{v1, v2}] = true;
      is_line[{v2, v1}] = true;
    }
  }
  queue<lpair> que;
  que.push({thre, thre});
  ll dx[4] = {0, 0, 1, -1};
  ll dy[4] = {1, -1, 0, 0};

  while (not que.empty()) {
    lpair l1 = que.front();
    que.pop();
    ll x = l1.first, y = l1.second;
    // print(x,y);
    if (x < 0 || y < 0 || x > 3 * thre || y > 3 * thre)
      continue;
    if (check[x][y])
      continue;
    check[x][y] = true;
    if (not is_line[{get(x + 1, y), get(x + 1, y + 1)}]) {
      if (not check[x + 1][y])
        que.push({x + 1, y});
    }
    if (not is_line[{get(x, y), get(x, y + 1)}]) {
      if (not check[x - 1][y])
        que.push({x - 1, y});
    }
    if (not is_line[{get(x + 1, y + 1), get(x, y + 1)}]) {
      if (not check[x][y + 1])
        que.push({x, y + 1});
    }
    if (not is_line[{get(x + 1, y), get(x, y)}]) {
      if (not check[x][y - 1])
        que.push({x, y - 1});
    }
  }
  ll ans = 0;
  rep(i, 0, thre * 2 + 1) {
    rep(j, 0, thre * 2 + 1) {
      if (check[i][j])
        ans++;
    }
  }
  if (check[3 * thre][3 * thre]) {
    print("INF");
  } else {
    print(ans);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
}