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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <stack>
#include <utility>

struct point {
  int x, y;
  int id;
};

bool order_x(const point &a, const point &b) { return a.x < b.x; }
bool order_y(const point &a, const point &b) { return a.y < b.y; }

const int N = 1000;
int n, m;
point p[N * 4 + 1];
int x_next[N * 3 + 1];
int y_next[N * 3 + 1];
int x_size, y_size;
int pr[N * 2];
bool g[N * 3 + 1][N * 3 + 1][4];
bool u[N * 3 + 1][N * 3 + 1];

long long dfs(int x, int y) {
  std::stack<std::pair<int, int>> st;
  st.push(std::make_pair(x, y));
  u[x][y] = true;
  long long res = 0;
  while (!st.empty()) {
    x = st.top().first;
    y = st.top().second;
    res += (long long)x_next[x] * y_next[y];
    st.pop();
    if (g[x][y][0]) {
      if (x == x_size - 1)
        return -1;
      if (!u[x + 1][y]) {
        u[x + 1][y] = true;
        st.push(std::make_pair(x + 1, y));
      }
    }
    if (g[x][y][1]) {
      if (y == y_size - 1)
        return -1;
      if (!u[x][y + 1]) {
        u[x][y + 1] = true;
        st.push(std::make_pair(x, y + 1));
      }
    }
    if (g[x][y][2]) {
      if (!x)
        return -1;
      if (!u[x - 1][y]) {
        u[x - 1][y] = true;
        st.push(std::make_pair(x - 1, y));
      }
    }
    if (g[x][y][3]) {
      if (!y)
        return -1;
      if (!u[x][y - 1]) {
        u[x][y - 1] = true;
        st.push(std::make_pair(x, y - 1));
      }
    }
  }
  return res;
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    p[i * 2].id = p[i * 2 + 1].id = i;
    p[i * 2].x = a;
    p[i * 2 + 1].x = b;
    p[i * 2].y = p[i * 2 + 1].y = c;
  }
  for (int i = n; i < n + m; ++i) {
    int d, e, f;
    scanf("%d%d%d", &d, &e, &f);
    p[i * 2].id = p[i * 2 + 1].id = i;
    p[i * 2].x = p[i * 2 + 1].x = d;
    p[i * 2].y = e;
    p[i * 2 + 1].y = f;
  }
  int p_amount = (n + m) * 2 + 1;
  p[p_amount - 1].id = -1;
  p[p_amount - 1].x = p[p_amount - 1].y = 0;
  std::sort(p, p + p_amount, order_x);
  int i, j;
  for (i = 0, j = 0; i < p_amount; ++j) {
    int val = p[i].x;
    do {
      p[i].x = j;
      ++i;
    } while (i < p_amount && p[i].x == val);
    if (i < p_amount)
      x_next[j] = p[i].x - val;
  }
  x_size = j;
  std::sort(p, p + p_amount, order_y);
  for (i = 0, j = 0; i < p_amount; ++j) {
    int val = p[i].y;
    do {
      p[i].y = j;
      ++i;
    } while (i < p_amount && p[i].y == val);
    if (i < p_amount)
      y_next[j] = p[i].y - val;
  }
  y_size = j;
  for (int i = 0; i < x_size; ++i)
    for (int j = 0; j < y_size; ++j)
      for (int k = 0; k < 4; ++k)
        g[i][j][k] = true;
  memset(pr, -1, (n + m) * sizeof(int));
  point z;
  for (int i = 0; i < p_amount; ++i) {
    if (p[i].id == -1)
      z = p[i];
    else if (pr[p[i].id] == -1)
      pr[p[i].id] = i;
    else {
      point s = p[pr[p[i].id]];
      point t = p[i];
      if (s.id < n) {
        if (s.x > t.x)
          std::swap(s, t);
        for (int i = s.x; i < t.x; ++i) {
          g[i][s.y][3] = false;
          if (s.y > 0)
            g[i][s.y - 1][1] = false;
        }
      } else {
        if (s.y > t.y)
          std::swap(s, t);
        for (int i = s.y; i < t.y; ++i) {
          g[s.x][i][2] = false;
          if (s.x > 0)
            g[s.x - 1][i][0] = false;
        }
      }
    }
  }
  long long r = dfs(z.x, z.y);
  if (r == -1)
    printf("INF\\n");
  else
    printf("%lld\\n", r);
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <utility>

struct point {
  int x, y;
  int id;
};

bool order_x(const point &a, const point &b) { return a.x < b.x; }
bool order_y(const point &a, const point &b) { return a.y < b.y; }

const int N = 1000;
int n, m;
point p[N * 4 + 1];
int x_next[N * 3 + 1];
int y_next[N * 3 + 1];
int x_size, y_size;
int pr[N * 2];
bool g[N * 3 + 1][N * 3 + 1][4];
bool u[N * 3 + 1][N * 3 + 1];
int step;

