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

// nani?
#include <bits/stdc++.h>
#define pi acos(-1);
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define f(i, a, b) for (int i = a; i < b; i++)
#define sor(a) sort(a.begin(), a.end())
#define rsor(a) sort(a.rbegin(), a.rend())
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0)
// #define inf 1LL<<62
typedef long long ll;
typedef double ld;
using namespace std;
const ll inf = 1e10 + 6;
const ll MOD = 1e9 + 7;
const ll mod = 998244353;
// ac cmtr;
const int MAX_N = 2 * 1e6 + 5;
const int LG = 25;

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

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

int movx[4] = {-1, 0, 1, 0};
int movy[4] = {0, 1, 0, -1};
ll val[3005][3005];
bool ri[3005][3005];
bool iz[3005][3005];
bool vis[3005][3005];
ll disl[3005], disr[3005];
ll ans = 0;
ll cnt = 1, cnt1 = 1;

int bfs(int a, int b) {
  queue<pair<int, int>> pq;
  // if(!vis[a+1][b+1]) ans+=val[a+1][b+1];
  vis[a][b] = 1;
  pq.push({a, b}); // pq.push({a+1,b+1});
  while (!pq.empty()) {
    pair<int, int> aux = pq.front();
    pq.pop();
    // trace(aux.fs,aux.sc);
    if ((aux.fs >= (cnt - 1)) || (aux.sc >= (cnt1 - 1)) || (aux.fs <= 0) ||
        (aux.sc <= 0)) {
      cout << "INF" << endl;
      exit(0);
    }
    ans += disl[aux.fs] * disr[aux.sc];
    // vis[aux.fs][aux.sc] = 1;
    /*for(int i = 0 ; i < 4;i++){
        int x,y;
        x = aux.fs + movx[i];
        y = aux.sc + movy[i] ;
        //trace(x,y);
        if(x == 3004 || y ==3004 || x == -1 || y == -1) {return 0;}
        if(!vis[x][y]) {pq.push({x,y});}
    }*/
    if (ri[aux.sc][aux.fs] && !vis[aux.fs][aux.sc - 1]) {
      vis[aux.fs][aux.sc - 1] = 1;
      pq.push({aux.fs, aux.sc - 1});
    }
    if (ri[aux.sc + 1][aux.fs] && !vis[aux.fs][aux.sc + 1]) {
      vis[aux.fs][aux.sc + 1] = 1;
      pq.push({aux.fs, aux.sc + 1});
    }
    if (iz[aux.fs][aux.sc] && !vis[aux.fs - 1][aux.sc]) {
      vis[aux.fs - 1][aux.sc] = 1;
      pq.push({aux.fs - 1, aux.sc});
    }
    if (iz[aux.fs + 1][aux.sc] && !vis[aux.fs + 1][aux.sc]) {
      vis[aux.fs + 1][aux.sc] = 1;
      pq.push({aux.fs + 1, aux.sc});
    }
  }
  return 1;
}

int main() {
  fastio;
  int n, m;
  cin >> n >> m;
  ll a, b, c;
  set<ll> sx;
  set<ll> sy;
  vector<pair<pair<ll, ll>, ll>> vx;
  vector<pair<pair<ll, ll>, ll>> vy;
  f(i, 0, n) {
    cin >> a >> b >> c;
    // scanf("%lld%lld%lld",&a,&b,&c);
    sy.insert(c);
    sx.insert(a);
    sx.insert(b);
    vx.pb({{a, b}, c});
  }
  f(i, 0, m) {
    cin >> a >> b >> c;
    // scanf("%d%d%d",&a,&b,&c);
    sx.insert(a);
    sy.insert(b);
    sy.insert(c);
    vy.pb({{b, c}, a});
  }
  sx.insert(0);
  sy.insert(0);
  // for(auto xd : sy) trace(xd);
  cnt = 1;
  for (auto xd : sx) {
    cx[xd] = cnt;
    xc[cnt] = xd;
    cnt++;
  }
  int last;
  ll cont = 0;
  for (auto xd : sx) {
    if (cont == 0) {
      cont++;
      last = xd;
      continue;
    }
    disl[cont] = xd - last;
    last = xd;
    cont++;
  }
  cnt1 = 1;
  for (auto xd : sy) {
    cy[xd] = cnt1;
    yc[cnt1] = xd;
    cnt1++;
  }
  cont = 0;
  for (auto xd : sy) {
    if (cont == 0) {
      cont++;
      last = xd;
      continue;
    }
    disr[cont] = xd - last;
    last = xd;
    cont++;
  }
  f(i, 0, 3005) {
    f(j, 0, 3005) {
      ri[i][j] = 1;
      iz[i][j] = 1;
    }
  }
  /*f(i,0,20){
      f(j,0,20){
        cout<<val[i][j];
      }
      cout<<endl;
  }*/
  // cout<<"------------"<<endl;
  for (auto xd : vx) {
    // ll a = xd.fs.fs,b = xd.fs.sc,c = xd.sc;
    for (int i = cx[xd.fs.fs]; i < cx[xd.fs.sc]; i++) {
      ri[cy[xd.sc]][i] = 0;
    }
  }
  for (auto xd : vy) {
    for (int i = cy[xd.fs.fs]; i < cy[xd.fs.sc]; i++) {
      iz[cx[xd.sc]][i] = 0;
    }
  }
  // vis[cx[0]][cy[0]] = 0;
  /*f(i,0,20){
      f(j,0,20){
        cout<<vis[i][j];
      }
      cout<<endl;
  }*/

  if (bfs(cx[0], cy[0]))
    cout << ans << endl;
  else
    cout << "INF" << endl;
  return 0;
}

