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

#include <bits/stdc++.h>
typedef int ll;
typedef long double ld;
#define pb push_back
#define pii pair<ll, ll>
#define F first
#define S second
#define endl '\\n'
// #define int long long
#define sync                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define kill(x) return cout << x << '\\n', 0;
using namespace std;
const int N = 520;
ll dis[N][N];
pii nod[N * N];
ll n;
ll vis[N][N];
ll upd(ll v, ll u, ll val) {
  if (v > n || v < 1 || u > n || u < 1)
    return 0;
  if (dis[v][u] > val) {
    dis[v][u] = val;
    return 1;
  }
  return 0;
}
ll solve(ll x, ll y) {
  dis[x][y]--;
  queue<pii> q;
  q.push({x, y});
  while (q.size()) {
    ll v = q.front().F, u = q.front().S;
    q.pop();
    ll z = dis[v][u] + vis[v + 1][u];
    ll b = upd(v + 1, u, z);
    if (b) {
      q.push({v + 1, u});
    }
    z = dis[v][u] + vis[v - 1][u];
    b = upd(v - 1, u, z);
    if (b) {
      q.push({v - 1, u});
    }
    z = dis[v][u] + vis[v][u + 1];
    b = upd(v, u + 1, z);
    if (b) {
      q.push({v, u + 1});
    }
    z = dis[v][u] + vis[v][u - 1];
    b = upd(v, u - 1, z);
    if (b) {
      q.push({v, u - 1});
    }
  }
  return 0;
}
int32_t main() {
  sync;
  cin >> n;
  ll cnt = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      dis[i][j] = min(i - 1, min(j - 1, min(n - i, n - j))) + 1;
      nod[cnt++] = {i, j};
      vis[i][j] = 1;
    }
  }
  ll ans = 0;
  for (int i = 1; i <= n * n; i++) {
    ll x;
    cin >> x;
    ll v = nod[x].F, u = nod[x].S;
    solve(v, u);
    ans += dis[v][u];
    vis[v][u] = 0;
  }
  cout << ans << endl;
}


Error: -6
free(): double free detected in tcache 2


Fixed submission:

#include <bits/stdc++.h>
typedef int ll;
typedef long double ld;
#define pb push_back
#define pii pair<ll, ll>
#define F first
#define S second
#define endl '\\n'
// #define int long long
#define sync                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define kill(x) return cout << x << '\\n', 0;
using namespace std;
const int N = 520;
ll dis[N][N];
pii nod[N * N];
ll n;
ll vis[N][N];
ll upd(ll v, ll u, ll val) {
  if (v > n || v < 1 || u > n || u < 1)
    return 0;
  if (dis[v][u] > val) {
    dis[v][u] = val;
    return 1;
  }
  return 0;
}
ll solve(ll x, ll y) {
  dis[x][y]--;
  queue<pii> q;
  q.push({x, y});
  while (q.size()) {
    ll v = q.front().F, u = q.front().S;
    q.pop();
    ll z = dis[v][u] + vis[v + 1][u];
    ll b = upd(v + 1, u, z);
    if (b) {
      q.push({v + 1, u});
    }
    z = dis[v][u] + vis[v - 1][u];
    b = upd(v - 1, u, z);
    if (b) {
      q.push({v - 1, u});
    }
    z = dis[v][u] + vis[v][u + 1];
    b = upd(v, u + 1, z);
    if (b) {
      q.push({v, u + 1});
    }
    z = dis[v][u] + vis[v][u - 1];
    b = upd(v, u - 1, z);
    if (b) {
      q.push({v, u - 1});
    }
  }
}
int32_t main() {
  sync;
  cin >> n;
  ll cnt = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      dis[i][j] = min(i - 1, min(j - 1, min(n - i, n - j))) + 1;
      nod[cnt++] = {i, j};
      vis[i][j] = 1;
    }
  }
  ll ans = 0;
  for (int i = 1; i <= n * n; i++) {
    ll x;
    cin >> x;
    ll v = nod[x].F, u = nod[x].S;
    solve(v, u);
    ans += dis[v][u];
  }
  cout << ans << endl;
}