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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define llu unsigned long long
#define ll long long
ll read() {
  ll x;
  if (scanf("%lld", &x) == -1)
    exit(0);
  return x;
}
#define vi vector<int>
#define vb vector<bool>
const long long INF = 5e18;

#define LOCAL_JUDGE
void c_p_c() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifdef LOCAL_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

const int N = 505;
bool killed[N][N];
int n;
int memo[N][N] = {0};
int dist[N][N] = {0};

int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
vector<pair<int, int>> nei[N][N];

int main() {
  // c_p_c();
  n = read();
  for (auto row = 0; row < n; row++) {
    for (auto col = 0; col < n; col++) {
      dist[row][col] = 1 + min({row, n - 1 - row, col, n - 1 - col});
    }
  }

  for (int row = 0; row < n; ++row) {
    for (int col = 0; col < n; ++col) {
      for (int dir = 0; dir < 4; ++dir) {
        int r2 = row + dx[dir];
        int c2 = col + dy[dir];
        if (0 <= r2 && 0 <= c2 && r2 < n && c2 < n) {
          nei[row][col].emplace_back(r2, c2);
        }
      }
    }
  }

  long long answer = 0;
  for (auto turn = 1; turn <= n * n; turn++) {
    int who = read();
    who--;
    int row = who / n;
    int col = who % n;
    answer += dist[row][col] - 1;
    dist[row][col]--;
    killed[row][col] = true;
    vector<pair<int, int>> q;
    q.emplace_back(row, col);
    for (int i = 0; i < (int)q.size(); i++) {
      pair<int, int> a = q[i];
      for (const pair<int, int> &b : nei[a.first][a.second]) {
        int maybe = dist[a.first][a.second];
        if (!killed[b.first][b.second])
          maybe++;
        if (dist[b.first][b.second] > maybe) {
          dist[b.first][b.second] = maybe;
          q.push_back(b);
        }
      }
    }
  }

  cout << answer << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define llu unsigned long long
#define ll long long
ll read() {
  ll x;
  if (scanf("%lld", &x) == -1)
    exit(0);
  return x;
}
#define vi vector<int>
#define vb vector<bool>
const long long INF = 5e18;

#define LOCAL_JUDGE
void c_p_c() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifdef LOCAL_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

const int N = 200;
int is_out[N][N] = {0};
int n;
int memo[N][N] = {0};
int scors[N][N] = {0};

void setscors(int x, int y) {
  scors[x][y] = 1 + min(x % (n - 1),
                        min(y % (n - 1),
                            min((n - 1 - x) % (n - 1), (n - 1 - y) % (n - 1))));
}
int min_(int v1, int v2, int v3, int v4) {
  return min(v1, min(v2, min(v3, v4)));
}

void update_point(int x, int y) {
  int v1 = INF;
  if (x < (n - 1)) {
    if (is_out[x + 1][y])
      v1 = scors[x + 1][y];
    else
      v1 = 1 + scors[x + 1][y];
  }

  int v2 = INF;
  if (x > 0) {
    if (is_out[x - 1][y])
      v2 = scors[x - 1][y];
    else
      v2 = 1 + scors[x - 1][y];
  }

  int v3 = INF;
  if (y < (n - 1)) {
    if (is_out[x][y + 1])
      v3 = scors[x][y + 1];
    else
      v3 = 1 + scors[x][y + 1];
  }

  int v4 = INF;
  if (y > 0) {
    if (is_out[x][y - 1])
      v4 = scors[x][y - 1];
    else
      v4 = 1 + scors[x][y - 1];
  }

  int v = INF;
  if (is_out[x][y])
    v = min_(v1, v2, v3, v4);
  else
    v = 1 + min_(v1, v2, v3, v4);
  scors[x][y] = min(scors[x][y], v);
}

void update_neighbors_of(int x, int y) {
  queue<pair<int, int>> q;
  if (x < n - 1)
    q.push({x + 1, y});
  if (x > 0)
    q.push({x - 1, y});
  if (y < n - 1)
    q.push({x, y + 1});
  if (y > 0)
    q.push({x, y - 1});
  int visited[N][N] = {0};
  while (!q.empty()) {
    auto p = q.front();
    q.pop();

    if (visited[p.first][p.second])
      continue;
    visited[p.first][p.second] = true;
    update_point(p.first, p.second);
    if (p.first < n - 1)
      q.push({p.first + 1, p.second});
    if (p.first > 0)
      q.push({p.first - 1, p.second});
    if (p.second < n - 1)
      q.push({p.first, p.second + 1});
    if (p.second > 0)
      q.push({p.first, p.second - 1});
  }
}

int main() {
  // c_p_c();
  n = read();
  vi v;
  for (auto i = 0; i < (n * n); i++) {
    v.push_back(read() - 1);
  }

  for (auto i = 0; i < n * n; i++) {
    int x = v[i] / n;
    int y = v[i] % n;
    setscors(x, y);
  }

  int cnt = 0;
  for (auto i = 0; i < n * n; i++) {
    int x = v[i] / n;
    int y = v[i] % n;
    cnt += (scors[x][y]) ? scors[x][y] - 1 : 0;
    is_out[x][y] = 1;
    if (scors[x][y] > 0)
      scors[x][y]--;
    update_neighbors_of(x, y);
  }

  cout << cnt << endl;
  return 0;
}