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

#include <bits/stdc++.h>

typedef long long ll;

using namespace std;

const ll MOD = 1e9 + 7;
const int N = 6010;

#define all(x) (x).begin(), (x).end()
#define int long long

int a[N], b[N], c[N], d[N], e[N], f[N];
vector<int> x, y;
int n, m, h, w;
int F[N][N];
int xline[N], yline[N];
int cx[4] = {0, 1, 0, -1};
int cy[4] = {1, 0, -1, 0};

int ok(int x, int y) {
  return 0 <= x && x < h && 0 <= y && y < w && F[x][y] == 0;
}
void demon() {
  return;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cout << F[i][j];
    }
    cout << "\\n";
  }
}
ll Sq(int i, int j) {
  if (i % 2 == 0 && j % 2 == 0) {
    return (xline[i + 1] - xline[i - 1]) * (yline[j + 1] - yline[j - 1]);
  }
  return 0;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    x.push_back(a[i]);
    x.push_back(b[i]);
    y.push_back(c[i]);
  }
  for (int i = 0; i < m; i++) {
    cin >> d[i] >> e[i] >> f[i];
    x.push_back(d[i]);
    y.push_back(e[i]);
    y.push_back(f[i]);
  }
  x.push_back(0);
  y.push_back(0);
  sort(all(x));
  x.resize(unique(all(x)) - x.begin());
  sort(all(y));
  y.resize(unique(all(y)) - y.begin());
  for (int i = 0; i < x.size(); i++) {
    xline[2 * i + 1] = x[i];
  }
  for (int i = 0; i < y.size(); i++) {
    yline[2 * i + 1] = y[i];
  }
  h = x.size() * 2 + 1;
  w = y.size() * 2 + 1;
  for (int i = 0; i < n; i++) {
    for (int j = 1; j < w; j += 2) {
      if (yline[j] == c[i]) {
        // cout << "ENTER" << endl;
        // cout << a[i] << " < " << b[i] << " " << j << endl;
        int t = 0;
        for (int k = 0; k < h; k++) {
          if (k % 2 == 1 && xline[k] == a[i])
            t = 1;
          if (t)
            F[k][j] = 1;
          if (k % 2 == 1 && xline[k] == b[i])
            t = 0;
        }
      }
    }
  }
  demon();
  for (int i = 0; i < m; i++) {
    for (int j = 1; j < h; j += 2) {
      if (xline[j] == d[i]) {
        // cout << "ENTER" << endl;
        // cout << e[i] << " < " << f[i] << " " << j << endl;
        int t = 0;
        for (int k = 0; k < w; k++) {
          if (k % 2 == 1 && yline[k] == e[i])
            t = 1;
          if (t)
            F[j][k] = 1;
          if (k % 2 == 1 && yline[k] == f[i])
            t = 0;
        }
      }
    }
  }
  demon();
  vector<pair<int, int>> s;
  ll ans = 0;
  for (int i = 1; i < h; i += 2)
    for (int j = 1; j < w; j += 2)
      if (xline[i] == 0 && yline[j] == 0)
        s.push_back({i, j}), ans += Sq(i, j), F[i][j] = 2;
  assert(s.size() == 1);
  for (int i = 0; i < s.size(); i++) {
    for (int k = 0; k < 4; k++) {
      int nx = s[i].first + cx[k];
      int ny = s[i].second + cy[k];
      if (ok(nx, ny)) {
        if (nx == 0 || nx == h - 1 || ny == 0 || ny == w - 1)
          cout << "INF", exit(0);
        s.push_back({nx, ny});
        ans += Sq(nx, ny);
        F[nx][ny] = 2;
      }
    }
  }
  demon();
  cout << ans;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

typedef long long ll;

using namespace std;

const ll MOD = 1e9 + 7;
const int N = 3010;

#define all(x) (x).begin(), (x).end()
#define int long long

int a[N], b[N], c[N], d[N], e[N], f[N];
vector<int> x, y;
int n, m, h, w;
long long s[N][N];
int F[N][N];
int xline[N], yline[N];
int cx[4] = {0, 1, 0, -1};
int cy[4] = {1, 0, -1, 0};

int ok(int x, int y) {
  return 0 <= x && x < h && 0 <= y && y < w && F[x][y] == 0;
}
void demon() {
  return;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cout << F[i][j];
    }
    cout << "\\n";
  }
}
ll Sq(int i, int j) {
  if (i % 2 == 0 && j % 2 == 0) {
    return (xline[i + 1] - xline[i - 1]) * (yline[j + 1] - yline[j - 1]);
  }
  return 0;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    x.push_back(a[i]);
    x.push_back(b[i]);
    y.push_back(c[i]);
  }
  for (int i = 0; i < m; i++) {
    cin >> d[i] >> e[i] >> f[i];
    x.push_back(d[i]);
    y.push_back(e[i]);
    y.push_back(f[i]);
  }
  x.push_back(0);
  y.push_back(0);
  sort(all(x));
  x.resize(unique(all(x)) - x.begin());
  sort(all(y));
  y.resize(unique(all(y)) - y.begin());
  for (int i = 0; i < x.size(); i++) {
    xline[2 * i + 1] = x[i];
  }
  for (int i = 0; i < y.size(); i++) {
    yline[2 * i + 1] = y[i];
  }
  h = x.size() * 2 + 1;
  w = y.size() * 2 + 1;
  for (int i = 0; i < n; i++) {
    for (int j = 1; j < w; j += 2) {
      if (yline[j] == c[i]) {
        // cout << "ENTER" << endl;
        // cout << a[i] << " < " << b[i] << " " << j << endl;
        int t = 0;
        for (int k = 0; k < h; k++) {
          if (k % 2 == 1 && xline[k] == a[i])
            t = 1;
          if (t)
            F[k][j] = 1;
          if (k % 2 == 1 && xline[k] == b[i])
            t = 0;
        }
      }
    }
  }
  demon();
  for (int i = 0; i < m; i++) {
    for (int j = 1; j < h; j += 2) {
      if (xline[j] == d[i]) {
        // cout << "ENTER" << endl;
        // cout << e[i] << " < " << f[i] << " " << j << endl;
        int t = 0;
        for (int k = 0; k < w; k++) {
          if (k % 2 == 1 && yline[k] == e[i])
            t = 1;
          if (t)
            F[j][k] = 1;
          if (k % 2 == 1 && yline[k] == f[i])
            t = 0;
        }
      }
    }
  }
  demon();
  vector<pair<int, int>> s;
  ll ans = 0;
  for (int i = 1; i < h; i += 2)
    for (int j = 1; j < w; j += 2)
      if (xline[i] == 0 && yline[j] == 0)
        s.push_back({i, j}), ans += Sq(i, j), F[i][j] = 2;
  assert(s.size() == 1);
  for (int i = 0; i < s.size(); i++) {
    for (int k = 0; k < 4; k++) {
      int nx = s[i].first + cx[k];
      int ny = s[i].second + cy[k];
      if (ok(nx, ny)) {
        if (nx == 0 || nx == h - 1 || ny == 0 || ny == w - 1)
          cout << "INF", exit(0);
        s.push_back({nx, ny});
        ans += Sq(nx, ny);
        F[nx][ny] = 2;
      }
    }
  }
  demon();
  cout << ans;
}