Problem p02670 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<ll, ll> PP;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define rrep(i, n) for (ll i = n - 1; i > -1; i--)
#define all(v) v.begin(), v.end()
#define pb push_back
#define fi first
#define se second
const ll INF = 1LL << 61;
const ll MOD = 1000000007 /*988244353*/;
const ll MAX_N = 500010;
ll a, b, c, d, e, f, h, x, y, z, p, q, m, n, t, r, k, w, l, ans, i, j;
string S, T;
vl A, B;
vl g[MAX_N];
vl visited(MAX_N, 0), used(MAX_N, 0);
vl D(MAX_N);
void solve() {
  rep(i, n) {
    rep(j, n - 1) {
      g[i * n + j].pb(i * n + j + 1);
      g[i * n + j + 1].pb(i * n + j);
      g[(j + 1) * n + i].pb(j * n + i);
      g[j * n + i].pb((j + 1) * n + i);
    }
  }

  rep(i, n) {
    rep(j, n) { D[i * n + j] = min({i, n - 1 - i, j, n - 1 - j}); }
  }

  rep(i, n * n) {
    x = A[i];
    // cout << x << endl;
    used[x] = 1;
    p = 0;
    queue<ll> Q;
    Q.push(x);
    vl K;
    visited[x] = 1;
    K.pb(x);
    while (!Q.empty()) {
      int v = Q.front();
      Q.pop();
      for (ll x : g[v]) {
        if (visited[x] == 0 && D[x] > D[v] + 1 && used[v] == 0) {
          visited[x] = 1;
          K.pb(x);
          Q.push(x);
          D[x] = D[v] + 1;
        }
        if (visited[x] == 0 && D[x] > D[v] && used[v] == 1) {
          visited[x] = 1;
          Q.push(x);
          K.pb(x);
          D[x] = D[v];
        }
      }
    }

    for (ll i : K)
      visited[i]--;

    /*rep(j, n) {
        rep(k, n) { cout << D[j * n + k]; }
        cout << " ";
        rep(k, n) { cout << used[j * n + k]; }
        cout << endl;
    }*/

    ans += D[x];

    // cout << endl;
  }

  cout << ans << endl;
}
int main() {
  // cout<<fixed<<setprecision(15);
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  rep(i, n * n) {
    cin >> a;
    a--;
    A.pb(a);
  }
  solve();
}


Fixed submission:
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef int ll;
typedef vector<ll> vl;
typedef pair<ll, ll> PP;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define rrep(i, n) for (ll i = n - 1; i > -1; i--)
#define all(v) v.begin(), v.end()
#define pb push_back
#define fi first
#define se second
const ll INF = 1LL << 61;
const ll MOD = 1000000007 /*988244353*/;
const ll MAX_N = 500010;
ll a, b, c, d, e, f, h, x, y, z, p, q, m, n, t, r, k, w, l, ans, i, j;
string S, T;
vl A, B;
vl g[MAX_N];
vl visited(MAX_N, 0), used(MAX_N, 0);
vl D(MAX_N);
void solve() {
  rep(i, n) {
    rep(j, n - 1) {
      g[i * n + j].pb(i * n + j + 1);
      g[i * n + j + 1].pb(i * n + j);
      g[(j + 1) * n + i].pb(j * n + i);
      g[j * n + i].pb((j + 1) * n + i);
    }
  }

  rep(i, n) {
    rep(j, n) { D[i * n + j] = min({i, n - 1 - i, j, n - 1 - j}); }
  }

  rep(i, n * n) {
    x = A[i];
    // cout << x << endl;
    used[x] = 1;
    p = 0;
    queue<ll> Q;
    Q.push(x);
    vl K;
    visited[x] = 1;
    K.pb(x);
    while (!Q.empty()) {
      int v = Q.front();
      Q.pop();
      for (ll x : g[v]) {
        if (visited[x] == 0 && D[x] > D[v] + 1 && used[v] == 0) {
          visited[x] = 1;
          K.pb(x);
          Q.push(x);
          D[x] = D[v] + 1;
        }
        if (visited[x] == 0 && D[x] > D[v] && used[v] == 1) {
          visited[x] = 1;
          Q.push(x);
          K.pb(x);
          D[x] = D[v];
        }
      }
    }

    for (ll i : K)
      visited[i]--;

    /*rep(j, n) {
        rep(k, n) { cout << D[j * n + k]; }
        cout << " ";
        rep(k, n) { cout << used[j * n + k]; }
        cout << endl;
    }*/

    ans += D[x];

    // cout << endl;
  }

  cout << ans << endl;
}
int main() {
  // cout<<fixed<<setprecision(15);
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  rep(i, n * n) {
    cin >> a;
    a--;
    A.pb(a);
  }
  solve();
}