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

#pragma GCC optimize("Ofast", "unroll-loops")

#include <bits/stdc++.h>

using namespace std;

#define ll long long

constexpr int INF = (1 << 30);
constexpr ll LINF = (1LL << 62);

int N, M;
vector<int> A, B, C, D, E, F;

void input() {
  cin >> N >> M;
  A.resize(N);
  B.resize(N);
  C.resize(N);
  D.resize(M);
  E.resize(M);
  F.resize(M);
  for (int i = 0; i < N; ++i)
    cin >> A[i] >> B[i] >> C[i];
  for (int i = 0; i < M; ++i)
    cin >> D[i] >> E[i] >> F[i];
}

struct node {
  ll S;
  bool l, r, u, d;
  node() {
    S = 0;
    l = r = u = d = true;
  }
};

map<int, int> x, y;
vector<int> _x, _y;

void compress() {
  x[INF];
  y[INF];
  x[-INF];
  y[-INF];
  x[0];
  y[0];
  for (int i = 0; i < N; ++i)
    x[A[i]], x[B[i]], y[C[i]];
  for (int i = 0; i < M; ++i)
    x[D[i]], y[E[i]], y[F[i]];
  int t = 0;
  for (auto &e : x) {
    e.second = t++;
    _x.push_back(e.first);
  }
  t = 0;
  for (auto &e : y) {
    e.second = t++;
    _y.push_back(e.first);
  }
}

int H, W;
node grid[3010][3010];

void make_grid() {
  W = x.size() - 1;
  H = y.size() - 1;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      ll dx = _x[j + 1] - (ll)_x[j];
      ll dy = _y[i + 1] - (ll)_y[i];
      ll S = dx * dy;
      grid[i][j].S = S;
      if (i == 0)
        grid[i][j].u = false;
      if (i == H - 1)
        grid[i][j].d = false;
      if (j == 0)
        grid[i][j].l = false;
      if (j == W - 1)
        grid[i][j].r = false;
    }
  }
  for (int i = 0; i < N; ++i) {
    int y_idx = y[C[i]];
    for (int x_idx = x[A[i]]; x_idx < x[B[i]]; ++x_idx) {
      grid[y_idx][x_idx].u = false;
      grid[y_idx - 1][x_idx].d = false;
    }
  }
  for (int i = 0; i < M; ++i) {
    int x_idx = x[D[i]];
    for (int y_idx = y[E[i]]; y_idx < y[F[i]]; ++y_idx) {
      grid[y_idx][x_idx].l = false;
      grid[y_idx][x_idx - 1].r = false;
    }
  }
}

int visited[3010][3010];
ll res = 0;

void dfs(int y_st, int x_st) {
  stack<pair<int, int>> st;
  visited[y_st][x_st] = 1;
  st.push({y_st, x_st});
  while (!st.empty()) {
    auto p = st.top();
    st.pop();
    int sy = p.first, sx = p.second;
    if (sy == 0 || sy == H - 1 || sx == 0 || sx == W - 1) {
      res = LINF;
      return;
    }
    res += grid[sy][sx].S;
    if (grid[sy][sx].l && !visited[sy][sx - 1])
      visited[sy][sx - 1] = 1, st.push({sy, sx - 1});
    if (grid[sy][sx].r && !visited[sy][sx + 1])
      visited[sy][sx + 1] = 1, st.push({sy, sx + 1});
    if (grid[sy][sx].u && !visited[sy - 1][sx])
      visited[sy - 1][sx] = 1, st.push({sy - 1, sx});
    if (grid[sy][sx].d && !visited[sy + 1][sx])
      visited[sy + 1][sx] = 1, st.push({sy + 1, sx});
  }
}

int main() {
  input();
  compress();
  make_grid();
  dfs(y[0], x[0]);
  if (res < LINF)
    cout << res << endl;
  else
    cout << "INF" << endl;
  return 0;
}

Error: -11

