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

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rc(x) return cout << x << endl, 0
#define pb push_back
#define mkp make_pair
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef long double ld;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll llinf = (1LL << 62);
const int inf = (1 << 30);
const int nmax = 5e2 + 50;
const ll mod = 1e9 + 7;
using namespace std;
int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1}, n, p[nmax * nmax],
    f[nmax][nmax], a[nmax][nmax], x, y, nx, ny, i, j, rs, vl;
pair<int, int> pr[nmax * nmax];
queue<pair<int, int>> q;
int main() {
  // freopen("sol.in","r",stdin);
  // freopen("sol.out","w",stdout);
  // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cerr.tie(0);
  cout.tie(0);
  cin >> n;
  for (i = 1; i <= n * n; i++)
    cin >> p[i];
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++)
      f[i][j] = min(min(i - 1, j - 1), min(n - i, n - j)), a[i][j] = 1,
      pr[n * (i - 1) + j] = mkp(i, j);
  for (i = 1; i <= n * n; i++) {
    x = pr[p[i]].fr, y = pr[p[i]].sc;
    rs += f[x][y];
    a[x][y] = 0;
    q.push(mkp(x, y));
    while (!q.empty()) {
      x = q.front().fr, y = q.front().sc;
      q.pop();
      vl = f[x][y] + a[x][y];
      for (j = 0; j < 4; j++) {
        nx = x + dx[j], ny = y + dy[j];
        if (nx < 1 || nx > n || ny < 1 || ny > n || f[nx][ny] <= vl)
          continue;
        q.push(mkp(nx, ny));
        f[nx][ny] = vl;
      }
    }
  }
  cout << rs << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rc(x) return cout << x << endl, 0
#define pb push_back
#define mkp make_pair
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef long double ld;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll llinf = (1LL << 62);
const int inf = (1 << 30);
const int nmax = 5e2 + 50;
const ll mod = 1e9 + 7;
using namespace std;
int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1}, n, p[nmax * nmax],
    f[nmax][nmax], a[nmax][nmax], x, y, nx, ny, v[nmax][nmax], i, j, rs, vl;
pair<int, int> pr[nmax * nmax];
queue<pair<int, int>> q;
int main() {
  // freopen("sol.in","r",stdin);
  // freopen("sol.out","w",stdout);
  // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cerr.tie(0);
  cout.tie(0);
  cin >> n;
  for (i = 1; i <= n * n; i++)
    cin >> p[i];
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++)
      f[i][j] = min(min(i - 1, j - 1), min(n - i, n - j)), a[i][j] = 1,
      pr[n * (i - 1) + j] = mkp(i, j);
  for (i = 1; i <= n * n; i++) {
    x = pr[p[i]].fr, y = pr[p[i]].sc;
    rs += f[x][y];
    a[x][y] = 0;
    q.push(mkp(x, y));
    v[i][j] = 1;
    while (!q.empty()) {
      x = q.front().fr, y = q.front().sc;
      q.pop();
      v[x][y] = 0;
      vl = f[x][y] + a[x][y];
      for (j = 0; j < 4; j++) {
        nx = x + dx[j], ny = y + dy[j];
        if (nx < 1 || nx > n || ny < 1 || ny > n || f[nx][ny] <= vl)
          continue;
        q.push(mkp(nx, ny));
        f[nx][ny] = vl;
        v[nx][ny] = 1;
      }
    }
  }
  cout << rs << endl;
  return 0;
}