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

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1145141919810893364
// #define INF 810114514
#define PI 3.141592653589
typedef long long ll;
typedef pair<ll, ll> PP;
#define int ll
#define setdouble setprecision
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define GOODBYE                                                                \\
  do {                                                                         \\
    cout << "0" << endl;                                                       \\
    return 0;                                                                  \\
  } while (false)
#define MM << " " <<
#define Endl endl
#define debug false
#define debug2 false

class compress {
private:
  std::vector<int> E;
  std::map<int, int> G;

public:
  int init(std::vector<long long> A) {
    E.clear();
    sort(A.begin(), A.end());
    for (int i = 0; i < A.size(); i++) {
      if (i == 0 || A[i] != A[i - 1]) {
        E.push_back(A[i]);
        G[A[i]] = (int)E.size();
      }
    }
    return (int)E.size();
  }

  int size() { return (int)E.size(); }

  int coordinate(int x) {
    if (0 <= x && x < (int)E.size()) {
      return E[x];
    } else {
      return 0;
    }
  }

  int index(int X) { return G[X] - 1; }
};

signed main(void) {
  int N, M;
  vector<int> A, B, C, D, E, F;
  vector<int> x, y;
  int xz = -INF, yz = -INF;
  cin >> N >> M;
  REP(i, N) {
    int a;
    cin >> a;
    A.push_back(a);
    x.push_back(a);
    cin >> a;
    B.push_back(a);
    x.push_back(a);
    cin >> a;
    C.push_back(a);
    y.push_back(a);
  }
  REP(i, M) {
    int a;
    cin >> a;
    D.push_back(a);
    x.push_back(a);
    cin >> a;
    E.push_back(a);
    y.push_back(a);
    cin >> a;
    F.push_back(a);
    y.push_back(a);
  }

  REP(i, x.size()) {
    if (x[i] <= 0) {
      xz = max(xz, x[i]);
    }
  }
  REP(i, y.size()) {
    if (y[i] <= 0) {
      yz = max(yz, y[i]);
    }
  }

  x.push_back(-INF);
  x.push_back(INF);
  y.push_back(-INF);
  y.push_back(INF);

  compress X, Y;
  X.init(x);
  Y.init(y);

  const int MEM = 3020;
  bool wallx[MEM][MEM] = {}, wally[MEM][MEM] = {};
  REP(i, N) {
    int a = X.index(A[i]);
    int b = X.index(B[i]);
    int c = Y.index(C[i]);
    // cout << "x! " << a << " " << b << " " << c << endl;
    for (int i = a; i < b; i++) {
      wally[c][i] = true;
    }
  }
  REP(i, M) {
    int d = X.index(D[i]);
    int e = Y.index(E[i]);
    int f = Y.index(F[i]);
    for (int i = e; i < f; i++) {
      wallx[i][d] = true;
    }
  }

  int Ans = 0;
  queue<pair<int, int>> que;
  bool Map[MEM][MEM];
  int xs = X.index(xz), ys = Y.index(yz);
  que.push({xs, ys});
  Map[xs][ys] = true;
  bool infinity = false;

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

  while (!que.empty()) {
    int s = que.front().first;
    int t = que.front().second;
    que.pop();
    // cout << X.coordinate(s) << " " << Y.coordinate(t) << endl;
    int Xe = (X.coordinate(s + 1) - X.coordinate(s));
    int Ye = (Y.coordinate(t + 1) - Y.coordinate(t));
    if (X.coordinate(s + 1) == INF || X.coordinate(s) == -INF) {
      infinity = true;
    }
    if (Y.coordinate(t + 1) == INF || Y.coordinate(t) == -INF) {
      infinity = true;
    }
    Ans += Xe * Ye;
    REP(k, 4) {

      if (s + dx[k] < 0 || X.size() <= s + dx[k]) {
        continue;
      }
      if (t + dy[k] < 0 || Y.size() <= t + dy[k]) {
        continue;
      }
      if (k == 0 && wallx[t][s + 1]) {
        continue;
      }
      if (k == 1 && wally[t + 1][s]) {
        continue;
      }
      if (k == 2 && wallx[t][s]) {
        continue;
      }
      if (k == 3 && wally[t][s]) {
        continue;
      }

      if (!Map[s + dx[k]][t + dy[k]]) {
        Map[s + dx[k]][t + dy[k]] = true;
        que.push({s + dx[k], t + dy[k]});
      }
    }
  }
  if (infinity) {
    cout << "INF" << endl;
  } else {
    cout << Ans << endl;
  }

#if debug
  cout << "  ";
  REP(j, 10) {
    if (X.coordinate(j) >= 0)
      cout << "+";
    if (X.coordinate(j) == -INF) {
      cout << "-I ";
    } else {
      cout << X.coordinate(j) << " ";
    }
  }
  cout << endl;
  REP(i, 10) {
    if (Y.coordinate(i) >= 0)
      cout << "+";
    if (Y.coordinate(i) == -INF) {
      cout << "-I ";
    } else {
      cout << Y.coordinate(i) << " ";
    }
    REP(j, 10) { cout << wallx[i][j] << "  "; }
    cout << endl;
  }
  cout << endl;
#endif

#if debug
  cout << "  ";
  REP(j, 10) {
    if (X.coordinate(j) >= 0)
      cout << "+";
    if (X.coordinate(j) == -INF) {
      cout << "-I ";
    } else {
      cout << X.coordinate(j) << " ";
    }
  }
  cout << endl;
  REP(i, 10) {
    if (Y.coordinate(i) >= 0)
      cout << "+";
    if (Y.coordinate(i) == -INF) {
      cout << "-I ";
    } else {
      cout << Y.coordinate(i) << " ";
    }
    REP(j, 10) { cout << wally[i][j] << "  "; }
    cout << endl;
  }
  cout << endl;
#endif

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1145141919810893364
// #define INF 810114514
#define PI 3.141592653589
typedef long long ll;
typedef pair<ll, ll> PP;
#define int ll
#define setdouble setprecision
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define GOODBYE                                                                \\
  do {                                                                         \\
    cout << "0" << endl;                                                       \\
    return 0;                                                                  \\
  } while (false)
#define MM << " " <<
#define Endl endl
#define debug false
#define debug2 false

class compress {
private:
  std::vector<int> E;
  std::map<int, int> G;

public:
  int init(std::vector<long long> A) {
    E.clear();
    sort(A.begin(), A.end());
    for (int i = 0; i < A.size(); i++) {
      if (i == 0 || A[i] != A[i - 1]) {
        E.push_back(A[i]);
        G[A[i]] = (int)E.size();
      }
    }
    return (int)E.size();
  }

  int size() { return (int)E.size(); }

  int coordinate(int x) {
    if (0 <= x && x < (int)E.size()) {
      return E[x];
    } else {
      return 0;
    }
  }

  int index(int X) { return G[X] - 1; }
};

signed main(void) {
  int N, M;
  vector<int> A, B, C, D, E, F;
  vector<int> x, y;
  int xz = -INF, yz = -INF;
  cin >> N >> M;
  REP(i, N) {
    int a;
    cin >> a;
    A.push_back(a);
    x.push_back(a);
    cin >> a;
    B.push_back(a);
    x.push_back(a);
    cin >> a;
    C.push_back(a);
    y.push_back(a);
  }
  REP(i, M) {
    int a;
    cin >> a;
    D.push_back(a);
    x.push_back(a);
    ;
    cin >> a;
    E.push_back(a);
    y.push_back(a);
    cin >> a;
    F.push_back(a);
    y.push_back(a);
  }

  REP(i, x.size()) {
    if (x[i] <= 0) {
      xz = max(xz, x[i]);
    }
  }
  REP(i, y.size()) {
    if (y[i] <= 0) {
      yz = max(yz, y[i]);
    }
  }

  x.push_back(-INF);
  x.push_back(INF);
  y.push_back(-INF);
  y.push_back(INF);

  compress X, Y;
  X.init(x);
  Y.init(y);

  const int MEM = 3020;
  bool wallx[MEM][MEM] = {}, wally[MEM][MEM] = {};
  REP(i, N) {
    int a = X.index(A[i]);
    int b = X.index(B[i]);
    int c = Y.index(C[i]);
    // cout << "x! " << a << " " << b << " " << c << endl;
    for (int i = a; i < b; i++) {
      wally[c][i] = true;
    }
  }
  REP(i, M) {
    int d = X.index(D[i]);
    int e = Y.index(E[i]);
    int f = Y.index(F[i]);
    for (int i = e; i < f; i++) {
      wallx[i][d] = true;
    }
  }

  int Ans = 0;
  queue<pair<int, int>> que;
  map<pair<int, int>, bool> Map;
  int xs = X.index(xz), ys = Y.index(yz);
  que.push({xs, ys});
  Map[{xs, ys}] = true;
  bool infinity = false;

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

  while (!que.empty()) {
    int s = que.front().first;
    int t = que.front().second;
    que.pop();
    // cout << X.coordinate(s) << " " << Y.coordinate(t) << endl;
    int Xe = (X.coordinate(s + 1) - X.coordinate(s));
    int Ye = (Y.coordinate(t + 1) - Y.coordinate(t));
    if (X.coordinate(s + 1) == INF || X.coordinate(s) == -INF) {
      infinity = true;
    }
    if (Y.coordinate(t + 1) == INF || Y.coordinate(t) == -INF) {
      infinity = true;
    }
    Ans += Xe * Ye;
    REP(k, 4) {

      if (s + dx[k] < 0 || X.size() <= s + dx[k]) {
        continue;
      }
      if (t + dy[k] < 0 || Y.size() <= t + dy[k]) {
        continue;
      }
      if (k == 0 && wallx[t][s + 1]) {
        continue;
      }
      if (k == 1 && wally[t + 1][s]) {
        continue;
      }
      if (k == 2 && wallx[t][s]) {
        continue;
      }
      if (k == 3 && wally[t][s]) {
        continue;
      }

      if (!Map[{s + dx[k], t + dy[k]}]) {
        Map[{s + dx[k], t + dy[k]}] = true;
        que.push({s + dx[k], t + dy[k]});
      }
    }
  }
  if (infinity) {
    cout << "INF" << endl;
  } else {
    cout << Ans << endl;
  }

#if debug
  cout << "  ";
  REP(j, 10) {
    if (X.coordinate(j) >= 0)
      cout << "+";
    if (X.coordinate(j) == -INF) {
      cout << "-I ";
    } else {
      cout << X.coordinate(j) << " ";
    }
  }
  cout << endl;
  REP(i, 10) {
    if (Y.coordinate(i) >= 0)
      cout << "+";
    if (Y.coordinate(i) == -INF) {
      cout << "-I ";
    } else {
      cout << Y.coordinate(i) << " ";
    }
    REP(j, 10) { cout << wallx[i][j] << "  "; }
    cout << endl;
  }
  cout << endl;
#endif

#if debug
  cout << "  ";
  REP(j, 10) {
    if (X.coordinate(j) >= 0)
      cout << "+";
    if (X.coordinate(j) == -INF) {
      cout << "-I ";
    } else {
      cout << X.coordinate(j) << " ";
    }
  }
  cout << endl;
  REP(i, 10) {
    if (Y.coordinate(i) >= 0)
      cout << "+";
    if (Y.coordinate(i) == -INF) {
      cout << "-I ";
    } else {
      cout << Y.coordinate(i) << " ";
    }
    REP(j, 10) { cout << wally[i][j] << "  "; }
    cout << endl;
  }
  cout << endl;
#endif

  return 0;
}