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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long int LL;
typedef pair<int, int> P;
typedef pair<LL, int> LP;
const int INF = 1 << 30;
const LL MAX = 1e9 + 7;

void array_show(int *array, int array_n, char middle = ' ') {
  for (int i = 0; i < array_n; i++)
    printf("%d%c", array[i], (i != array_n - 1 ? middle : '\\n'));
}
void array_show(LL *array, int array_n, char middle = ' ') {
  for (int i = 0; i < array_n; i++)
    printf("%lld%c", array[i], (i != array_n - 1 ? middle : '\\n'));
}
void array_show(vector<int> &vec_s, int vec_n = -1, char middle = ' ') {
  if (vec_n == -1)
    vec_n = vec_s.size();
  for (int i = 0; i < vec_n; i++)
    printf("%d%c", vec_s[i], (i != vec_n - 1 ? middle : '\\n'));
}
void array_show(vector<LL> &vec_s, int vec_n = -1, char middle = ' ') {
  if (vec_n == -1)
    vec_n = vec_s.size();
  for (int i = 0; i < vec_n; i++)
    printf("%lld%c", vec_s[i], (i != vec_n - 1 ? middle : '\\n'));
}

int t[2200][3];
map<LL, int> m1[2];
bool used[6200][6200];
LL sq[6200][6200];
vector<LL> v[2];
const int N = 6100;
queue<P> q1;

int main() {
  int n, m;
  int i, j, k;
  LL a, b, c, d;
  cin >> n >> m;
  m1[0][0] = 1, m1[1][0] = 1;
  for (i = 0; i < n + m; i++) {
    for (j = 0; j < 3; j++) {
      cin >> t[i][j];
      if (i < n) {
        if (j < 2)
          a = 0;
        else
          a = 1;
      } else {
        if (j < 1)
          a = 0;
        else
          a = 1;
      }
      m1[a][t[i][j]] = 1;
    }
  }
  for (i = 0; i < 2; i++) {
    j = 0;
    for (auto &node : m1[i]) {
      node.second = j++;
      v[i].push_back(node.first);
    }
  }
  for (i = 0; i < n + m; i++) {
    if (i < n) {
      a = m1[0][t[i][0]] * 2, b = m1[1][t[i][2]] * 2;
      c = m1[0][t[i][1]] * 2, d = m1[1][t[i][2]] * 2;
    } else {
      a = m1[0][t[i][0]] * 2, b = m1[1][t[i][1]] * 2;
      c = m1[0][t[i][0]] * 2, d = m1[1][t[i][2]] * 2;
    }
    while (a <= c && b <= d) {
      used[a][b] = true;
      if (i < n)
        a++;
      else
        b++;
    }
  }
  for (i = 0; i + 1 < v[0].size(); i++) {
    for (j = 0; j + 1 < v[1].size(); j++) {
      sq[2 * i + 1][2 * j + 1] =
          (v[0][i + 1] - v[0][i]) * (v[1][j + 1] - v[1][j]);
    }
  }
  a = m1[0][0], b = m1[1][0];
  q1.push(make_pair(a * 2, b * 2));
  LL s = 0;
  while (!q1.empty()) {
    a = q1.front().first, b = q1.front().second;
    q1.pop();
    if (a < 0 || a > N || b < 0 || b > N) {
      s = -1;
      break;
    }
    if (used[a][b])
      continue;
    used[a][b] = true;
    s += sq[a][b];
    q1.push(make_pair(a + 1, b));
    q1.push(make_pair(a - 1, b));
    q1.push(make_pair(a, b - 1));
    q1.push(make_pair(a, b + 1));
  }
  if (s == -1)
    cout << "INF" << endl;
  else
    cout << s << endl;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long int LL;
typedef pair<int, int> P;
typedef pair<LL, int> LP;
const int INF = 1 << 30;
const LL MAX = 1e9 + 7;

void array_show(int *array, int array_n, char middle = ' ') {
  for (int i = 0; i < array_n; i++)
    printf("%d%c", array[i], (i != array_n - 1 ? middle : '\\n'));
}
void array_show(LL *array, int array_n, char middle = ' ') {
  for (int i = 0; i < array_n; i++)
    printf("%lld%c", array[i], (i != array_n - 1 ? middle : '\\n'));
}
void array_show(vector<int> &vec_s, int vec_n = -1, char middle = ' ') {
  if (vec_n == -1)
    vec_n = vec_s.size();
  for (int i = 0; i < vec_n; i++)
    printf("%d%c", vec_s[i], (i != vec_n - 1 ? middle : '\\n'));
}
void array_show(vector<LL> &vec_s, int vec_n = -1, char middle = ' ') {
  if (vec_n == -1)
    vec_n = vec_s.size();
  for (int i = 0; i < vec_n; i++)
    printf("%lld%c", vec_s[i], (i != vec_n - 1 ? middle : '\\n'));
}

int t[2200][3];
map<LL, int> m1[2];
bool used[6200][6200];
LL sq[6200][6200];
const int N = 6100;

LL check(int a, int b) {
  if (a < 0 || a > N || b < 0 || b > N)
    return -1;
  if (used[a][b])
    return 0;
  used[a][b] = true;
  LL s = sq[a][b];
  LL p;
  for (int i = -1; i <= 1; i++) {
    for (int j = -1; j <= 1; j++) {
      if ((i + j + 2) % 2 == 0)
        continue;
      p = check(a + i, b + j);
      if (p == -1)
        return -1;
      s += p;
    }
  }
  return s;
}

int main() {
  int n, m;
  int i, j, k;
  LL a, b, c, d;
  cin >> n >> m;
  m1[0][0] = 1, m1[1][0] = 1;
  for (i = 0; i < n + m; i++) {
    for (j = 0; j < 3; j++) {
      cin >> t[i][j];
      if (i < n) {
        if (j < 2)
          a = 0;
        else
          a = 1;
      } else {
        if (j < 1)
          a = 0;
        else
          a = 1;
      }
      m1[a][t[i][j]] = 1;
    }
  }
  vector<LL> v[2];
  for (i = 0; i < 2; i++) {
    j = 0;
    for (auto &node : m1[i]) {
      node.second = j++;
      v[i].push_back(node.first);
    }
  }
  for (i = 0; i < n + m; i++) {
    if (i < n) {
      a = m1[0][t[i][0]] * 2, b = m1[1][t[i][2]] * 2;
      c = m1[0][t[i][1]] * 2, d = m1[1][t[i][2]] * 2;
    } else {
      a = m1[0][t[i][0]] * 2, b = m1[1][t[i][1]] * 2;
      c = m1[0][t[i][0]] * 2, d = m1[1][t[i][2]] * 2;
    }
    while (a <= c && b <= d) {
      used[a][b] = true;
      if (i < n)
        a++;
      else
        b++;
    }
  }
  for (i = 0; i + 1 < v[0].size(); i++) {
    for (j = 0; j + 1 < v[1].size(); j++) {
      sq[2 * i + 1][2 * j + 1] =
          (v[0][i + 1] - v[0][i]) * (v[1][j + 1] - v[1][j]);
    }
  }
  a = m1[0][0], b = m1[1][0];
  a = check(a * 2, b * 2);
  if (a == -1)
    cout << "INF" << endl;
  else
    cout << a << endl;
}