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

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

typedef long long LL;
const int M = 1005;
const int INF = 1e9 + 5;

struct Node {
  int a, b, c;
  void read() { scanf("%d%d%d", &a, &b, &c); }
} r[M], c[M];

int x[M * 3], y[M * 3];
int nx, ny;
int findx(int v) { return lower_bound(x, x + nx, v) - x; }
int findy(int v) { return lower_bound(y, y + ny, v) - y; }

int vis[M * 3][M * 3];
int block[M * 3][M * 3][4];
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
//         L R U D

LL ans;

int qx[M * M * 9];
int qy[M * M * 9];

void bfs(int xx, int yy) {
  int t = 0;
  qx[t] = xx;
  qy[t] = yy;
  t++;
  vis[xx][yy] = 1;
  for (int ii = 0; ii < t; ii++) {
    xx = qx[ii];
    yy = qy[ii];
    ans += 1LL * (x[xx + 1] - x[xx]) * (y[yy + 1] - y[yy]);
    for (int i = 0; i < 4; i++) {
      if (block[xx][yy][i])
        continue;
      int x = xx + dx[i], y = yy + dy[i];
      if (x <= 0 || nx - 1 <= x || y <= 0 || ny - 1 <= y) {
        ans = -1;
        return;
      }
      if (vis[x][y])
        continue;
      vis[x][y] = 1;
      qx[t] = x;
      qy[t] = y;
      t++;
    }
  }
}

int main() {
  // freopen("F.in","r",stdin);
  int n, m;
  scanf("%d%d", &n, &m);
  nx = ny = 0;
  for (int i = 0; i < n; i++) {
    r[i].read();
    x[nx++] = r[i].a;
    x[nx++] = r[i].b;
    y[ny++] = r[i].c;
  }
  for (int i = 0; i < m; i++) {
    c[i].read();
    x[nx++] = c[i].a;
    y[ny++] = c[i].b;
    y[ny++] = c[i].c;
  }
  x[nx++] = -INF;
  x[nx++] = INF;
  y[ny++] = -INF;
  y[ny++] = INF;
  sort(x, x + nx);
  nx = unique(x, x + nx) - x;
  sort(y, y + ny);
  ny = unique(y, y + ny) - y;
  // cout<<nx<<" "<<ny<<endl;
  // for(int i=0;i<nx;i++)
  // 	cout<<x[i]<<" ";
  // cout<<endl;
  // for(int i=0;i<ny;i++)
  // 	cout<<y[i]<<" ";
  // cout<<endl;
  for (int i = 0; i < n; i++) {
    int p = findy(r[i].c);
    // cout<<p<<" "<<findx(r[i].a)<<" "<<findx(r[i].b)<<endl;
    for (int j = findx(r[i].a); j < findx(r[i].b); j++)
      block[j][p - 1][1] = block[j][p][0] = 1;
  }
  for (int i = 0; i < m; i++) {
    int p = findx(c[i].a);
    for (int j = findy(c[i].b); j < findy(c[i].c); j++)
      block[p - 1][j][3] = block[p][j][2] = 1;
  }
  bfs(findx(0) - 1, findy(0) - 1);
  if (ans == -1)
    printf("INF\\n");
  else
    printf("%lld\\n", ans);
  return 0;
}

Error: -11

Fixed submission:

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

typedef long long LL;
const int M = 1005;
const int INF = 0x3f3f3f3f;

struct Node {
  int a, b, c;
  void read() { scanf("%d%d%d", &a, &b, &c); }
} r[M], c[M];

int x[M * 3], y[M * 3];
int nx, ny;
int findx(int v) { return lower_bound(x, x + nx, v) - x; }
int findy(int v) { return lower_bound(y, y + ny, v) - y; }

int vis[M * 3][M * 3];
int block[M * 3][M * 3][4];
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
//         L R U D

LL ans;

void dfs(int xx, int yy) {
  // cout<<xx<<" "<<yy<<endl;
  if (ans == -1)
    return;
  vis[xx][yy] = 1;
  ans += 1LL * (x[xx + 1] - x[xx]) * (y[yy + 1] - y[yy]);
  // cout<<1LL*x[xx]*y[yy]<<endl;
  for (int i = 0; i < 4; i++) {
    if (block[xx][yy][i])
      continue;
    int x = xx + dx[i], y = yy + dy[i];
    if (x <= 0 || nx <= x || y <= 0 || ny <= y) {
      ans = -1;
      return;
    }
    if (vis[x][y])
      continue;
    dfs(x, y);
  }
}

int main() {
  // freopen("F.in","r",stdin);
  int n, m;
  scanf("%d%d", &n, &m);
  nx = ny = 0;
  for (int i = 0; i < n; i++) {
    r[i].read();
    x[nx++] = r[i].a;
    x[nx++] = r[i].b;
    y[ny++] = r[i].c;
  }
  for (int i = 0; i < m; i++) {
    c[i].read();
    x[nx++] = c[i].a;
    y[ny++] = c[i].b;
    y[ny++] = c[i].c;
  }
  x[nx++] = -INF;
  x[nx++] = INF;
  y[ny++] = -INF;
  y[ny++] = INF;
  sort(x, x + nx);
  nx = unique(x, x + nx) - x;
  sort(y, y + ny);
  ny = unique(y, y + ny) - y;
  // cout<<nx<<" "<<ny<<endl;
  // for(int i=0;i<nx;i++)
  // 	cout<<x[i]<<" ";
  // cout<<endl;
  // for(int i=0;i<ny;i++)
  // 	cout<<y[i]<<" ";
  // cout<<endl;
  for (int i = 0; i < n; i++) {
    int p = findy(r[i].c);
    // cout<<p<<" "<<findx(r[i].a)<<" "<<findx(r[i].b)<<endl;
    for (int j = findx(r[i].a); j < findx(r[i].b); j++)
      block[j][p - 1][1] = block[j][p][0] = 1;
  }
  for (int i = 0; i < m; i++) {
    int p = findx(c[i].a);
    for (int j = findy(c[i].b); j < findy(c[i].c); j++)
      block[p - 1][j][3] = block[p][j][2] = 1;
  }
  ans = 0;
  dfs(findx(0), findy(0));
  if (ans == -1)
    printf("INF\\n");
  else
    printf("%lld\\n", ans);
  return 0;
}