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: 0

Fixed submission:

// 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 vis[3005][3005];
ll ans = 0;

void bfs(int a, int b) {
  // trace(a,b);
  queue<pair<int, int>> pq;
  if (!vis[a + 1][b + 1])
    ans += val[a + 1][b + 1];
  pq.push({a, b});
  pq.push({a + 1, b + 1});
  // pq.push({a+1,b});pq.push({a+1,b+1});
  vis[a][b] = 1;
  vis[a][b + 1] = 1;
  vis[a + 1][b] = 1;
  vis[a + 1][b + 1] = 1;
  // trace(a,b);
  while (!pq.empty()) {
    pair<int, int> aux = pq.front();
    pq.pop();
    if (!vis[aux.fs][aux.sc])
      ans += val[aux.fs][aux.sc];
    vis[aux.fs][aux.sc] = 1;
    // trace(aux.fs,aux.sc,val[aux.fs][aux.sc]);
    // ans += (yc[aux.sc+2] - yc[aux.sc])*(xc[aux.fs+2] - xc[aux.fs]);
    // ans += val[aux.fs][aux.sc];
    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) {
        cout << "INF" << endl;
        exit(0);
      }
      if (!vis[x][y]) {
        pq.push({x, y});
      }
    }
  }
}

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);
  int cnt = 0;
  for (auto xd : sx) {
    cx[xd] = 2 * cnt;
    xc[2 * cnt] = xd;
    cnt++;
  }
  cnt = 0;
  for (auto xd : sy) {
    cy[xd] = 2 * cnt;
    yc[2 * cnt] = xd;
    cnt++;
  }
  f(i, 0, 3004) {
    f(j, 0, 3004) {
      if ((j & 1) && (i & 1)) {
        val[i][j] = (xc[j + 1] - xc[j - 1]) * (yc[i + 1] - yc[i - 1]);
      }
    }
  }

  // trace(val[5][5]);trace(val[5][7]);trace(val[5][9]);trace(val[5][11]);
  // trace(val[7][5]);trace(val[7][7]);trace(val[7][11]);
  // trace(val[9][5]);trace(val[9][11]);
  // trace(val[11][5]);trace(val[11][7]);trace(val[11][9]);trace(val[11][11]);
  /*f(i,0,20){
  f(j,0,20){
    cout<<val[i][j];
  }
  cout<<endl;
}*/
  // cout<<"------------"<<endl;
  for (auto xd : vx) {
    for (int i = cx[xd.fs.fs]; i <= cx[xd.fs.sc]; i++) {
      vis[i][cy[xd.sc]] = 1;
    }
  }
  for (auto xd : vy) {
    for (int i = cy[xd.fs.fs]; i <= cy[xd.fs.sc]; i++) {
      vis[cx[xd.sc]][i] = 1;
    }
  }
  // vis[cx[0]][cy[0]] = 0;
  /*f(i,0,20){
  f(j,0,20){
    cout<<vis[i][j];
  }
  cout<<endl;
}*/
  bfs(cx[0], cy[0]);
  cout << ans << endl;
  return 0;
}