Problem p02670 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define llinf (1ll << 60)
#define BG(x) ((x).begin())
#define ED(x) ((x).end())
#define rBG(x) ((x).rbegin())
#define rED(x) ((x).rend())
#define mp(a, b) make_pair((a), (b))

ll t;

ll n;
ll p[505];

ll dist[505][505];
ll occ[505][505];
struct cmp {
  bool operator()(const pair<ll, pair<ll, ll>> &a,
                  const pair<ll, pair<ll, ll>> &b) {
    return a.first > b.first;
  }
};

priority_queue<pair<ll, pair<ll, ll>>, vector<pair<ll, pair<ll, ll>>>, cmp> di;
bitset<400000> visited;
ll ans;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  //    cin >> t;
  t = 1;

  for (int _ = 1; _ <= t; _++) {
    //    cout << "Case #" << _ << ": ";

    cin >> n;

    for (int i = 0; i <= n + 1; i++) {
      for (int j = 0; j <= n + 1; j++) {
        dist[i][j] = llinf;
      }
    }
    for (ll i = 1; i <= n; i++) {
      for (ll j = 1; j <= n; j++) {
        occ[i][j] = 1;
        dist[i][j] = min(min(i, j) - 1, n - max(i, j));
      }
    }

    for (int i = 0; i < n * n; i++) {
      cin >> p[i];
      p[i]--;

      ans += dist[p[i] / n + 1][p[i] % n + 1];
      occ[p[i] / n + 1][p[i] % n + 1] = 0;

      di.push(mp(0, mp(p[i] / n + 1, p[i] % n + 1)));
      visited.reset();

      /* for(int i = 0; i < n+2; i++) {
              for(int j = 0; j < n+2; j++) {
                      printf("%2lld ", dist[i][j]);
              }
              printf("\n");
      }
      printf("\n"); */
      while (di.size()) {
        auto now = di.top().second;
        di.pop();
        if (visited[now.first * (n + 1) + now.second])
          continue;
        visited[now.first * (n + 1) + now.second] = 1;
        if (now.first > 1) {
          //	ll best[3]{dist[now.first][now.second-1],
          //dist[now.first][now.second+1], dist[now.first+1][now.second]};
          //	sort(best, best + 3);
          if (dist[now.first - 1][now.second] >
              dist[now.first][now.second] + occ[now.first][now.second]) {
            dist[now.first - 1][now.second] =
                dist[now.first][now.second] + occ[now.first][now.second];
            di.push(mp(dist[now.first - 1][now.second],
                       mp(now.first - 1, now.second)));
          }
        }
        if (now.second > 1) {
          //	ll best[3]{dist[now.first-1][now.second],
          //dist[now.first+1][now.second], dist[now.first][now.second+1]};
          //	sort(best, best + 3);
          if (dist[now.first][now.second - 1] >
              dist[now.first][now.second] + occ[now.first][now.second]) {
            dist[now.first][now.second - 1] =
                dist[now.first][now.second] + occ[now.first][now.second];
            di.push(mp(dist[now.first][now.second - 1],
                       mp(now.first, now.second - 1)));
          }
        }
        if (now.first < n) {
          //	ll best[3]{dist[now.first][now.second-1],
          //dist[now.first][now.second+1], dist[now.first+1][now.second]};
          //	sort(best, best + 3);
          if (dist[now.first + 1][now.second] >
              dist[now.first][now.second] + occ[now.first][now.second]) {
            dist[now.first + 1][now.second] =
                dist[now.first][now.second] + occ[now.first][now.second];
            di.push(mp(dist[now.first + 1][now.second],
                       mp(now.first + 1, now.second)));
          }
        }
        if (now.second < n) {
          //	ll best[3]{dist[now.first-1][now.second],
          //dist[now.first+1][now.second], dist[now.first][now.second+1]};
          //	sort(best, best + 3);
          if (dist[now.first][now.second + 1] >
              dist[now.first][now.second] + occ[now.first][now.second]) {
            dist[now.first][now.second + 1] =
                dist[now.first][now.second] + occ[now.first][now.second];
            di.push(mp(dist[now.first][now.second + 1],
                       mp(now.first, now.second + 1)));
          }
        }
      }
    }
    cout << ans << '\n';
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define llinf (1ll << 60)
#define BG(x) ((x).begin())
#define ED(x) ((x).end())
#define rBG(x) ((x).rbegin())
#define rED(x) ((x).rend())
#define mp(a, b) make_pair((a), (b))

ll t;

ll n;
ll p[300000];

ll dist[505][505];
ll occ[505][505];
struct cmp {
  bool operator()(const pair<ll, pair<ll, ll>> &a,
                  const pair<ll, pair<ll, ll>> &b) {
    return a.first > b.first;
  }
};

priority_queue<pair<ll, pair<ll, ll>>, vector<pair<ll, pair<ll, ll>>>, cmp> di;
bitset<400000> visited;
ll ans;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  //    cin >> t;
  t = 1;

  for (int _ = 1; _ <= t; _++) {
    //    cout << "Case #" << _ << ": ";

    cin >> n;

    for (int i = 0; i <= n + 1; i++) {
      for (int j = 0; j <= n + 1; j++) {
        dist[i][j] = llinf;
      }
    }
    for (ll i = 1; i <= n; i++) {
      for (ll j = 1; j <= n; j++) {
        occ[i][j] = 1;
        dist[i][j] = min(min(i, j) - 1, n - max(i, j));
      }
    }

    for (int i = 0; i < n * n; i++) {
      cin >> p[i];
      p[i]--;

      ans += dist[p[i] / n + 1][p[i] % n + 1];
      occ[p[i] / n + 1][p[i] % n + 1] = 0;

      di.push(mp(0, mp(p[i] / n + 1, p[i] % n + 1)));
      visited.reset();

      /* for(int i = 0; i < n+2; i++) {
              for(int j = 0; j < n+2; j++) {
                      printf("%2lld ", dist[i][j]);
              }
              printf("\n");
      }
      printf("\n"); */
      while (di.size()) {
        auto now = di.top().second;
        di.pop();
        if (visited[now.first * (n + 1) + now.second])
          continue;
        visited[now.first * (n + 1) + now.second] = 1;
        if (now.first > 1) {
          //	ll best[3]{dist[now.first][now.second-1],
          //dist[now.first][now.second+1], dist[now.first+1][now.second]};
          //	sort(best, best + 3);
          if (dist[now.first - 1][now.second] >
              dist[now.first][now.second] + occ[now.first][now.second]) {
            dist[now.first - 1][now.second] =
                dist[now.first][now.second] + occ[now.first][now.second];
            di.push(mp(dist[now.first - 1][now.second],
                       mp(now.first - 1, now.second)));
          }
        }
        if (now.second > 1) {
          //	ll best[3]{dist[now.first-1][now.second],
          //dist[now.first+1][now.second], dist[now.first][now.second+1]};
          //	sort(best, best + 3);
          if (dist[now.first][now.second - 1] >
              dist[now.first][now.second] + occ[now.first][now.second]) {
            dist[now.first][now.second - 1] =
                dist[now.first][now.second] + occ[now.first][now.second];
            di.push(mp(dist[now.first][now.second - 1],
                       mp(now.first, now.second - 1)));
          }
        }
        if (now.first < n) {
          //	ll best[3]{dist[now.first][now.second-1],
          //dist[now.first][now.second+1], dist[now.first+1][now.second]};
          //	sort(best, best + 3);
          if (dist[now.first + 1][now.second] >
              dist[now.first][now.second] + occ[now.first][now.second]) {
            dist[now.first + 1][now.second] =
                dist[now.first][now.second] + occ[now.first][now.second];
            di.push(mp(dist[now.first + 1][now.second],
                       mp(now.first + 1, now.second)));
          }
        }
        if (now.second < n) {
          //	ll best[3]{dist[now.first-1][now.second],
          //dist[now.first+1][now.second], dist[now.first][now.second+1]};
          //	sort(best, best + 3);
          if (dist[now.first][now.second + 1] >
              dist[now.first][now.second] + occ[now.first][now.second]) {
            dist[now.first][now.second + 1] =
                dist[now.first][now.second] + occ[now.first][now.second];
            di.push(mp(dist[now.first][now.second + 1],
                       mp(now.first, now.second + 1)));
          }
        }
      }
    }
    cout << ans << '\n';
  }

  return 0;
}