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

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> P;

vector<int> A;
vector<int> B;
vector<int> C;
vector<int> D;
vector<int> E;
vector<int> F;
vector<LL> xs;
vector<LL> ys;
int st[6001][6001];
bool isInf = false;
LL f(int p, int q) {
  stack<P> nex;
  LL ret = 0;
  nex.push(P(p, q));
  while (nex.size()) {
    p = nex.top().first;
    q = nex.top().second;
    nex.pop();
    if (st[p][q] & 16) {
      continue;
    }
    st[p][q] |= 16;
    // cerr << p << ", " << q << endl;
    if (p == 0 || q == 0 || p == xs.size() || q == ys.size()) {
      isInf = true;
      return 0;
    }
    ret += (xs[p] - xs[p - 1]) * (ys[q] - ys[q - 1]);

    if (!(st[p][q] & 1)) {
      nex.push(P(p, q + 1));
    }
    if (!(st[p][q] & 2)) {
      nex.push(P(p + 1, q));
    }
    if (!(st[p][q] & 4)) {
      nex.push(P(p, q - 1));
    }
    if (!(st[p][q] & 8)) {
      nex.push(P(p - 1, q));
    }
  }

  return ret;
}
int main() {
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    A.push_back(a);
    B.push_back(b);
    C.push_back(c);
    xs.push_back(a);
    xs.push_back(b);
    ys.push_back(c);
  }
  for (int i = 0; i < M; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    D.push_back(a);
    E.push_back(b);
    F.push_back(c);
    xs.push_back(a);
    ys.push_back(b);
    ys.push_back(c);
  }
  sort(xs.begin(), xs.end());
  xs.resize(unique(xs.begin(), xs.end()) - xs.begin());
  sort(ys.begin(), ys.end());
  ys.resize(unique(ys.begin(), ys.end()) - ys.begin());
  for (int i = 0; i < N; i++) {
    int q = lower_bound(ys.begin(), ys.end(), C[i]) - ys.begin();
    int p = lower_bound(xs.begin(), xs.end(), min(A[i], B[i])) - xs.begin();
    while (xs[p] < max(A[i], B[i])) {
      st[p + 1][q] |= 1;
      st[p + 1][q + 1] |= 4;
      p++;
    }
  }
  for (int i = 0; i < M; i++) {
    int q = lower_bound(ys.begin(), ys.end(), min(E[i], F[i])) - ys.begin();
    int p = lower_bound(xs.begin(), xs.end(), D[i]) - xs.begin();
    while (ys[q] < max(E[i], F[i])) {
      st[p][q + 1] |= 2;
      st[p + 1][q + 1] |= 8;
      q++;
    }
  }

  int u = lower_bound(xs.begin(), xs.end(), 0) - xs.begin();
  int v = lower_bound(ys.begin(), ys.end(), 0) - ys.begin();
  LL r = f(u, v);
  if (isInf) {
    cout << "INF" << endl;
    return 0;
  }
  cout << r << endl;

  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> P;

vector<int> A;
vector<int> B;
vector<int> C;
vector<int> D;
vector<int> E;
vector<int> F;
vector<LL> xs;
vector<LL> ys;
int st[6001][6001];
bool isInf = false;
LL f(int p, int q) {
  if (st[p][q] & 16) {
    return 0;
  }
  st[p][q] |= 16;
  // cerr << p << ", " << q << endl;
  if (p == 0 || q == 0 || p == xs.size() || q == ys.size()) {
    isInf = true;
    return 0;
  }
  LL ret = (xs[p] - xs[p - 1]) * (ys[q] - ys[q - 1]);

  if (!(st[p][q] & 1)) {
    ret += f(p, q + 1);
  }
  if (!(st[p][q] & 2)) {
    ret += f(p + 1, q);
  }
  if (!(st[p][q] & 4)) {
    ret += f(p, q - 1);
  }
  if (!(st[p][q] & 8)) {
    ret += f(p - 1, q);
  }

  return ret;
}
int main() {
  int N, M;
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    A.push_back(a);
    B.push_back(b);
    C.push_back(c);
    xs.push_back(a);
    xs.push_back(b);
    ys.push_back(c);
  }
  for (int i = 0; i < M; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    D.push_back(a);
    E.push_back(b);
    F.push_back(c);
    xs.push_back(a);
    ys.push_back(b);
    ys.push_back(c);
  }
  sort(xs.begin(), xs.end());
  xs.resize(unique(xs.begin(), xs.end()) - xs.begin());
  sort(ys.begin(), ys.end());
  ys.resize(unique(ys.begin(), ys.end()) - ys.begin());
  for (int i = 0; i < N; i++) {
    int q = lower_bound(ys.begin(), ys.end(), C[i]) - ys.begin();
    int p = lower_bound(xs.begin(), xs.end(), min(A[i], B[i])) - xs.begin();
    while (xs[p] < max(A[i], B[i])) {
      st[p + 1][q] |= 1;
      st[p + 1][q + 1] |= 4;
      p++;
    }
  }
  for (int i = 0; i < M; i++) {
    int q = lower_bound(ys.begin(), ys.end(), min(E[i], F[i])) - ys.begin();
    int p = lower_bound(xs.begin(), xs.end(), D[i]) - xs.begin();
    while (ys[q] < max(E[i], F[i])) {
      st[p][q + 1] |= 2;
      st[p + 1][q + 1] |= 8;
      q++;
    }
  }

  int u = lower_bound(xs.begin(), xs.end(), 0) - xs.begin();
  int v = lower_bound(ys.begin(), ys.end(), 0) - ys.begin();
  LL r = f(u, v);
  if (isInf) {
    cout << "INF" << endl;
    return 0;
  }
  cout << r << endl;

  return 0;
}