Fixed submission:

#pragma GCC optimize("Ofast", "unroll-loops")

#include <bits/stdc++.h>

using namespace std;

#define ll long long

constexpr int INF = (1 << 30);
constexpr ll LINF = (1LL << 62);

int N, M;
vector<int> A, B, C, D, E, F;

void input() {
  cin >> N >> M;
  A.resize(N);
  B.resize(N);
  C.resize(N);
  D.resize(M);
  E.resize(M);
  F.resize(M);
  for (int i = 0; i < N; ++i)
    cin >> A[i] >> B[i] >> C[i];
  for (int i = 0; i < M; ++i)
    cin >> D[i] >> E[i] >> F[i];
}

struct node {
  ll S;
  bool l, r, u, d;
  node() {
    S = 0;
    l = r = u = d = true;
  }
};

map<int, int> x, y;
vector<int> _x, _y;

void compress() {
  x[INF];
  y[INF];
  x[-INF];
  y[-INF];
  x[0];
  y[0];
  for (int i = 0; i < N; ++i) {
    x[A[i]];
    x[B[i]];
    y[C[i]];
  }
  for (int i = 0; i < M; ++i) {
    x[D[i]];
    y[E[i]];
    y[F[i]];
  }
  int t = 0;
  for (auto &e : x) {
    e.second = t++;
    _x.push_back(e.first);
  }
  t = 0;
  for (auto &e : y) {
    e.second = t++;
    _y.push_back(e.first);
  }
}

int H, W;
node grid[3010][3010];

void make_grid() {
  W = x.size() - 1;
  H = y.size() - 1;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      ll dx = _x[j + 1] - (ll)_x[j];
      ll dy = _y[i + 1] - (ll)_y[i];
      ll S = dx * dy;
      grid[i][j].S = S;
      if (i == 0)
        grid[i][j].u = false;
      if (i == H - 1)
        grid[i][j].d = false;
      if (j == 0)
        grid[i][j].l = false;
      if (j == W - 1)
        grid[i][j].r = false;
    }
  }
  for (int i = 0; i < N; ++i) {
    int y_idx = y[C[i]];
    for (int x_idx = x[A[i]]; x_idx < x[B[i]]; ++x_idx) {
      grid[y_idx][x_idx].u = false;
      grid[y_idx - 1][x_idx].d = false;
    }
  }
  for (int i = 0; i < M; ++i) {
    int x_idx = x[D[i]];
    for (int y_idx = y[E[i]]; y_idx < y[F[i]]; ++y_idx) {
      grid[y_idx][x_idx].l = false;
      grid[y_idx][x_idx - 1].r = false;
    }
  }
}

int visited[3010][3010];
ll res = 0;

void dfs(int sx, int sy) {
  if (visited[sy][sx])
    return;
  visited[sy][sx] = 1;
  if (res >= LINF)
    return;
  if (sx == 0 || sx == W - 1 || sy == 0 || sy == H - 1) {
    res = LINF;
    return;
  }
  res += grid[sy][sx].S;
  if (grid[sy][sx].l && !visited[sy][sx - 1])
    dfs(sx - 1, sy);
  if (grid[sy][sx].r && !visited[sy][sx + 1])
    dfs(sx + 1, sy);
  if (grid[sy][sx].u && !visited[sy - 1][sx])
    dfs(sx, sy - 1);
  if (grid[sy][sx].d && !visited[sy + 1][sx])
    dfs(sx, sy + 1);
}

void solve() {
  int x_idx = x[0], y_idx = y[0];
  dfs(x_idx - 1, y_idx - 1);
  /*dfs(x_idx, y_idx - 1);
  dfs(x_idx - 1, y_idx);
  dfs(x_idx, y_idx);*/
}

int main() {
  input();
  compress();
  make_grid();
  solve();
  if (res < LINF)
    cout << res << endl;
  else
    cout << "INF" << endl;
  return 0;
}