Problem p02670 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define repdi(i, a, b) for (ll i = (a)-1; i >= (b); --i)
#define repd(i, a) repdi(i, a, 0)
#define itr(it, a) for (auto it = (a).begin(); it != (a).end(); ++it)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

using ll = long long;
using Pr = std::pair<ll, ll>;

constexpr ll INF = 1ll << 60;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class S, class T>
std::ostream &operator<<(std::ostream &out, const std::pair<S, T> &a) {
  std::cout << '(' << a.first << ", " << a.second << ')';
  return out;
}

template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &a) {
  std::cout << '[';
  rep(i, a.size()) {
    std::cout << a[i];
    if (i != a.size() - 1)
      std::cout << ", ";
  }
  std::cout << ']';
  return out;
}

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

ll N;
ll P[250010];
ll h[250010];
bool used[250010];

ll bfs(ll i, ll j) {
  ll ret = h[i * N + j];
  ll nh = h[i * N + j] + used[i * N + j];

  rep(d, 4) {
    ll ni = i + dy[d], nj = j + dx[d];

    if (nh < h[ni * N + nj]) {
      h[ni * N + nj] = nh;
      bfs(ni, nj);
    }
  }

  return ret;
}

int main() {
  std::cin >> N;

  rep(i, N * N) std::cin >> P[i], --P[i];

  rep(i, N * N) {
    ll r = i / N, c = i % N;

    h[i] = std::min({r, N - r - 1, c, N - c - 1});
    used[i] = true;
  }

  ll ans = 0;

  rep(i, N * N) {
    ll r = P[i] / N;
    ll c = P[i] % N;

    used[r * N + c] = false;
    ans += bfs(r, c);
  }

  std::cout << ans << std::endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define repdi(i, a, b) for (ll i = (a)-1; i >= (b); --i)
#define repd(i, a) repdi(i, a, 0)
#define itr(it, a) for (auto it = (a).begin(); it != (a).end(); ++it)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

using ll = long long;
using Pr = std::pair<ll, ll>;

constexpr ll INF = 1ll << 60;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class S, class T>
std::ostream &operator<<(std::ostream &out, const std::pair<S, T> &a) {
  std::cout << '(' << a.first << ", " << a.second << ')';
  return out;
}

template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &a) {
  std::cout << '[';
  rep(i, a.size()) {
    std::cout << a[i];
    if (i != a.size() - 1)
      std::cout << ", ";
  }
  std::cout << ']';
  return out;
}

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

ll N;
ll P[250010];
ll h[250010];
bool used[250010];

ll bfs(ll i, ll j) {
  ll ret = h[i * N + j];
  ll nh = h[i * N + j] + used[i * N + j];

  rep(d, 4) {
    ll ni = i + dy[d], nj = j + dx[d];

    if (ni < 0 || ni >= N || nj < 0 || nj >= N)
      continue;

    if (nh < h[ni * N + nj]) {
      h[ni * N + nj] = nh;
      bfs(ni, nj);
    }
  }

  return ret;
}

int main() {
  std::cin >> N;

  rep(i, N * N) std::cin >> P[i], --P[i];

  rep(i, N * N) {
    ll r = i / N, c = i % N;

    h[i] = std::min({r, N - r - 1, c, N - c - 1});
    used[i] = true;
  }

  ll ans = 0;

  rep(i, N * N) {
    ll r = P[i] / N;
    ll c = P[i] % N;

    used[r * N + c] = false;
    ans += bfs(r, c);
  }

  std::cout << ans << std::endl;

  return 0;
}