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

// cities to vapori-
// corner cases // int vs ll // cin vs scanf // clear structures // statement //
// doublesz
#include <bits/stdc++.h>
#define pb push_back
#define fst first
#define snd second
#define sz(x) int(x.size())
#define fill(x, v) memset(x, v, sizeof(x))
#define REP(i, a, b) for (int i = int(a); i < int(b); ++i)
#define trace(x) cout << #x << " = " << x << endl
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;

int get(vector<int> &all) {
  auto it = upper_bound(all.begin(), all.end(), 0);
  return it - all.begin() - 1;
}

int main() {

  fastio;
  int n, m;
  cin >> n >> m;
  vector<array<int, 3>> ver(n), hor(m);
  vector<int> allx, ally;
  for (int i = 0; i < n; ++i) {
    cin >> ver[i][0] >> ver[i][1] >> ver[i][2];
    allx.pb(ver[i][0]);
    allx.pb(ver[i][1]);
    ally.pb(ver[i][2]);
  }
  for (int i = 0; i < m; ++i) {
    cin >> hor[i][0] >> hor[i][1] >> hor[i][2];
    allx.pb(hor[i][0]);
    ally.pb(hor[i][1]);
    ally.pb(hor[i][2]);
  }
  allx.pb(-2e9);
  allx.pb(2e9);
  ally.pb(-2e9);
  ally.pb(2e9);

  sort(allx.begin(), allx.end());
  allx.erase(unique(allx.begin(), allx.end()), allx.end());
  sort(ally.begin(), ally.end());
  ally.erase(unique(ally.begin(), ally.end()), ally.end());

  /*
  cout << "Allx" << endl;
  for(int i = 0; i < sz(allx); ++i){
          cout << allx[i] << " ";
  }
  cout << endl;
  cout << "Ally" << endl;
  for(int i = 0; i < sz(ally); ++i){
          cout << ally[i] << " ";
  }
  cout << endl;
  */
  map<int, int> tox, toy;
  vector<int> revx(sz(allx)), revy(sz(ally));
  for (int i = 0; i < sz(allx); ++i) {
    tox[allx[i]] = i;
    revx[i] = allx[i];
  }
  for (int i = 0; i < sz(ally); ++i) {
    toy[ally[i]] = i;
    revy[i] = ally[i];
  }

  int len = max(sz(allx), sz(ally)) + 5;
  vector<vector<bool>> up(len, vector<bool>(len, true));
  vector<vector<bool>> dw(len, vector<bool>(len, true));
  vector<vector<bool>> lp(len, vector<bool>(len, true));
  vector<vector<bool>> rp(len, vector<bool>(len, true));

  for (int i = 0; i < n; ++i) {
    int a = tox[ver[i][0]];
    int b = tox[ver[i][1]];
    int c = toy[ver[i][2]];
    for (int ii = a; ii < b; ++ii) {
      lp[ii][c] = false;
      rp[ii][c - 1] = false;
    }
  }

  for (int i = 0; i < m; ++i) {
    int c = tox[hor[i][0]];
    int a = toy[hor[i][1]];
    int b = toy[hor[i][2]];
    for (int ii = a; ii < b; ++ii) {
      dw[c - 1][ii] = false;
      up[c][ii] = false;
    }
  }

  int px = get(allx);
  int py = get(ally);
  vector<vector<bool>> vist(len, vector<bool>(len, false));

  queue<ii> q;
  q.push(ii(px, py));
  vist[px][py] = true;

  auto valid = [&](int x, int y) {
    return x >= 0 && x < len && y >= 0 && y < len && !vist[x][y];
  };

  while (sz(q)) {
    ii par = q.front();
    q.pop();
    int x = par.fst, y = par.snd;
    if (up[x][y]) {
      int nx = x - 1, ny = y;
      if (valid(nx, ny)) {
        vist[nx][ny] = true;
        q.push(ii(nx, ny));
      }
    }
    if (dw[x][y]) {
      int nx = x + 1, ny = y;
      if (valid(nx, ny)) {
        vist[nx][ny] = true;
        q.push(ii(nx, ny));
      }
    }
    if (lp[x][y]) {
      int nx = x, ny = y - 1;
      if (valid(nx, ny)) {
        vist[nx][ny] = true;
        q.push(ii(nx, ny));
      }
    }
    if (rp[x][y]) {
      int nx = x, ny = y + 1;
      if (valid(nx, ny)) {
        vist[nx][ny] = true;
        q.push(ii(nx, ny));
      }
    }
  }

  if (vist[0][0]) {
    cout << "INF" << endl;
    exit(0);
  }

  ll ans = 0;
  for (int i = 0; i < len; ++i) {
    for (int j = 0; j < len; ++j) {
      if (vist[i][j]) {
        ans += 1LL * (allx[i + 1] - allx[i]) * (ally[j + 1] - ally[j]);
      }
    }
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
// v.erase(unique(v.begin(),v.end()),v.end());

#define ff first
#define ss second
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)

typedef long double ld;
typedef long long ll;
const int N = 3050;
const ll mod = 998244353;
const int INF = 1e+9;
using namespace std;
// using namespace __gnu_pbds;

// typedef tree<pair<ll, ll>,null_type,less<pair<ll,
// ll>>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}

template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

map<int, int> l, r;
int d_l[N], d_r[N];
vector<int> la, ra;
vector<pair<int, pair<int, int>>> X, Y;
bool adj_l[N][N], adj_r[N][N], vis[N][N];
int curr1 = 1, curr2 = 1;
ll ans = 0;
bool wea = false;

void llenar() {
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      adj_l[i][j] = true;
      adj_r[i][j] = true;
    }
  }
}

