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

#include <bits/stdc++.h>
#define DEBUG if (0)
#define lli long long int
#define ull unsigned long long int
#define ldouble long double
using namespace std;

/*
  compress the coordinates
  floodfill
*/

const int maxN = 1e3 + 10;
int n, m, limitx, limity;
lli horizontal[3][maxN], vertical[3][maxN];

map<int, lli> revx, revy;

int mat[6 * maxN][6 * maxN];

int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
lli ans = 0;
bool bfs(int sx, int sy) {
  queue<pair<int, int>> q;
  q.push({sx, sy});
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    if (x < 0 || x >= limitx || y < 0 || y >= limity)
      return true;
    if (mat[x][y] != 0)
      continue;
    mat[x][y] = 2;
    if (x & y & 1LL) {
      ans += (revx[x + 1] - revx[x - 1]) * (revy[y + 1] - revy[y - 1]);
      // assert(revx[x + 1] - revx[x - 1] >= 0);
      // assert(revy[y + 1] - revy[y - 1] >= 0);
    }
    for (int k = 0; k < 4; k++) {
      int nx = x + dx[k], ny = y + dy[k];
      if (nx < 0 || nx >= limitx || ny < 0 || ny >= limity)
        return true;
      if (mat[nx][ny] == 0)
        q.push({x + dx[k], y + dy[k]});
    }
  }
  return false;
}

void removeRepeated(vector<lli> &arr) {
  sort(arr.begin(), arr.end());
  arr.resize(distance(arr.begin(), unique(arr.begin(), arr.end())));
}

int main() {
  int t = 0;
  while (scanf("%d %d", &n, &m) != EOF) {
    if (t++)
      memset(mat, 0, sizeof(mat));

    vector<lli> xs, ys;
    xs.push_back(0), ys.push_back(0);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 3; j++)
        scanf("%lld", &horizontal[j][i]);
      xs.push_back(horizontal[0][i]), xs.push_back(horizontal[1][i]);
      ys.push_back(horizontal[2][i]);
      assert(horizontal[2][i] >= -1e9 && horizontal[2][i] <= 1e9);
      assert(horizontal[0][i] >= -1e9 && horizontal[0][i] < horizontal[1][i]);
      assert(horizontal[1][i] <= 1e9);
    }

    for (int i = 0; i < m; i++) {
      for (int j = 0; j < 3; j++)
        scanf("%lld", &vertical[j][i]);
      xs.push_back(vertical[0][i]);
      ys.push_back(vertical[1][i]), ys.push_back(vertical[2][i]);
      assert(vertical[0][i] >= -1e9 && vertical[0][i] <= 1e9);
      assert(vertical[1][i] >= -1e9 && vertical[1][i] < vertical[2][i]);
      assert(vertical[2][i] <= 1e9);
    }

    removeRepeated(xs), removeRepeated(ys);
    map<lli, int> compx, compy;
    revx.clear(), revy.clear();
    for (int i = 0; i < xs.size(); i++)
      compx[xs[i]] = 2 * i, revx[2 * i] = xs[i];
    for (int i = 0; i < ys.size(); i++)
      compy[ys[i]] = 2 * i, revy[2 * i] = ys[i];

    for (int i = 0; i < n; i++) {
      horizontal[0][i] = compx[horizontal[0][i]],
      horizontal[1][i] = compx[horizontal[1][i]];
      horizontal[2][i] = compy[horizontal[2][i]];
    }

    for (int i = 0; i < m; i++) {
      vertical[0][i] = compx[vertical[0][i]];
      vertical[1][i] = compy[vertical[1][i]],
      vertical[2][i] = compy[vertical[2][i]];
    }

    limitx = 2 * xs.size() + 1, limity = 2 * ys.size() + 1;
    assert(limitx <= 6 * maxN);
    assert(limity <= 6 * maxN);

    for (int i = 0; i < n; i++)
      for (int j = horizontal[0][i]; j <= horizontal[1][i]; j++)
        mat[j][horizontal[2][i]] = 1;
    for (int i = 0; i < m; i++)
      for (int j = vertical[1][i]; j <= vertical[2][i]; j++)
        mat[vertical[0][i]][j] = 1;

    int zerox = compx[0], zeroy = compy[0];
    ans = 0;
    bool beyond = bfs(zerox, zeroy);
    DEBUG
    for (int y = 0; y < limity; y++) {
      for (int x = 0; x < limitx; x++)
        if (x == zerox && y == zeroy)
          printf("x");
        else
          printf("%c", mat[x][y] == 1   ? '@'
                       : mat[x][y] == 2 ? ((x & 1LL & y) ? '~' : '.')
                                        : ' ');
      printf("\\n");
    }

    if (beyond)
      printf("INF\\n");
    else {
      assert(ans >= 0);
      printf("%lld\\n", ans);
    }
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define DEBUG if (0)
#define lli long long int
#define ull unsigned long long int
#define ldouble long double
using namespace std;

