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

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1005;
const int M = 7005;
int a[N], b[N], c[N], d[N], e[N], f[N];
vector<int> vx, vy;
int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0};
bool ma[M][M], vis[M][M];
int get_x(int x) { return lower_bound(vx.begin(), vx.end(), x) - vx.begin(); }
int get_y(int x) { return lower_bound(vy.begin(), vy.end(), x) - vy.begin(); }
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  vx.push_back(-1.1e9), vx.push_back(0);
  vy.push_back(-1.1e9), vy.push_back(0);
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d", &a[i], &b[i], &c[i]);
    vx.push_back(a[i]), vx.push_back(b[i]);
    vy.push_back(c[i]);
  }
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &d[i], &e[i], &f[i]);
    vy.push_back(e[i]), vy.push_back(f[i]);
    vx.push_back(d[i]);
  }
  sort(vx.begin(), vx.end()), vx.erase(unique(vx.begin(), vx.end()), vx.end());
  sort(vy.begin(), vy.end()), vy.erase(unique(vy.begin(), vy.end()), vy.end());
  for (int i = 0; i < n; i++) {
    a[i] = get_x(a[i]), b[i] = get_x(b[i]), c[i] = get_y(c[i]);
    for (int j = a[i] * 2 - 1; j < 2 * b[i]; j++) {
      ma[j][2 * c[i] - 1] = true;
    }
  }
  for (int i = 0; i < m; i++) {
    d[i] = get_x(d[i]), e[i] = get_y(e[i]), f[i] = get_y(f[i]);
    for (int j = 2 * e[i] - 1; j < 2 * f[i]; j++) {
      ma[2 * d[i] - 1][j] = true;
    }
  }
  int sx = get_x(0) * 2 - 1, sy = get_y(0) * 2 - 1;
  bool flag = false;
  ll ans = 0;
  queue<pair<int, int>> qu;
  qu.push({sx, sy}), vis[sx][sy] = true;
  while (!qu.empty()) {
    int ux = qu.front().first, uy = qu.front().second;
    qu.pop();
    if (ux == M - 1 || uy == M - 1 || ux == 0 || uy == 0) {
      flag = true;
      break;
    }
    if (ux % 2 == 0 && uy % 2 == 0) {
      int x = ux / 2, y = uy / 2;
      ans += 1LL * (vx[x + 1] - vx[x]) * (vy[y + 1] - vy[y]);
    }
    for (int i = 0; i < 4; i++) {
      int xx = ux + dx[i], yy = uy + dy[i];
      if (xx >= M || xx < 0 || yy >= M || yy < 0 || vis[xx][yy] || ma[xx][yy])
        continue;
      qu.push({xx, yy}), vis[xx][yy] = true;
    }
  }
  if (flag)
    printf("INF\\n");
  else
    printf("%lld\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
const int maxm = maxn << 2;
typedef long long ll;

vector<int> vx, vy;
int dir[4][2] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}};

int a[maxn], b[maxn], c[maxn], d[maxn], e[maxn], f[maxn];
int vis[maxm][maxm];

int get_x(int val) {
  return lower_bound(vx.begin(), vx.end(), val) - vx.begin();
}

int get_y(int val) {
  return lower_bound(vy.begin(), vy.end(), val) - vy.begin();
}

int main() {
  int n, m;
  cin >> n >> m;
  vx.push_back(0), vy.push_back(0);           // add start vertex
  vx.push_back(-1.2e9), vy.push_back(-1.2e9); // add boundary data
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    vx.push_back(a[i]), vx.push_back(b[i]);
    vy.push_back(c[i]);
  }
  for (int i = 0; i < m; i++) {
    cin >> d[i] >> e[i] >> f[i];
    vx.push_back(d[i]);
    vy.push_back(e[i]), vy.push_back(f[i]);
  }

  sort(vx.begin(), vx.end());
  vx.erase(unique(vx.begin(), vx.end()), vx.end());

  sort(vy.begin(), vy.end());
  vy.erase(unique(vy.begin(), vy.end()), vy.end());

  for (int i = 0; i < n; i++) {
    a[i] = get_x(a[i]), b[i] = get_x(b[i]), c[i] = get_y(c[i]);
    for (int j = a[i] * 2 - 1; j < 2 * b[i]; j++)
      vis[j][2 * c[i] - 1] = true;
  }

  for (int i = 0; i < m; i++) {
    d[i] = get_x(d[i]), e[i] = get_y(e[i]), f[i] = get_y(f[i]);
    for (int j = e[i] * 2 - 1; j < f[i] * 2; j++)
      vis[d[i] * 2 - 1][j] = true;
  }

  int sx = get_x(0) * 2 - 1, sy = get_y(0) * 2 - 1;

  queue<pair<int, int>> q;
  q.push({sx, sy}), vis[sx][sy] = true;
  ll ans = 0;
  bool flag = false;
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    if (x == maxm - 1 || y == maxm - 1 || x == 0 || y == 0) {
      flag = true;
      break;
    }
    if (x % 2 == 0 && y % 2 == 0) {
      int xx = x / 2, yy = y / 2;
      ans += 1ll * (vx[xx + 1] - vx[xx]) * (vy[yy + 1] - vy[yy]);
    }
    for (int i = 0; i < 4; i++) {
      int xx = x + dir[i][0], yy = y + dir[i][1];
      if (xx >= maxn || xx < 0 || yy >= maxn || yy < 0 || vis[xx][yy])
        continue;
      q.push({xx, yy});
      vis[xx][yy] = 1;
    }
  }
  if (flag)
    puts("INF");
  else
    cout << ans << endl;
  return 0;
}