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

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

#define pb push_back

#define jael(args...)                                                          \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cout << *it << " : " << a << endl;
  err(++it, args...);
}

const int N = 4010, inf = 1e9 + 7;
int a[N], b[N], c[N], d[N], e[N], f[N];
int uu[N][N], dd[N][N], ll[N][N], rr[N][N];
bool vis[N][N];

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

int main() {

  std::ios::sync_with_stdio(0);
  std::cin.tie(0);

  int n, m;
  cin >> n >> m;
  vector<int> xs, ys;
  xs.pb(inf), xs.pb(inf * -1);
  ys.pb(inf), ys.pb(inf * -1);

  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    xs.pb(a[i]), xs.pb(b[i]);
    ys.pb(c[i]);
  }
  for (int i = 0; i < m; i++) {
    cin >> d[i] >> e[i] >> f[i];
    xs.pb(d[i]);
    ys.pb(e[i]), ys.pb(f[i]);
  }

  // la magie opère ici
  sort(xs.begin(), xs.end()), sort(ys.begin(), ys.end());
  xs.resize(unique(xs.begin(), xs.end()) - xs.begin());
  ys.resize(unique(ys.begin(), ys.end()) - ys.begin());

  for (int i = 0; i < n;
       i++) { // ici on remplit les contraintes de mouvements de haut en bas
    int x1 = lower_bound(xs.begin(), xs.end(), a[i]) - xs.begin();
    int x2 = lower_bound(xs.begin(), xs.end(), b[i]) - xs.begin();
    int y = lower_bound(ys.begin(), ys.end(), c[i]) - ys.begin();
    for (int j = x1; j < x2; j++) {
      uu[j][y - 1] = 1;
      dd[j][y] = 1;
    }
  }

  for (int i = 0; i < m;
       i++) { // ici on remplit les contraintes de mouvements gauche et droite
    int x = lower_bound(xs.begin(), xs.end(), d[i]) - xs.begin();
    int y1 = lower_bound(ys.begin(), ys.end(), e[i]) - ys.begin();
    int y2 = lower_bound(ys.begin(), ys.end(), f[i]) - ys.begin();
    for (int j = y1; j < y2; j++) {
      rr[x - 1][j] = 1;
      ll[x][j] = 1;
    }
  }
  ////////ici se termine la magie /////////
  ////////////////////////////////////////

  int x = lower_bound(xs.begin(), xs.end(), 0) - xs.begin();
  int y = lower_bound(ys.begin(), ys.end(), 0) - ys.begin();
  x--, y--;

  // trace(x,y);
  ///////////////// largeur première recherche //////////////////////

  queue<pair<int, int>> q;
  q.push({x, y});
  vis[x][y] = 1;
  // trace(q.size())	;
  while (!q.empty()) {
    int xf = q.front().first;
    int yf = q.front().second;
    // trace(xf,yf,vis[xf][yf]);
    q.pop();
    for (int i = 0; i < 4; i++) {
      if (i == 0 and uu[xf][yf])
        continue;
      if (i == 1 and dd[xf][yf])
        continue;
      if (i == 2 and rr[xf][yf])
        continue;
      if (i == 3 and ll[xf][yf])
        continue;
      int nx = xf + dx[i], ny = yf + dy[i];
      if (nx < 0 or nx >= xs.size() - 1 or ny < 0 or ny >= ys.size() - 1)
        continue;
      if (vis[nx][ny])
        continue;
      vis[nx][ny] = true;
      q.push({nx, ny});
    }
  }

  ////////////////////////////////////////////////////////////////

  if (vis[0][0] or vis[xs.size() - 1][ys.size() - 1]) {
    cout << "INF" << endl;
    return 0;
  }

  long long wyn = 0;
  for (int i = 0; i < xs.size() - 1; i++) {
    for (int j = 0; j < ys.size() - 1; j++) {
      if (vis[i][j]) {
        //	trace(i,j);
        wyn += (long long)(xs[i + 1] - xs[i]) * (ys[j + 1] - ys[j]);
      }
    }
  }
  cout << wyn << 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 = 3550;
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;
}