Problem p02680 (C++) — buggy submission, verdict: Runtime Error

#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:
  void 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();
      }
    }
  }

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

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

  int index(int X) {
    return (upper_bound(E.begin(), E.end(), X)) - E.begin() - 1;
  }
};

signed main(void) {
  int N, M;
  vector<int> A, B, C, D, E, F;
  vector<int> x, y;
  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);
  }

  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(0), ys = Y.index(0);
  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.value(s) << " " << Y.value(t) << endl;
    int Xe = (X.value(s + 1) - X.value(s));
    int Ye = (Y.value(t + 1) - Y.value(t));
    if (X.value(s + 1) == INF || X.value(s) == -INF) {
      infinity = true;
    }
    if (Y.value(t + 1) == INF || Y.value(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.value(j) >= 0)
      cout << "+";
    if (X.value(j) == -INF) {
      cout << "-I ";
    } else {
      cout << X.value(j) << " ";
    }
  }
  cout << endl;
  REP(i, 10) {
    if (Y.value(i) >= 0)
      cout << "+";
    if (Y.value(i) == -INF) {
      cout << "-I ";
    } else {
      cout << Y.value(i) << " ";
    }
    REP(j, 10) { cout << wallx[i][j] << "  "; }
    cout << endl;
  }
  cout << endl;
#endif

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

  return 0;
}


Error: 0

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:
  void 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();
      }
    }
  }

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

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

  int index(int X) {
    return (upper_bound(E.begin(), E.end(), X)) - E.begin() - 1;
  }
};

signed main(void) {
  int N, M;
  vector<int> A, B, C, D, E, F;
  vector<int> x, y;
  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);
  }

  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 = 320;
  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(0), ys = Y.index(0);
  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.value(s) << " " << Y.value(t) << endl;
    int Xe = (X.value(s + 1) - X.value(s));
    int Ye = (Y.value(t + 1) - Y.value(t));
    if (X.value(s + 1) == INF || X.value(s) == -INF) {
      infinity = true;
    }
    if (Y.value(t + 1) == INF || Y.value(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.value(j) >= 0)
      cout << "+";
    if (X.value(j) == -INF) {
      cout << "-I ";
    } else {
      cout << X.value(j) << " ";
    }
  }
  cout << endl;
  REP(i, 10) {
    if (Y.value(i) >= 0)
      cout << "+";
    if (Y.value(i) == -INF) {
      cout << "-I ";
    } else {
      cout << Y.value(i) << " ";
    }
    REP(j, 10) { cout << wallx[i][j] << "  "; }
    cout << endl;
  }
  cout << endl;
#endif

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

  return 0;
}