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

#include <bits/stdc++.h>
using namespace std;

const int N = 6005;

int n, m;
int a[N], b[N], c[N];
int d[N], e[N], f[N];

bool seg[N][N];

bool valid(int x, int y) { return x >= 0 && x < N && y >= 0 && y < N; }

int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};

bool inf;
bool vis[N][N];
void bfs(int x, int y) {
  queue<pair<int, int>> q;
  q.push(make_pair(x, y));
  vis[x][y] = true;

  while (q.size()) {
    int x, y;
    tie(x, y) = q.front();
    q.pop();

    for (int k = 0; k < 4; ++k) {
      int nx = x + dx[k], ny = y + dy[k];
      if (!valid(nx, ny)) {
        inf = true;
        continue;
      }
      if (seg[nx][ny])
        continue;
      if (vis[nx][ny])
        continue;
      vis[nx][ny] = true;
      q.push(make_pair(nx, ny));
    }
  }
}

int solve() {
  scanf("%d %d", &n, &m);

  vector<int> xs = {0}, ys = {0};

  for (int i = 0; i < n; ++i) {
    scanf("%d %d %d", &a[i], &b[i], &c[i]);
    xs.push_back(a[i]);
    xs.push_back(b[i]);
    ys.push_back(c[i]);
  }

  for (int i = 0; i < m; ++i) {
    scanf("%d %d %d", &d[i], &e[i], &f[i]);
    xs.push_back(d[i]);
    ys.push_back(e[i]);
    ys.push_back(f[i]);
  }

  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) {
    a[i] = lower_bound(xs.begin(), xs.end(), a[i]) - xs.begin();
    b[i] = lower_bound(xs.begin(), xs.end(), b[i]) - xs.begin();
    c[i] = lower_bound(ys.begin(), ys.end(), c[i]) - ys.begin();

    for (int k = a[i] * 2; k <= b[i] * 2; ++k) {
      seg[k][c[i] * 2] = true;
    }
  }

  for (int i = 0; i < m; ++i) {
    d[i] = lower_bound(xs.begin(), xs.end(), d[i]) - xs.begin();
    e[i] = lower_bound(ys.begin(), ys.end(), e[i]) - ys.begin();
    f[i] = lower_bound(ys.begin(), ys.end(), f[i]) - ys.begin();

    for (int k = e[i] * 2; k <= f[i] * 2; ++k) {
      seg[d[i] * 2][k] = true;
    }
  }

  bfs((lower_bound(xs.begin(), xs.end(), 0) - xs.begin()) * 2,
      (lower_bound(ys.begin(), ys.end(), 0) - ys.begin()) * 2);

  if (false)
    for (int i = 0; i <= xs.size() * 2; ++i) {
      for (int j = 0; j <= ys.size() * 2; ++j) {
        if (seg[i][j])
          printf("#");
        else if (vis[i][j])
          printf(",");
        else
          printf(".");
      }
      printf("\\n");
    }

  if (inf)
    return 0 * printf("INF\\n");

  long long ans = 0;
  for (int i = 1; i < N; i += 2) {
    for (int j = 1; j < N; j += 2) {
      if (!vis[i][j])
        continue;
      ans += 1LL * (xs[(i + 1) / 2] - xs[(i - 1) / 2]) *
             (ys[(j + 1) / 2] - ys[(j - 1) / 2]);
    }
  }

  printf("%lld\\n", ans);
  return 0;
}

int main() {
  int t = 1;
  // scanf("%d", &t);
  for (int tc = 0; tc < t; ++tc) {
    // printf("Case #%d: ", tc+1);
    solve();
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const int N = 3005;

int n, m;
int a[N], b[N], c[N];
int d[N], e[N], f[N];

bool seg[N][N];

bool valid(int x, int y) { return x >= 0 && x < N && y >= 0 && y < N; }

int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};

bool inf;
bool vis[N][N];
void bfs(int x, int y) {
  queue<pair<int, int>> q;
  q.push(make_pair(x, y));
  vis[x][y] = true;

  while (q.size()) {
    int x, y;
    tie(x, y) = q.front();
    q.pop();

    for (int k = 0; k < 4; ++k) {
      int nx = x + dx[k], ny = y + dy[k];
      if (!valid(nx, ny)) {
        inf = true;
        continue;
      }
      if (seg[nx][ny])
        continue;
      if (vis[nx][ny])
        continue;
      vis[nx][ny] = true;
      q.push(make_pair(nx, ny));
    }
  }
}

int solve() {
  scanf("%d %d", &n, &m);

  vector<int> xs = {0}, ys = {0};

  for (int i = 0; i < n; ++i) {
    scanf("%d %d %d", &a[i], &b[i], &c[i]);
    xs.push_back(a[i]);
    xs.push_back(b[i]);
    ys.push_back(c[i]);
  }

  for (int i = 0; i < m; ++i) {
    scanf("%d %d %d", &d[i], &e[i], &f[i]);
    xs.push_back(d[i]);
    ys.push_back(e[i]);
    ys.push_back(f[i]);
  }

  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) {
    a[i] = lower_bound(xs.begin(), xs.end(), a[i]) - xs.begin();
    b[i] = lower_bound(xs.begin(), xs.end(), b[i]) - xs.begin();
    c[i] = lower_bound(ys.begin(), ys.end(), c[i]) - ys.begin();

    for (int k = a[i] * 2; k <= b[i] * 2; ++k) {
      seg[k][c[i] * 2] = true;
    }
  }

  for (int i = 0; i < m; ++i) {
    d[i] = lower_bound(xs.begin(), xs.end(), d[i]) - xs.begin();
    e[i] = lower_bound(ys.begin(), ys.end(), e[i]) - ys.begin();
    f[i] = lower_bound(ys.begin(), ys.end(), f[i]) - ys.begin();

    for (int k = e[i] * 2; k <= f[i] * 2; ++k) {
      seg[d[i] * 2][k] = true;
    }
  }

  bfs((lower_bound(xs.begin(), xs.end(), 0) - xs.begin()) * 2,
      (lower_bound(ys.begin(), ys.end(), 0) - ys.begin()) * 2);

  if (false)
    for (int i = 0; i <= xs.size() * 2; ++i) {
      for (int j = 0; j <= ys.size() * 2; ++j) {
        if (seg[i][j])
          printf("#");
        else if (vis[i][j])
          printf(",");
        else
          printf(".");
      }
      printf("\\n");
    }

  if (inf)
    return 0 * printf("INF\\n");

  long long ans = 0;
  for (int i = 1; i < N; i += 2) {
    for (int j = 1; j < N; j += 2) {
      if (!vis[i][j])
        continue;
      ans += 1LL * (xs[(i + 1) / 2] - xs[(i - 1) / 2]) *
             (ys[(i + 1) / 2] - ys[(i - 1) / 2]);
    }
  }

  printf("%lld\\n", ans);
  return 0;
}

int main() {
  int t = 1;
  // scanf("%d", &t);
  for (int tc = 0; tc < t; ++tc) {
    // printf("Case #%d: ", tc+1);
    solve();
  }
  return 0;
}