/*
  compress the coordinates
  floodfill
*/

const int maxN = 1e3 + 10;
int n, m, limitx, limity;
lli horizontal[3][maxN], vertical[3][maxN];

map<int, lli> revx, revy;

int mat[6 * maxN][6 * maxN];

int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
lli ans = 0;
bool bfs(int sx, int sy) {
  queue<pair<int, int>> q;
  q.push({sx, sy});
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    if (x < 0 || x >= limitx || y < 0 || y >= limity)
      return true;
    if (mat[x][y] != 0)
      continue;
    mat[x][y] = 2;
    if (x & y & 1LL) {
      ans += (revx[x + 1] - revx[x - 1]) * (revy[y + 1] - revy[y - 1]);
      assert(revx[x + 1] - revx[x - 1] >= 0);
      assert(revy[y + 1] - revy[y - 1] >= 0);
    }
    for (int k = 0; k < 4; k++) {
      int nx = x + dx[k], ny = y + dy[k];
      if (nx < 0 || nx >= limitx || ny < 0 || ny >= limity)
        return true;
      if (mat[nx][ny] == 0)
        q.push({x + dx[k], y + dy[k]});
    }
  }
  return false;
}

void removeRepeated(vector<lli> &arr) {
  sort(arr.begin(), arr.end());
  arr.resize(distance(arr.begin(), unique(arr.begin(), arr.end())));
}

int main() {
  int t = 0;
  while (scanf("%d %d", &n, &m) != EOF) {
    if (t++)
      memset(mat, 0, sizeof(mat));

    vector<lli> xs, ys;
    xs.push_back(0), ys.push_back(0);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 3; j++)
        scanf("%lld", &horizontal[j][i]);
      xs.push_back(horizontal[0][i]), xs.push_back(horizontal[1][i]);
      ys.push_back(horizontal[2][i]);
      assert(horizontal[2][i] >= -1e9 && horizontal[2][i] <= 1e9);
      assert(horizontal[0][i] >= -1e9 && horizontal[0][i] < horizontal[1][i]);
      assert(horizontal[1][i] <= 1e9);
    }

    for (int i = 0; i < m; i++) {
      for (int j = 0; j < 3; j++)
        scanf("%lld", &vertical[j][i]);
      xs.push_back(vertical[0][i]);
      ys.push_back(vertical[1][i]), ys.push_back(vertical[2][i]);
      assert(vertical[0][i] >= -1e9 && vertical[0][i] <= 1e9);
      assert(vertical[1][i] >= -1e9 && vertical[1][i] < vertical[2][i]);
      assert(vertical[2][i] <= 1e9);
    }

    removeRepeated(xs), removeRepeated(ys);
    map<lli, int> compx, compy;
    revx.clear(), revy.clear();
    for (int i = 0; i < xs.size(); i++)
      compx[xs[i]] = 2 * i, revx[2 * i] = xs[i];
    for (int i = 0; i < ys.size(); i++)
      compy[ys[i]] = 2 * i, revy[2 * i] = ys[i];

    for (int i = 0; i < n; i++) {
      horizontal[0][i] = compx[horizontal[0][i]],
      horizontal[1][i] = compx[horizontal[1][i]];
      horizontal[2][i] = compy[horizontal[2][i]];
    }

    for (int i = 0; i < m; i++) {
      vertical[0][i] = compx[vertical[0][i]];
      vertical[1][i] = compy[vertical[1][i]],
      vertical[2][i] = compy[vertical[2][i]];
    }

    limitx = 2 * xs.size() + 1, limity = 2 * ys.size() + 1;
    assert(limitx <= 6 * maxN);
    assert(limity <= 6 * maxN);

    for (int i = 0; i < n; i++)
      for (int j = horizontal[0][i]; j <= horizontal[1][i]; j++)
        mat[j][horizontal[2][i]] = 1;
    for (int i = 0; i < m; i++)
      for (int j = vertical[1][i]; j <= vertical[2][i]; j++)
        mat[vertical[0][i]][j] = 1;

    int zerox = compx[0], zeroy = compy[0];
    ans = 0;
    bool beyond = bfs(zerox, zeroy);
    DEBUG
    for (int y = 0; y < limity; y++) {
      for (int x = 0; x < limitx; x++)
        if (x == zerox && y == zeroy)
          printf("x");
        else
          printf("%c", mat[x][y] == 1   ? '@'
                       : mat[x][y] == 2 ? ((x & 1LL & y) ? '~' : '.')
                                        : ' ');
      printf("\\n");
    }

    if (beyond)
      printf("INF\\n");
    else {
      assert(ans >= 0);
      printf("%lld\\n", ans);
    }
  }
  return 0;
}