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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;

template <class T> void debug(T itr1, T itr2) {
  auto now = itr1;
  while (now < itr2) {
    cout << *now << " ";
    now++;
  }
  cout << endl;
}
#define repr(i, from, to) for (int i = (int)from; i < (int)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i, N) repr(i, 0, N)
#define per(i, N) for (int i = (int)N - 1; i >= 0; i--)

const ll MOD = pow(10, 9) + 7;
const ll LLINF = pow(2, 61) - 1;
const int INF = pow(2, 30) - 1;

vector<ll> fac;
void c_fac(int x = pow(10, 6) + 10) {
  fac.resize(x, true);
  rep(i, x) fac[i] = i ? (fac[i - 1] * i) % MOD : 1;
}
ll inv(ll a, ll m = MOD) {
  ll b = m, x = 1, y = 0;
  while (b != 0) {
    int d = a / b;
    a -= b * d;
    swap(a, b);
    x -= y * d;
    swap(x, y);
  }
  return (x + m) % m;
}
ll nck(ll n, ll k) { return fac[n] * inv(fac[k] * fac[n - k] % MOD) % MOD; }
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  return b == 0 ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

struct query {
  ll A, B, C;
};

vector<vector<vector<bool>>> adj;
vector<vector<bool>> used;

void dfs(int x, int y) {
  if (used[x][y])
    return;
  // cout << x << "," << y << endl;
  used[x][y] = true;
  if (adj[x][y][0] && !used[x - 1][y])
    dfs(x - 1, y);
  if (adj[x][y][1] && !used[x][y - 1])
    dfs(x, y - 1);
  if (adj[x][y][2] && !used[x + 1][y])
    dfs(x + 1, y);
  if (adj[x][y][3] && !used[x][y + 1])
    dfs(x, y + 1);
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<query> qx, qy;
  set<ll> setx, sety;
  rep(i, N) {
    ll A, B, C;
    cin >> A >> B >> C;
    if (A > B)
      swap(A, B);
    qx.push_back({A, B, C});
    sety.insert(A);
    sety.insert(B);
    setx.insert(C);
  }
  rep(i, M) {
    ll A, B, C;
    cin >> A >> B >> C;
    if (B > C)
      swap(B, C);
    qy.push_back({B, C, A});
    sety.insert(A);
    setx.insert(B);
    setx.insert(C);
  }
  vector<ll> vx, vy;
  for (auto x : setx)
    vx.push_back(x);
  for (auto x : sety)
    vy.push_back(x);

  // debug(all(vx)); debug(all(vy));

  ll sx = vy.size() + 1, sy = vx.size() + 1;
  adj.resize(sx);
  rep(i, sx) {
    adj[i].resize(sy);
    rep(j, sy) adj[i][j].resize(4, true);
  }
  used.resize(sx);
  rep(i, sx) used[i].resize(sy, false);

  rep(i, sx) rep(j, sy) {
    if (i == 0)
      adj[i][j][0] = false;
    if (i == sx - 1)
      adj[i][j][2] = false;
    if (j == 0)
      adj[i][j][1] = false;
    if (j == sy - 1)
      adj[i][j][3] = false;
  }
  rep(i, N) {
    int pa = lower_bound(all(vy), qx[i].A) - vy.begin();
    int pb = lower_bound(all(vy), qx[i].B) - vy.begin();
    int pc = lower_bound(all(vx), qx[i].C) - vx.begin();
    // cout << i << " " << pa << "-" << pb << ", " << pc << endl;
    repr(j, pa + 1, pb + 1) {
      adj[j][pc][3] = false;
      adj[j][pc + 1][1] = false;
    }
  }
  rep(i, M) {
    int pa = lower_bound(all(vx), qy[i].A) - vx.begin();
    int pb = lower_bound(all(vx), qy[i].B) - vx.begin();
    int pc = lower_bound(all(vy), qy[i].C) - vy.begin();
    // cout << i << " " << pa << "-" << pb << ", " << pc << endl;
    repr(j, pa + 1, pb + 1) {
      adj[pc][j][2] = false;
      adj[pc + 1][j][0] = false;
    }
  }
  // rep(i,sx) { rep(j,sy) cout << adj[i][j][2] << " "; cout << endl; }

  int spx = lower_bound(all(vy), 0) - vy.begin();
  int spy = lower_bound(all(vx), 0) - vx.begin();
  // dfs(spx,spy);

  queue<p_ll> que;
  que.push(make_pair(spx, spy));
  used[spx][spy] = true;
  while (que.size()) {
    p_ll qt = que.front();
    que.pop();
    ll x = qt.first, y = qt.second;
    if (adj[x][y][0] && !used[x - 1][y]) {
      used[x - 1][y] = true;
      que.push(make_pair(x - 1, y));
    }
    if (adj[x][y][1] && !used[x][y - 1]) {
      used[x][y - 1] = true;
      que.push(make_pair(x, y - 1));
    }
    if (adj[x][y][2] && !used[x + 1][y]) {
      used[x + 1][y] = true;
      que.push(make_pair(x + 1, y));
    }
    if (adj[x][y][3] && !used[x][y + 1]) {
      used[x][y + 1] = true;
      que.push(make_pair(x, y + 1));
    }
  }

  // rep(i,sx) debug(all(used[i]));

  ll result = 0;
  rep(i, sx) rep(j, sy) {
    if (used[i][j]) {
      if (i == 0 || i == sx - 1 || j == 0 || j == sy - 1) {
        cout << "INF" << endl;
        return 0;
      } else
        result += (vy[i] - vy[i - 1]) * (vx[j] - vx[j - 1]);
    }
  }
  cout << result << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;

template <class T> void debug(T itr1, T itr2) {
  auto now = itr1;
  while (now < itr2) {
    cout << *now << " ";
    now++;
  }
  cout << endl;
}
#define repr(i, from, to) for (int i = (int)from; i < (int)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i, N) repr(i, 0, N)
#define per(i, N) for (int i = (int)N - 1; i >= 0; i--)

const ll MOD = pow(10, 9) + 7;
const ll LLINF = pow(2, 61) - 1;
const int INF = pow(2, 30) - 1;

vector<ll> fac;
void c_fac(int x = pow(10, 6) + 10) {
  fac.resize(x, true);
  rep(i, x) fac[i] = i ? (fac[i - 1] * i) % MOD : 1;
}
ll inv(ll a, ll m = MOD) {
  ll b = m, x = 1, y = 0;
  while (b != 0) {
    int d = a / b;
    a -= b * d;
    swap(a, b);
    x -= y * d;
    swap(x, y);
  }
  return (x + m) % m;
}
ll nck(ll n, ll k) { return fac[n] * inv(fac[k] * fac[n - k] % MOD) % MOD; }
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  return b == 0 ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

struct query {
  ll A, B, C;
};

vector<vector<vector<bool>>> adj;
vector<vector<bool>> used;

void dfs(int x, int y) {
  if (used[x][y])
    return;
  // cout << x << "," << y << endl;
  used[x][y] = true;
  if (adj[x][y][0] && !used[x - 1][y])
    dfs(x - 1, y);
  if (adj[x][y][1] && !used[x][y - 1])
    dfs(x, y - 1);
  if (adj[x][y][2] && !used[x + 1][y])
    dfs(x + 1, y);
  if (adj[x][y][3] && !used[x][y + 1])
    dfs(x, y + 1);
}

int main() {
  int N, M;
  cin >> N >> M;
  vector<query> qx, qy;
  set<ll> setx, sety;
  rep(i, N) {
    ll A, B, C;
    cin >> A >> B >> C;
    if (A > B)
      swap(A, B);
    qx.push_back({A, B, C});
    sety.insert(A);
    sety.insert(B);
    setx.insert(C);
  }
  rep(i, M) {
    ll A, B, C;
    cin >> A >> B >> C;
    if (B > C)
      swap(B, C);
    qy.push_back({B, C, A});
    sety.insert(A);
    setx.insert(B);
    setx.insert(C);
  }
  vector<ll> vx, vy;
  for (auto x : setx)
    vx.push_back(x);
  for (auto x : sety)
    vy.push_back(x);

  // debug(all(vx)); debug(all(vy));

  ll sx = vy.size() + 1, sy = vx.size() + 1;
  adj.resize(sx);
  rep(i, sx) {
    adj[i].resize(sy);
    rep(j, sy) adj[i][j].resize(4, true);
  }
  used.resize(sx);
  rep(i, sx) used[i].resize(sy, false);

  rep(i, sx) rep(j, sy) {
    if (i == 0)
      adj[i][j][0] = false;
    if (i == sx - 1)
      adj[i][j][2] = false;
    if (j == 0)
      adj[i][j][1] = false;
    if (j == sy - 1)
      adj[i][j][3] = false;
  }
  rep(i, N) {
    int pa = lower_bound(all(vy), qx[i].A) - vy.begin();
    int pb = lower_bound(all(vy), qx[i].B) - vy.begin();
    int pc = lower_bound(all(vx), qx[i].C) - vx.begin();
    // cout << i << " " << pa << "-" << pb << ", " << pc << endl;
    repr(j, pa + 1, pb + 1) {
      adj[j][pc][3] = false;
      adj[j][pc + 1][1] = false;
    }
  }
  rep(i, M) {
    int pa = lower_bound(all(vx), qy[i].A) - vx.begin();
    int pb = lower_bound(all(vx), qy[i].B) - vx.begin();
    int pc = lower_bound(all(vy), qy[i].C) - vy.begin();
    // cout << i << " " << pa << "-" << pb << ", " << pc << endl;
    repr(j, pa + 1, pb + 1) {
      adj[pc][j][2] = false;
      adj[pc + 1][j][0] = false;
    }
  }
  // rep(i,sx) { rep(j,sy) cout << adj[i][j][2] << " "; cout << endl; }

  int spx = lower_bound(all(vy), 0) - vy.begin();
  int spy = lower_bound(all(vx), 0) - vx.begin();
  dfs(spx, spy);

  // rep(i,sx) debug(all(used[i]));

  ll result = 0;
  rep(i, sx) rep(j, sy) {
    if (used[i][j]) {
      if (i == 0 || i == sx - 1 || j == 0 || j == sy - 1) {
        cout << "INF" << endl;
        return 0;
      } else
        result += (vy[i] - vy[i - 1]) * (vx[j] - vx[j - 1]);
    }
  }
  cout << result << endl;
  return 0;
}