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

#include <bits/stdc++.h>

#define pb push_back
#define fi first
#define se second
#define eb emplace_back
#define int ll
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
const int N = 10007, inf = 0x3f3f3f3f;

void fly() {
  cout << "INF\\n";
  exit(0);
}

char bad[N][N];
char vis[N][N];

// bool conta[N][N];

vector<ii> moves = {{0, 1},   {0, -1}, {1, 0},  {-1, 0},
                    {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n, m;
  cin >> n >> m;

  vector<int> I, J;
  vector<pair<ii, ii>> hor, ver;
  for (int i = 0; i < n; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a *= 2, b *= 2, c *= 2;

    ver.emplace_back(make_pair(a, c), make_pair(b, c));

    I.pb(a - 1);
    I.pb(a);
    I.pb(a + 1);

    I.pb(b - 1);
    I.pb(b);
    I.pb(b + 1);

    J.pb(c);
    J.pb(c + 1);
    J.pb(c - 1);
  }

  for (int i = 0; i < m; i++) {
    int d, e, f;
    cin >> d >> e >> f;
    d *= 2, e *= 2, f *= 2;

    hor.emplace_back(make_pair(d, e), make_pair(d, f));

    J.pb(e - 1);
    J.pb(e);
    J.pb(e + 1);

    J.pb(f - 1);
    J.pb(f);
    J.pb(f + 1);

    I.pb(d - 1);
    I.pb(d);
    I.pb(d + 1);
  }

  I.pb(0);
  J.pb(0);

  sort(I.begin(), I.end());
  I.erase(unique(I.begin(), I.end()), I.end());
  sort(J.begin(), J.end());
  J.erase(unique(J.begin(), J.end()), J.end());

  // monta paredes
  // vertical

  for (int k = 0; k < n; ++k) {
    int p, q;
    tie(p, q) = ver[k].first;

    int i = lower_bound(I.begin(), I.end(), p) - I.begin();
    int j = lower_bound(J.begin(), J.end(), q) - J.begin();

    tie(p, q) = ver[k].se;

    for (; I[i] != p or J[j] != q; i++) {
      bad[i][j] = true;
    }
    bad[i][j] = true;
  }

  // horizontal
  for (int k = 0; k < m; ++k) {
    int p, q;
    tie(p, q) = hor[k].first;

    int i = lower_bound(I.begin(), I.end(), p) - I.begin();
    int j = lower_bound(J.begin(), J.end(), q) - J.begin();

    tie(p, q) = hor[k].se;

    for (; I[i] != p or J[j] != q; j++) {
      bad[i][j] = true;
    }
    bad[i][j] = true;
  }

  n = I.size();
  m = J.size();

  // for(int i = 0; i < n; ++i) {
  // 	for(int j = 0; j < m; ++j) {
  // 		cout << (int)bad[i][j] << ' ';
  // 	}
  // 	cout << '\\n';
  // }
  // acha a posicao inicial
  int p, q;
  p = lower_bound(I.begin(), I.end(), 0) - I.begin();
  q = lower_bound(J.begin(), J.end(), 0) - J.begin();

  assert(bad[p][q] == false);
  // faz a dfs e ja ve se da infinito

  queue<ii> qu;
  qu.push({p, q});

  vis[p][q] = 1;
  while (qu.size()) {
    int i, j;
    tie(i, j) = qu.front();
    qu.pop();

    for (ii aux : moves) {
      int u = i + aux.fi;
      int v = j + aux.se;

      if (u >= 0 and u < n and j >= 0 and j < m) {
        if (!vis[u][v]) {
          vis[u][v] = 1;
          if (!bad[u][v])
            qu.push({u, v});
        }
      } else {
        fly();
      }
    }
  }

  unsigned long long area = 0;
  for (int i = 1; i < n; ++i) {
    for (int j = 1; j < m; ++j) {

      if (vis[i][j] and vis[i - 1][j - 1])
        if (vis[i][j] and vis[i - 1][j] and vis[i][j - 1] and
            vis[i - 1][j - 1]) {
          // conta[i][j] = true;
          area += (I[i] - I[i - 1]) * (J[j] - J[j - 1]);
        }
    }
  }

  // cout << endl;
  // for(int i = 0; i < n; ++i) {
  // 	for(int j = 0; j < m; ++j) {
  // 		cout << (bad[i][j] +  2 * conta[i][j]) << ' ';
  // 	}
  // 	cout << '\\n';
  // }

  assert(area % 4 == 0);
  cout << area / 4 << '\\n';
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>

#define pb push_back
#define fi first
#define se second
#define eb emplace_back
#define int ll
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
const int N = 10007, inf = 0x3f3f3f3f;

void fly() {
  cout << "INF\\n";
  exit(0);
}

char bad[N][N];
char vis[N][N];

// bool conta[N][N];

vector<ii> moves = {{0, 1},   {0, -1}, {1, 0},  {-1, 0},
                    {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n, m;
  cin >> n >> m;

  vector<int> I, J;
  vector<pair<ii, ii>> hor, ver;
  for (int i = 0; i < n; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a *= 2, b *= 2, c *= 2;

    ver.emplace_back(make_pair(a, c), make_pair(b, c));

    I.pb(a - 1);
    I.pb(a);
    I.pb(a + 1);

    I.pb(b - 1);
    I.pb(b);
    I.pb(b + 1);

    J.pb(c);
    J.pb(c + 1);
    J.pb(c - 1);
  }

  for (int i = 0; i < m; i++) {
    int d, e, f;
    cin >> d >> e >> f;
    d *= 2, e *= 2, f *= 2;

    hor.emplace_back(make_pair(d, e), make_pair(d, f));

    J.pb(e - 1);
    J.pb(e);
    J.pb(e + 1);

    J.pb(f - 1);
    J.pb(f);
    J.pb(f + 1);

    I.pb(d - 1);
    I.pb(d);
    I.pb(d + 1);
  }

  I.pb(0);
  J.pb(0);

  sort(I.begin(), I.end());
  I.erase(unique(I.begin(), I.end()), I.end());
  sort(J.begin(), J.end());
  J.erase(unique(J.begin(), J.end()), J.end());

  // monta paredes
  // vertical

  for (int k = 0; k < n; ++k) {
    int p, q;
    tie(p, q) = ver[k].first;

    int i = lower_bound(I.begin(), I.end(), p) - I.begin();
    int j = lower_bound(J.begin(), J.end(), q) - J.begin();

    tie(p, q) = ver[k].se;

    for (; I[i] != p or J[j] != q; i++) {
      bad[i][j] = true;
    }
    bad[i][j] = true;
  }

  // horizontal
  for (int k = 0; k < m; ++k) {
    int p, q;
    tie(p, q) = hor[k].first;

    int i = lower_bound(I.begin(), I.end(), p) - I.begin();
    int j = lower_bound(J.begin(), J.end(), q) - J.begin();

    tie(p, q) = hor[k].se;

    for (; I[i] != p or J[j] != q; j++) {
      bad[i][j] = true;
    }
    bad[i][j] = true;
  }

  n = I.size();
  m = J.size();

  // for(int i = 0; i < n; ++i) {
  // 	for(int j = 0; j < m; ++j) {
  // 		cout << (int)bad[i][j] << ' ';
  // 	}
  // 	cout << '\\n';
  // }
  // acha a posicao inicial
  int p, q;
  p = lower_bound(I.begin(), I.end(), 0) - I.begin();
  q = lower_bound(J.begin(), J.end(), 0) - J.begin();

  assert(bad[p][q] == false);
  // faz a dfs e ja ve se da infinito

  queue<ii> qu;
  qu.push({p, q});

  vis[p][q] = 1;
  while (qu.size()) {
    int i, j;
    tie(i, j) = qu.front();
    qu.pop();

    for (ii aux : moves) {
      int u = i + aux.fi;
      int v = j + aux.se;

      if (u >= 0 and u < n and j >= 0 and j < m) {
        if (!vis[u][v]) {
          vis[u][v] = 1;
          if (!bad[u][v])
            qu.push({u, v});
        }
      } else {
        fly();
      }
    }
  }

  int area = 0;
  for (int i = 1; i < n; ++i) {
    for (int j = 1; j < m; ++j) {
      if (vis[i][j] and vis[i - 1][j - 1]) {
        area += (I[i] - I[i - 1]) * (J[j] - J[j - 1]);
      }
    }
  }

  // cout << endl;
  // for(int i = 0; i < n; ++i) {
  // 	for(int j = 0; j < m; ++j) {
  // 		cout << (bad[i][j] +  2 * conta[i][j]) << ' ';
  // 	}
  // 	cout << '\\n';
  // }

  assert(area % 4 == 0);
  cout << area / 4 << '\\n';
}