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

#include <bits/stdc++.h>

template <class T> inline void read(T &x) {
  static char ch;
  static bool opt;
  while (!isdigit(ch = getchar()) && ch != '-')
    ;
  x = (opt = ch == '-') ? 0 : ch - '0';
  while (isdigit(ch = getchar()))
    x = x * 10 + ch - '0';
  if (opt)
    x = ~x + 1;
}

template <class T> inline void putint(T x) {
  static char buf[45], *tail = buf;
  if (!x)
    putchar('0');
  else {
    if (x < 0)
      putchar('-'), x = ~x + 1;
    for (; x; x /= 10)
      *++tail = x % 10 + '0';
    for (; tail != buf; --tail)
      putchar(*tail);
  }
}

template <class T> inline bool tense(T &x, const T &y) {
  return y < x ? x = y, true : false;
}

template <class T> inline bool relax(T &x, const T &y) {
  return x < y ? x = y, true : false;
}

using std::cin;
using std::cout;
using std::max;
using std::min;
using std::pair;
using std::vector;
typedef long long s64;
typedef long double ld;
typedef std::pair<int, int> pii;
#define mp(x, y) std::make_pair(x, y)

const int MaxN = 5e2 + 5;
const int MaxS = MaxN * MaxN;

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

int n, s;
int dis[MaxN][MaxN];
bool opt[MaxN][MaxN];

inline void bfs_init() {
  static int que[MaxS][2];
  int qr = 0;

  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      if (dis[i][j]) {
        ++qr;
        que[qr][0] = i;
        que[qr][1] = j;
      }

  for (int i = 1; i <= qr; ++i) {
    int x = que[i][0];
    int y = que[i][1];

    for (int dir = 0; dir < 4; ++dir) {
      int nx = x + dx[dir];
      int ny = y + dy[dir];

      if (nx < 1 || ny < 1 || nx > n || ny > n)
        continue;

      if (!dis[nx][ny]) {
        dis[nx][ny] = dis[x][y] + 1;

        ++qr;
        que[qr][0] = nx;
        que[qr][1] = ny;
      }
    }
  }
}

inline void bfs(int x, int y) {
  static int que[MaxS][2];
  int qr = 1;

  que[1][0] = x;
  que[1][1] = y;

  for (int i = 1; i <= qr; ++i) {
    int x = que[i][0];
    int y = que[i][1];

    for (int dir = 0; dir < 4; ++dir) {
      int nx = x + dx[dir];
      int ny = y + dy[dir];

      if (nx < 1 || ny < 1 || nx > n || ny > n)
        continue;

      if (tense(dis[nx][ny], dis[x][y] + opt[nx][ny])) {
        ++qr;
        que[qr][0] = nx;
        que[qr][1] = ny;
      }
    }
  }
}

int main() {
  read(n);
  s = n * n;

  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      opt[i][j] = true;
  for (int i = 1; i <= n; ++i)
    dis[1][i] = dis[i][1] = dis[n][i] = dis[i][n] = 1;
  bfs_init();

  int res = 0;
  for (int i = 1; i <= s; ++i) {
    int p;
    read(p);

    int x = (p - 1) / n + 1, y = p - (x - 1) * n;

    // std::cerr << x << ' ' << y << ':' << dis[x][y] << '\\n';
    res += --dis[x][y], opt[x][y] = false;

    bfs(x, y);
  }

  std::cout << res << '\\n';

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

template <class T> inline void read(T &x) {
  static char ch;
  static bool opt;
  while (!isdigit(ch = getchar()) && ch != '-')
    ;
  x = (opt = ch == '-') ? 0 : ch - '0';
  while (isdigit(ch = getchar()))
    x = x * 10 + ch - '0';
  if (opt)
    x = ~x + 1;
}

template <class T> inline void putint(T x) {
  static char buf[45], *tail = buf;
  if (!x)
    putchar('0');
  else {
    if (x < 0)
      putchar('-'), x = ~x + 1;
    for (; x; x /= 10)
      *++tail = x % 10 + '0';
    for (; tail != buf; --tail)
      putchar(*tail);
  }
}

template <class T> inline bool tense(T &x, const T &y) {
  return y < x ? x = y, true : false;
}

template <class T> inline bool relax(T &x, const T &y) {
  return x < y ? x = y, true : false;
}

using std::cin;
using std::cout;
using std::max;
using std::min;
using std::pair;
using std::vector;
typedef long long s64;
typedef long double ld;
typedef std::pair<int, int> pii;
#define mp(x, y) std::make_pair(x, y)

const int MaxN = 5e2 + 5;
const int MaxS = MaxN * MaxN;

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

int n, s;
int dis[MaxN][MaxN];
bool opt[MaxN][MaxN];

inline void bfs_init() {
  static int que[MaxN][2];
  int qr = 0;

  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      if (dis[i][j]) {
        ++qr;
        que[qr][0] = i;
        que[qr][1] = j;
      }

  for (int i = 1; i <= qr; ++i) {
    int x = que[i][0];
    int y = que[i][1];

    for (int dir = 0; dir < 4; ++dir) {
      int nx = x + dx[dir];
      int ny = y + dy[dir];

      if (nx < 1 || ny < 1 || nx > n || ny > n)
        continue;

      if (!dis[nx][ny]) {
        dis[nx][ny] = dis[x][y] + 1;

        ++qr;
        que[qr][0] = nx;
        que[qr][1] = ny;
      }
    }
  }
}

inline void bfs(int x, int y) {
  static int que[MaxN][2];
  int qr = 1;

  que[1][0] = x;
  que[1][1] = y;

  for (int i = 1; i <= qr; ++i) {
    int x = que[i][0];
    int y = que[i][1];

    for (int dir = 0; dir < 4; ++dir) {
      int nx = x + dx[dir];
      int ny = y + dy[dir];

      if (nx < 1 || ny < 1 || nx > n || ny > n)
        continue;

      if (tense(dis[nx][ny], dis[x][y] + opt[nx][ny])) {
        ++qr;
        que[qr][0] = nx;
        que[qr][1] = ny;
      }
    }
  }
}

int main() {
  read(n);
  s = n * n;

  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      opt[i][j] = true;
  for (int i = 1; i <= n; ++i)
    dis[1][i] = dis[i][1] = dis[n][i] = dis[i][n] = 1;
  bfs_init();

  int res = 0;
  for (int i = 1; i <= s; ++i) {
    int p;
    read(p);

    int x = (p - 1) / n + 1, y = p - (x - 1) * n;

    // std::cerr << x << ' ' << y << ':' << dis[x][y] << '\\n';
    res += --dis[x][y], opt[x][y] = false;

    bfs(x, y);
  }

  std::cout << res << '\\n';

  return 0;
}