Error: -11

Fixed submission:

// I read the news today, oh boy
// About a lucky man who made the grade

#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sz(x) ll((x).size())
#define all(v) (v).begin(), (v).end()
#define trace(x) cerr << #x << " = " << x << endl
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0)
#define For(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
#define roF(i, a, b) for (ll i = ll(a); i >= ll(b); i--)
using namespace std;

typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;

const ll N = 3e3 + 100;
const ll INF = 1e18;

int w, h;
ll area[N][N];
ll ans;

int movx[4] = {0, 1, 0, -1};
int movy[4] = {1, 0, -1, 0};
bool block[N][N][4];
bool vis[N][N];

void dfs(int i, int j) {
  if (i < 0 || i >= h || j < 0 || j >= w) {
    cout << "INF" << endl;
    exit(0);
  }
  if (vis[i][j])
    return;
  vis[i][j] = 1;
  ans += area[i][j];
  For(k, 0, 4) if (!block[i][j][k]) {
    int nxti = i + movy[k];
    int nxtj = j + movx[k];
    dfs(nxti, nxtj);
  }
}

vector<pair<ii, ll>> vert, hor;
vi coordx, coordy;
unordered_map<ll, ll> idx, idy;

int main() {
  fastio;
  int n, m;
  cin >> n >> m;
  For(i, 0, n) {
    ll d, u, x;
    cin >> d >> u >> x;
    vert.pb(mp(mp(d, u), x));
    coordx.pb(x);
    coordy.pb(d);
    coordy.pb(u);
  }
  For(i, 0, m) {
    ll y, l, r;
    cin >> y >> l >> r;
    hor.pb(mp(mp(l, r), y));
    coordy.pb(y);
    coordx.pb(l);
    coordx.pb(r);
  }
  coordx.pb(0);
  coordy.pb(0);

  sort(all(coordx));
  coordx.resize(unique(all(coordx)) - coordx.begin());
  w = sz(coordx);
  For(i, 0, w) idx[coordx[i]] = i;

  sort(all(coordy));
  coordy.resize(unique(all(coordy)) - coordy.begin());
  h = sz(coordy);
  For(i, 0, h) idy[coordy[i]] = i;
  if (w >= N || h >= N) {
    cout << "xd" << endl;
  }
  w--, h--;
  For(i, 0, h) For(j, 0, w) area[i][j] =
      (coordy[i + 1] - coordy[i]) * (coordx[j + 1] - coordx[j]);
  for (auto l : vert) {
    int d = l.ff.ff, u = l.ff.ss, x = l.ss;
    int izq = idx[x] - 1, der = idx[x];
    int i = idy[d];
    int fin = idy[u];
    while (i < fin) {
      if (izq >= 0)
        block[i][izq][1] = 1;
      if (der < w)
        block[i][der][3] = 1;
      i++;
    }
  }
  for (auto elem : hor) {
    int l = elem.ff.ff, r = elem.ff.ss, y = elem.ss;
    int down = idy[y] - 1, up = idy[y];
    int j = idx[l];
    int fin = idx[r];
    while (j < fin) {
      if (down >= 0)
        block[down][j][0] = 1;
      if (up < h)
        block[up][j][2] = 1;
      j++;
    }
  }
  dfs(idy[0], idx[0]);
  cout << ans << endl;
  return 0;
}