long long dfs(int x, int y) {
  ++step;
  if (step >= (N * 3 + 1) * (N * 3 + 1)) {
    printf("-1\\n");
    return 0;
  }
  if (x < 0 || x >= x_size || y < 0 || y >= y_size) {
    printf("-1\\n");
    return 0;
  }
  u[x][y] = true;
  long long res = (long long)x_next[x] * y_next[y];
  if (g[x][y][0]) {
    if (x == x_size - 1)
      return -1;
    if (!u[x + 1][y]) {
      long long ll = dfs(x + 1, y);
      if (ll == -1)
        return -1;
      res += ll;
    }
  }
  if (g[x][y][1]) {
    if (y == y_size - 1)
      return -1;
    if (!u[x][y + 1]) {
      long long ll = dfs(x, y + 1);
      if (ll == -1)
        return -1;
      res += ll;
    }
  }
  if (g[x][y][2]) {
    if (!x)
      return -1;
    if (!u[x - 1][y]) {
      long long ll = dfs(x - 1, y);
      if (ll == -1)
        return -1;
      res += ll;
    }
  }
  if (g[x][y][3]) {
    if (!y)
      return -1;
    if (!u[x][y - 1]) {
      long long ll = dfs(x, y - 1);
      if (ll == -1)
        return -1;
      res += ll;
    }
  }
  return res;
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    p[i * 2].id = p[i * 2 + 1].id = i;
    p[i * 2].x = a;
    p[i * 2 + 1].x = b;
    p[i * 2].y = p[i * 2 + 1].y = c;
  }
  for (int i = n; i < n + m; ++i) {
    int d, e, f;
    scanf("%d%d%d", &d, &e, &f);
    p[i * 2].id = p[i * 2 + 1].id = i;
    p[i * 2].x = p[i * 2 + 1].x = d;
    p[i * 2].y = e;
    p[i * 2 + 1].y = f;
  }
  int p_amount = (n + m) * 2 + 1;
  p[p_amount - 1].id = -1;
  p[p_amount - 1].x = p[p_amount - 1].y = 0;
  std::sort(p, p + p_amount, order_x);
  int i, j;
  for (i = 0, j = 0; i < p_amount; ++j) {
    int val = p[i].x;
    do {
      p[i].x = j;
      ++i;
    } while (i < p_amount && p[i].x == val);
    if (i < p_amount)
      x_next[j] = p[i].x - val;
  }
  x_size = j;
  std::sort(p, p + p_amount, order_y);
  for (i = 0, j = 0; i < p_amount; ++j) {
    int val = p[i].y;
    do {
      p[i].y = j;
      ++i;
    } while (i < p_amount && p[i].y == val);
    if (i < p_amount)
      y_next[j] = p[i].y - val;
  }
  y_size = j;
  for (int i = 0; i < x_size; ++i)
    for (int j = 0; j < y_size; ++j)
      for (int k = 0; k < 4; ++k)
        g[i][j][k] = true;
  memset(pr, -1, (n + m) * sizeof(int));
  point z;
  z.x = -1;
  for (int i = 0; i < p_amount; ++i) {
    if (p[i].id == -1)
      z = p[i];
    else if (pr[p[i].id] == -1) {
      if (p[i].id < 0 || p[i].id >= n + m) {
        printf("-1\\n");
        return 0;
      }
      pr[p[i].id] = i;
    } else {
      if (pr[p[i].id] < 0 || pr[p[i].id] >= p_amount) {
        printf("-1\\n");
        return 0;
      }
      point s = p[pr[p[i].id]];
      point t = p[i];
      if (s.id < n) {
        if (s.x > t.x)
          std::swap(s, t);
        if (s.x < 0 || s.x >= x_size || s.y < 0 || s.y >= y_size) {
          printf("-1\\n");
          return 0;
        }
        for (int i = s.x; i < t.x; ++i) {
          g[i][s.y][3] = false;
          if (s.y > 0)
            g[i][s.y - 1][1] = false;
        }
      } else {
        if (s.y > t.y)
          std::swap(s, t);
        if (s.x < 0 || s.x >= x_size || s.y < 0 || s.y >= y_size) {
          printf("-1\\n");
          return 0;
        }
        for (int i = s.y; i < t.y; ++i) {
          g[s.x][i][2] = false;
          if (s.x > 0)
            g[s.x - 1][i][0] = false;
        }
      }
    }
  }
  if (z.x == -1) {
    printf("-1\\n");
    return 0;
  }
  long long r = dfs(z.x, z.y);
  if (r == -1)
    printf("INF\\n");
  else
    printf("%lld\\n", r);
  return 0;
}