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

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

#define int long long
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> struct Zip {
  vector<T> val;
  unordered_map<T, int> idx;

  void add(const T v) { val.emplace_back(v); }

  void zip() {
    sort(val.begin(), val.end());
    val.erase(unique(val.begin(), val.end()), val.end());
    for (int i = 0; i < val.size(); i++)
      idx[val[i]] = i;
  }

  int operator[](const T v) { return idx[v]; }
  T operator()(const int i) { return val[i]; }
  int size() { return val.size(); }
};

struct Box {
  bool left;
  bool right;
  bool top;
  bool down;
  bool v;

  Box() {
    left = right = top = down = true;
    v = false;
  }
};

Box mas[4000][4000];

struct Num {
  int a, b, c, d, e, f;
};

Zip<int> tate, yoko;
int dfs(int i, int j) {
  queue<pair<int, int>> que;
  que.push({i, j});
  int ans = 0;
  while (!que.empty()) {
    i = que.front().first;
    j = que.front().second;
    que.pop();
    if (mas[i][j].v)
      continue;
    if (i == 0 || i == tate.size() - 1 || j == 0 || j == yoko.size() - 1) {
      cout << "INF" << endl;
      exit(0);
    }
    mas[i][j].v = true;
    ans += (tate(i) - tate(i - 1)) * (yoko(j) - yoko(j - 1));
    if (mas[i][j].left)
      que.push({i, j - 1});
    if (mas[i][j].right)
      que.push({i, j + 1});
    if (mas[i][j].top)
      que.push({i + 1, j});
    if (mas[i][j].down)
      que.push({i - 1, j});
  }
  return ans;
}

signed main() {
  int n, m;
  cin >> n >> m;

  Num t[1010], y[1010];

  for (int i = 0; i < n; i++) {
    cin >> t[i].a >> t[i].b >> t[i].c;
    tate.add(t[i].a);
    tate.add(t[i].b);
    yoko.add(t[i].c);
  }

  for (int i = 0; i < m; i++) {
    cin >> y[i].d >> y[i].e >> y[i].f;
    tate.add(y[i].d);
    yoko.add(y[i].e);
    yoko.add(y[i].f);
  }

  tate.add(INT_MIN);
  yoko.add(INT_MIN);
  tate.add(INT_MAX);
  yoko.add(INT_MAX);

  tate.add(0);
  yoko.add(0);
  tate.zip();
  yoko.zip();

  for (int i = 0; i < n; i++) {
    int idx = yoko[t[i].c];
    int l = tate[t[i].a];
    int r = tate[t[i].b];
    for (int j = l + 1; j <= r; j++) {
      mas[j][idx].right = false;
      mas[j][idx + 1].left = false;
    }
  }

  for (int i = 0; i < m; i++) {
    int idx = tate[y[i].d];
    int l = yoko[y[i].e];
    int r = yoko[y[i].f];
    for (int j = l + 1; j <= r; j++) {
      mas[idx][j].top = false;
      mas[idx + 1][j].down = false;
    }
  }

  int ans = dfs(tate[0], yoko[0]);

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

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

#define int long long
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> struct Zip {
  vector<T> val;
  unordered_map<T, int> idx;

  void add(const T v) { val.emplace_back(v); }

  void zip() {
    sort(val.begin(), val.end());
    val.erase(unique(val.begin(), val.end()), val.end());
    for (int i = 0; i < val.size(); i++)
      idx[val[i]] = i;
  }

  int operator[](const T v) { return idx[v]; }
  T operator()(const int i) { return val[i]; }
  int size() { return val.size(); }
};

struct Box {
  bool left;
  bool right;
  bool top;
  bool down;
  bool v;

  Box() {
    left = right = top = down = true;
    v = false;
  }
};

Box mas[4000][4000];

struct Num {
  int a, b, c, d, e, f;
};

Zip<int> tate, yoko;
int dfs(int i, int j) {
  if (mas[i][j].v)
    return 0;
  if (i == 0 || i == tate.size() - 1 || j == 0 || j == yoko.size() - 1) {
    cout << "INF" << endl;
    exit(0);
  }
  mas[i][j].v = true;
  int ans = (tate(i) - tate(i - 1)) * (yoko(j) - yoko(j - 1));
  if (mas[i][j].left)
    ans += dfs(i, j - 1);
  if (mas[i][j].right)
    ans += dfs(i, j + 1);
  if (mas[i][j].top)
    ans += dfs(i + 1, j);
  if (mas[i][j].down)
    ans += dfs(i - 1, j);
  return ans;
}

signed main() {
  int n, m;
  cin >> n >> m;

  Num t[1010], y[1010];

  for (int i = 0; i < n; i++) {
    cin >> t[i].a >> t[i].b >> t[i].c;
    tate.add(t[i].a);
    tate.add(t[i].b);
    yoko.add(t[i].c);
  }

  for (int i = 0; i < m; i++) {
    cin >> y[i].d >> y[i].e >> y[i].f;
    tate.add(y[i].d);
    yoko.add(y[i].e);
    yoko.add(y[i].f);
  }

  tate.add(INT_MIN);
  yoko.add(INT_MIN);
  tate.add(INT_MAX);
  yoko.add(INT_MAX);

  tate.add(0);
  yoko.add(0);
  tate.zip();
  yoko.zip();

  for (int i = 0; i < n; i++) {
    int idx = yoko[t[i].c];
    int l = tate[t[i].a];
    int r = tate[t[i].b];
    for (int j = l + 1; j <= r; j++) {
      mas[j][idx].right = false;
      mas[j][idx + 1].left = false;
    }
  }

  for (int i = 0; i < m; i++) {
    int idx = tate[y[i].d];
    int l = yoko[y[i].e];
    int r = yoko[y[i].f];
    for (int j = l + 1; j <= r; j++) {
      mas[idx][j].top = false;
      mas[idx + 1][j].down = false;
    }
  }

  int ans = dfs(tate[0], yoko[0]);

  cout << ans << endl;

  return 0;
}