void dfs(int x, int y) {
  vis[x][y] = true;
  if (wea or (x <= 0) or (x >= (curr1 - 1)) or (y <= 0) or (y >= (curr2 - 1))) {
    wea = true;
    return;
  }
  vis[x][y] = true;
  ans += d_l[x] * d_r[y];
  // ahora visitamos llos cuatro lados

  if (adj_r[y][x] and !vis[x][y - 1])
    dfs(x, y - 1);
  if (wea)
    return;
  if (adj_r[y + 1][x] and !vis[x][y + 1])
    dfs(x, y + 1);
  if (wea)
    return;
  if (adj_l[x][y] and !vis[x - 1][y])
    dfs(x - 1, y);
  if (wea)
    return;
  if (adj_l[x + 1][y] and !vis[x + 1][y])
    dfs(x + 1, y);
  if (wea)
    return;
}

void solve() {
  int n, m;
  cin >> n >> m;
  la.push_back(0);
  ra.push_back(0);
  for (int i = 0; i < n; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    X.push_back({c, {a, b}});
    ra.push_back(c);
    la.push_back(a);
    la.push_back(b);
  }
  for (int i = 0; i < m; i++) {
    int d, e, f;
    cin >> d >> e >> f;
    Y.push_back({d, {e, f}});
    ra.push_back(e);
    ra.push_back(f);
    la.push_back(d);
  }

  sort(la.begin(), la.end());
  sort(ra.begin(), ra.end());
  la.erase(unique(la.begin(), la.end()), la.end());
  ra.erase(unique(ra.begin(), ra.end()), ra.end());
  // for(auto x: ra) trace(x);
  if ((la.size() <= 2) or (ra.size() <= 2)) {
    cout << "INF" << endl;
    return;
  }
  for (auto val : la)
    l[val] = curr1++;
  for (int i = 0; i < la.size() - 1; i++) {
    d_l[i + 1] = la[i + 1] - la[i];
  }

  for (auto val : ra)
    r[val] = curr2++;
  for (int i = 0; i < ra.size() - 1; i++) {
    d_r[i + 1] = ra[i + 1] - ra[i];
  }
  // trace(curr1, curr2);
  // for(int i = 0; i < 12; i++) trace(d_r[i]);
  // ya ahora tenemos mapeado todos las weadas

  llenar();

  // ahora recorremos de nuevo la mrd

  for (int i = 0; i < n; i++) {
    int a = X[i].ss.ff, b = X[i].ss.ss, c = X[i].ff;
    for (int j = l[a]; j < l[b]; j++)
      adj_r[r[c]][j] = false;
  }
  for (int i = 0; i < m; i++) {
    int d = Y[i].ff, e = Y[i].ss.ff, f = Y[i].ss.ss;
    for (int j = r[e]; j < r[f]; j++)
      adj_l[l[d]][j] = false;
  }
  /*
  for(int i = 0; i < 12; i++){
      for(int j = 0; j < 12; j++){
          if(adj_r[i][j]) cout << ".";
          else cout << "#";
      }
      cout << endl;
  }*/
  // ya llenamos los vacios de mrd que no se pueden cruzar xd

  // ahora el dfs
  dfs(l[0], r[0]);

  if (wea) {
    cout << "INF" << endl;
    return;
  }
  cout << ans << endl;
}

int main() {
  fastio;
  int tc = 1;
  // cin >> tc;

  while (tc--)
    solve();

  return 0;
}