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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<int, int> i_i;

#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define SZ(x) ((ll)(x).size())

const double PI = acos(-1.0);
const double EPS = 1e-9;
const ll INF = 1e9 + 10;
const ll MOD = 1e9 + 7;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define print(x) cerr << #x << " is " << x << endl;

vector<ll> A(1001), B(1001), C(1001), D(1001), E(1001), F(1001);
vector<ll> AA(1001), BB(1001), CC(1001), DD(1001), EE(1001), FF(1001);
vector<ll> xs, ys;
vector<ll> xl(3006), yl(3006);
bool fld[3006][3006][4];
bool v[3006][3006];
// #define print(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << "
// " << __FILE__ << endl;
int main() {
  ll N, M, x0, y0;
  cin >> N >> M;
  rep(i, N) {
    cin >> A[i] >> B[i] >> C[i];
    xs.push_back(A[i]);
    xs.push_back(B[i]);
    ys.push_back(C[i]);
  }
  rep(i, M) {
    cin >> D[i] >> E[i] >> F[i];
    xs.push_back(D[i]);
    ys.push_back(E[i]);
    ys.push_back(F[i]);
  }
  xs.push_back(0), ys.push_back(0);
  sort(ALL(xs)), sort(ALL(ys));
  xs.push_back(xs[xs.size() - 1] + 1);
  xs.push_back(xs[0] - 1);
  ys.push_back(ys[ys.size() - 1] + 1);
  ys.push_back(ys[0] - 1);
  sort(ALL(xs)), sort(ALL(ys));
  xs.erase(unique(ALL(xs)), xs.end());
  ys.erase(unique(ALL(ys)), ys.end());

  rep(i, xs.size() - 1) {
    xl[i] = xs[i + 1] - xs[i];
    if (xs[i] == 0)
      x0 = i;
    // if (xs[i] <= 0 && xs[i + 1] >= 0){
    //     x0 = i - 1;
    // }
  }
  xl[xs.size() - 1] = 1;
  rep(i, ys.size() - 1) {
    yl[i] = ys[i + 1] - ys[i];
    if (ys[i] == 0)
      y0 = i;
    // if (ys[i] <= 0 && ys[i + 1] >= 0){
    //     y0 = i - 1;
    // }
  }
  yl[ys.size() - 1] = 1;
  // fprintf(stderr, "zero (%d, %d)\\n", x0, y0);
  rep(i, N) {
    AA[i] = find(ALL(xs), A[i]) - xs.begin();
    BB[i] = find(ALL(xs), B[i]) - xs.begin();
    CC[i] = find(ALL(ys), C[i]) - ys.begin();
    // fprintf(stderr, "abc(%d, %d) -> (%d, %d)\\n", AA[i], CC[i], BB[i], CC[i]);
    for (int j = AA[i]; j < BB[i]; j++) {
      fld[j][CC[i] - 1][0] = true;
      fld[j][CC[i]][2] = true;
    }
  }
  rep(i, M) {
    DD[i] = find(ALL(xs), D[i]) - xs.begin();
    EE[i] = find(ALL(ys), E[i]) - ys.begin();
    FF[i] = find(ALL(ys), F[i]) - ys.begin();
    for (int j = EE[i]; j < FF[i]; j++) {
      fld[DD[i] - 1][j][1] = true;
      fld[DD[i]][j][3] = true;
    }
    // fprintf(stderr, "def(%d, %d) -> (%d, %d)\\n", DD[i], EE[i], DD[i], FF[i]);
  }
  ll ans = 0;
  queue<P> que;
  que.push(make_pair(x0, y0));
  while (!que.empty()) {
    ll qx = que.front().first, qy = que.front().second;
    que.pop();
    if (qx == 0 || qx == xs.size() - 1 || qy == 0 || qy == ys.size() - 1) {
      cout << "INF";
      return 0;
    }
    if (v[qx][qy]) {
      continue;
    }
    // fprintf(stderr, "(%d, %d)\\n", qx, qy);
    v[qx][qy] = true;

    ans += xl[qx] * yl[qy];

    for (int j = 0; j < 4; j++) {
      if (qx >= 1 && qx < xs.size() - 1 && qy >= 1 && qy < ys.size() - 1 &&
          !fld[qx][qy][j]) {
        if (j == 0)
          que.push(make_pair(qx, qy + 1));
        if (j == 1)
          que.push(make_pair(qx + 1, qy));
        if (j == 2)
          que.push(make_pair(qx, qy - 1));
        if (j == 3)
          que.push(make_pair(qx - 1, qy));
      }
    }
  }
  cout << ans;

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<int, int> i_i;

#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define SZ(x) ((ll)(x).size())

const double PI = acos(-1.0);
const double EPS = 1e-9;
const ll INF = 1e9 + 10;
const ll MOD = 1e9 + 7;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define print(x) cerr << #x << " is " << x << endl;

vector<ll> A(1001), B(1001), C(1001), D(1001), E(1001), F(1001);
vector<ll> AA(1001), BB(1001), CC(1001), DD(1001), EE(1001), FF(1001);
vector<ll> xs, ys;
vector<ll> xl(3006), yl(3006);
bool fld[3006][3006][4];
bool v[3006][3006];
// #define print(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << "
// " << __FILE__ << endl;
int main() {
  ll N, M, x0, y0;
  cin >> N >> M;
  rep(i, N) {
    cin >> A[i] >> B[i] >> C[i];
    xs.push_back(A[i]);
    xs.push_back(B[i]);
    ys.push_back(C[i]);
  }
  rep(i, M) {
    cin >> D[i] >> E[i] >> F[i];
    xs.push_back(D[i]);
    ys.push_back(E[i]);
    ys.push_back(F[i]);
  }

  sort(ALL(xs)), sort(ALL(ys));
  xs.push_back(xs[xs.size() - 1] + 1);
  xs.push_back(xs[0] - 1);
  ys.push_back(ys[ys.size() - 1] + 1);
  ys.push_back(ys[0] - 1);
  sort(ALL(xs)), sort(ALL(ys));
  xs.erase(unique(ALL(xs)), xs.end());
  ys.erase(unique(ALL(ys)), ys.end());

  rep(i, xs.size() - 1) {
    xl[i] = xs[i + 1] - xs[i];
    if (xs[i] <= 0 && xs[i + 1] >= 0) {
      x0 = i - 1;
    }
  }
  xl[xs.size() - 1] = 1;
  rep(i, ys.size() - 1) {
    yl[i] = ys[i + 1] - ys[i];
    if (ys[i] <= 0 && ys[i + 1] >= 0) {
      y0 = i - 1;
    }
  }
  yl[ys.size() - 1] = 1;
  // fprintf(stderr, "zero (%d, %d)\\n", x0, y0);
  rep(i, N) {
    AA[i] = find(ALL(xs), A[i]) - xs.begin();
    BB[i] = find(ALL(xs), B[i]) - xs.begin();
    CC[i] = find(ALL(ys), C[i]) - ys.begin();
    // fprintf(stderr, "abc(%d, %d) -> (%d, %d)\\n", AA[i], CC[i], BB[i], CC[i]);
    for (int j = AA[i]; j < BB[i]; j++) {
      fld[j][CC[i] - 1][0] = true;
      fld[j][CC[i]][2] = true;
    }
  }
  rep(i, M) {
    DD[i] = find(ALL(xs), D[i]) - xs.begin();
    EE[i] = find(ALL(ys), E[i]) - ys.begin();
    FF[i] = find(ALL(ys), F[i]) - ys.begin();
    for (int j = EE[i]; j < FF[i]; j++) {
      fld[DD[i] - 1][j][1] = true;
      fld[DD[i]][j][3] = true;
    }
    // fprintf(stderr, "def(%d, %d) -> (%d, %d)\\n", DD[i], EE[i], DD[i], FF[i]);
  }
  ll ans = 0;
  queue<P> que;
  que.push(make_pair(x0, y0));
  while (!que.empty()) {
    ll qx = que.front().first, qy = que.front().second;
    que.pop();
    if (qx == 0 || qx == xs.size() - 1 || qy == 0 || qy == ys.size() - 1) {
      cout << "INF";
      return 0;
    }
    if (v[qx][qy]) {
      continue;
    }
    // fprintf(stderr, "(%d, %d)\\n", qx, qy);
    v[qx][qy] = true;

    ans += xl[qx] * yl[qy];

    for (int j = 0; j < 4; j++) {
      if (qx >= 1 && qx < xs.size() - 1 && qy >= 1 && qy < ys.size() - 1 &&
          !fld[qx][qy][j]) {
        if (j == 0)
          que.push(make_pair(qx, qy + 1));
        if (j == 1)
          que.push(make_pair(qx + 1, qy));
        if (j == 2)
          que.push(make_pair(qx, qy - 1));
        if (j == 3)
          que.push(make_pair(qx - 1, qy));
      }
    }
  }
  cout << ans;

  return 0;
}