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

#include <stdio.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define rep(i, N) for (int i = 0; i < (int)N; i++)
static inline char CH() {
  static char buf[100000], *ptr1 = buf, *ptr2 = buf;
  return ptr1 == ptr2 && (ptr2 = (ptr1 = buf) + fread(buf, 1, 100000, stdin),
                          ptr1 == ptr2)
             ? EOF
             : *ptr1++;
}
static inline int IN(void) {
  int x = 0, f = 0, c = CH();
  while (c < 48 || c > 57) {
    f ^= c == 45, c = CH();
  }
  while (c > 47 && c < 58) {
    x = x * 10 + c - 48, c = CH();
  }
  return f ? -x : x;
}
static inline void OUT(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    OUT(x / 10);
  putchar(x % 10 + 48);
}
static inline int min(const int a, const int b) { return a < b ? a : b; }
int main(void) {
  int N = IN(), a = 0, across, d, down, p, q, xy, que[251502], vacant[252525],
      dist[252525];
  rep(i, N) {
    xy = (N + 2) * (i + 1) + 1;
    across = min(i, N - 1 - i), down = 3;
    rep(j, across) { dist[xy++] = down++; }
    rep(j, N - 2 * across) { dist[xy++] = across + 3; }
    rep(j, across) { dist[xy++] = --down; }
  }
  rep(k, N * N) {
    xy = IN();
    xy += (xy - 1) / N * 2 + N + 2;
    a += dist[xy];
    p = 0, q = 0;
    que[p++] = xy;
    vacant[xy] = 1;
    while (p > q) {
      xy = que[q++], d = dist[xy] - vacant[xy] + 1;
      if (dist[xy - N - 2] > d) {
        dist[xy - N - 2] = d;
        que[p++] = xy - N - 2;
      }
      if (dist[xy + N + 2] > d) {
        dist[xy + N + 2] = d;
        que[p++] = xy + N + 2;
      }
      if (dist[xy - 1] > d) {
        dist[xy - 1] = d;
        que[p++] = xy - 1;
      }
      if (dist[xy + 1] > d) {
        dist[xy + 1] = d;
        que[p++] = xy + 1;
      }
    }
  }
  OUT(a - 3 * N * N);
  return puts(""), 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define rep(i, N) for (int i = 0; i < (int)N; i++)
static inline char CH() {
  static char buf[100000], *ptr1 = buf, *ptr2 = buf;
  return ptr1 == ptr2 && (ptr2 = (ptr1 = buf) + fread(buf, 1, 100000, stdin),
                          ptr1 == ptr2)
             ? EOF
             : *ptr1++;
}
static inline int IN(void) {
  int x = 0, f = 0, c = CH();
  while (c < 48 || c > 57) {
    f ^= c == 45, c = CH();
  }
  while (c > 47 && c < 58) {
    x = x * 10 + c - 48, c = CH();
  }
  return f ? -x : x;
}
static inline void OUT(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    OUT(x / 10);
  putchar(x % 10 + 48);
}
static inline int min(const int a, const int b) { return a < b ? a : b; }
int main(void) {
  int N = IN(), a = 0, across, d, down, p, q, xy, que[250000], vacant[252525],
      dist[252525];
  rep(i, N) {
    xy = (N + 2) * (i + 1) + 1;
    across = min(i, N - 1 - i), down = 3;
    rep(j, across) { dist[xy++] = down++; }
    rep(j, N - 2 * across) { dist[xy++] = across + 3; }
    rep(j, across) { dist[xy++] = --down; }
  }
  rep(k, N * N) {
    xy = IN();
    xy += (xy - 1) / N * 2 + N + 2;
    a += dist[xy];
    p = 0, q = 0;
    que[p++] = xy;
    vacant[xy] = 1;
    while (p > q) {
      xy = que[q++], d = dist[xy] - vacant[xy] + 1;
      if (dist[xy - N - 2] > d) {
        dist[xy - N - 2] = d;
        que[p++] = xy - N - 2;
      }
      if (dist[xy + N + 2] > d) {
        dist[xy + N + 2] = d;
        que[p++] = xy + N + 2;
      }
      if (dist[xy - 1] > d) {
        dist[xy - 1] = d;
        que[p++] = xy - 1;
      }
      if (dist[xy + 1] > d) {
        dist[xy + 1] = d;
        que[p++] = xy + 1;
      }
    }
  }
  OUT(a - 3 * N * N);
  return puts(""), 0;
}