alexjercan/bugnet huggingface dataset:
Problem p02670 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 1LL << 61;

const ll N = 510;
ll n;
vec idx = {1, -1, 0, 0};
vec idy = {0, 0, 1, -1};
mat dis(N, vec(N)), v(N, vec(N));

void dfs(ll x, ll y) {
  for (ll i = 0; i < 4; i++) {
    ll nx = idx[i] + x;
    ll ny = idy[i] + y;
    if (dis[x][y] + v[x][y] < dis[nx][ny]) {
      dis[nx][ny] = dis[x][y] + v[x][y];
      dfs(nx, ny);
    }
  }
}

int main() {
  cin >> n;
  vec p(n * n);
  for (ll i = 0; i < n * n; i++) {
    cin >> p[i];
  }
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= n; j++) {
      v[i][j] = 1;
      dis[i][j] = min({i - 1, j - 1, n - i, n - j});
    }
  }
  ll ans = 0;
  for (ll i = 0; i < n * n; i++) {
    ll x = (p[i] - 1) / n + 1;
    ll y = (p[i] - 1) % n + 1;
    v[x][y] = 0;
    ans += dis[x][y];
    dfs(x, y);
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 1LL << 61;

int main() {
  ll n;
  cin >> n;
  vec p(n * n);
  for (ll i = 0; i < n * n; i++) {
    cin >> p[i];
    p[i]--;
  }
  vec d(n * n, -1);
  queue<pll> q;
  for (ll i = 0; i < n; i++) {
    q.push({i * n, 0});
    q.push({i * n + n - 1, 0});
    q.push({i, 0});
    q.push({n * (n - 1) + i, 0});
  }
  while (q.size()) {
    ll id = q.front().first;
    ll td = q.front().second;
    q.pop();
    if (d[id] > -1)
      continue;
    d[id] = td;
    if (id + 1 < n * n)
      q.push({id + 1, td + 1});
    if (id - 1 > -1)
      q.push({id - 1, td + 1});
    if (id + n < n * n)
      q.push({id + n, td + 1});
    if (id - n > -1)
      q.push({id - n, td + 1});
  }
  ll ans = 0;
  set<ll> al;
  for (ll i = 0; i < n * n; i++) {
    ll id = p[i];
    ans += d[id];
    q.push({id, d[id] - 1});
    while (q.size()) {
      ll tid = q.front().first;
      ll td = q.front().second;
      q.pop();
      if (td >= d[tid])
        continue;
      d[tid] = td;
      if (!al.count(tid)) {
        td++;
      }
      if (tid + 1 < n * n && d[tid + 1] > td)
        q.push({tid + 1, td});
      if (tid - 1 > -1 && d[tid - 1] > td)
        q.push({tid - 1, td});
      if (tid + n < n * n && d[tid + n] > td)
        q.push({tid + n, td});
      if (tid - n > -1 && d[tid - n] > td)
        q.push({tid - n, td});
    }
    al.insert(id);
    d[id]++;
  }
  cout << ans << endl;
}
Problem p02670 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
using namespace std;
#define MAXN 100100
#define ll long long
ll n, m, a[510][510], dis[510][510], p[500100], ans;
void dfs(ll x, ll y) {
  if (dis[x - 1][y] > dis[x][y] + a[x][y]) {
    dis[x - 1][y] = dis[x][y] + a[x][y];
    dfs(x - 1, y);
  }
  if (dis[x][y - 1] > dis[x][y] + a[x][y]) {
    dis[x][y - 1] = dis[x][y] + a[x][y];
    dfs(x, y - 1);
  }
  if (dis[x + 1][y] > dis[x][y] + a[x][y]) {
    dis[x + 1][y] = dis[x][y] + a[x][y];
    dfs(x + 1, y);
  }
  if (dis[x][y + 1] > dis[x][y] + a[x][y]) {
    dis[x][y + 1] = dis[x][y] + a[x][y];
    dfs(x, y + 1);
  }
}
int main() {
  scanf("%lld", &n);
  for (ll i = 1; i <= n * n; i++)
    scanf("%lld", &p[i]);
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= n; j++) {
      a[i][j] = 1;
      dis[i][j] = min(i - 1, min(j - 1, min(n - i, n - j)));
    }
  }
  int x, y;
  for (ll i = 1; i <= n * n; i++) {
    y = p[i] % n;
    if (y == 0)
      y = n;
    x = (p[i] - y) / n + 1;
    ans += dis[x][y];
    a[x][y] = 0;
    dfs(x, y);
  }
  printf("%lld\\n", ans);

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
using namespace std;
#define MAXN 100100
#define ll long long
ll n, m, a[510][510], dis[510][510], p[50100], ans;
void dfs(ll x, ll y) {
  if (dis[x - 1][y] > dis[x][y] + a[x][y]) {
    dis[x - 1][y] = dis[x][y] + a[x][y];
    dfs(x - 1, y);
  }
  if (dis[x][y - 1] > dis[x][y] + a[x][y]) {
    dis[x][y - 1] = dis[x][y] + a[x][y];
    dfs(x, y - 1);
  }
  if (dis[x + 1][y] > dis[x][y] + a[x][y]) {
    dis[x + 1][y] = dis[x][y] + a[x][y];
    dfs(x + 1, y);
  }
  if (dis[x][y + 1] > dis[x][y] + a[x][y]) {
    dis[x][y + 1] = dis[x][y] + a[x][y];
    dfs(x, y + 1);
  }
}
int main() {
  scanf("%lld", &n);
  for (ll i = 1; i <= n * n; i++)
    scanf("%lld", &p[i]);
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= n; j++) {
      a[i][j] = 1;
      dis[i][j] = min(i - 1, min(j - 1, min(n - i, n - j)));
    }
  }
  int x, y;
  for (ll i = 1; i <= n * n; i++) {
    y = p[i] % n;
    if (y == 0)
      y = n;
    x = (p[i] - y) / n + 1;
    ans += dis[x][y];
    a[x][y] = 0;
    dfs(x, y);
  }
  printf("%lld\\n", ans);

  return 0;
}
Problem p02670 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
using namespace std;
const int N = 555;
int a[N * N], d[N * N], f[N * N];
int vis[N][N];
int n, m, k, tot;
int dx[4] = {0, 0, -1, 1}, dy[4] = {1, -1, 0, 0};
void dfs(int x, int y, int z) {
  z = z + (vis[x][y] ^ 1);
  for (int i = 0; i < 4; i++) {
    int p = x + dx[i], q = y + dy[i];
    if (p < 1 || p > n || q < 1 || q > n)
      continue;
    if (d[(p - 1) * n + q] > z) {
      d[(p - 1) * n + q] = z;
      dfs(p, q, z);
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      d[(i - 1) * n + j] = min(min(i - 1, n - i), min(j - 1, n - j));
  for (int i = 1; i <= n * n; i++)
    scanf("%d", &a[i]);
  int ans = 0;
  for (int i = 1; i <= n * n; i++) {
    ans = ans + d[a[i]];
    int x = (a[i] - 1) / n + 1;
    int y = a[i] - (x - 1) * n;
    vis[x][y] = 1;
    dfs(x, y, d[a[i]]);
  }
  printf("%d\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;
const int N = 555;
int a[N];
int vis[N][N];
int n, m, k, tot;
int put(int s) {
  int x = (s - 1) / n + 1;
  int y = s - (x - 1) * n;
  int sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0;
  vis[x][y] = 1;
  for (int i = 1; i < x; i++)
    sum1 = sum1 + (vis[i][y] ^ 1);
  for (int i = x + 1; i <= n; i++)
    sum2 = sum2 + (vis[i][y] ^ 1);
  for (int i = 1; i < y; i++)
    sum3 = sum3 + (vis[x][i] ^ 1);
  for (int i = y + 1; i <= n; i++)
    sum4 = sum4 + (vis[x][i] ^ 1);
  return min(min(sum1, sum2), min(sum3, sum4));
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n * n; i++)
    scanf("%d", &a[i]);
  int ans = 0;
  for (int i = 1; i <= n * n; i++)
    ans = ans + put(a[i]);
  printf("%d\\n", ans);
  return 0;
}
Problem p02670 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#define FPEN freopen("in.txt", "r", stdin);
#define FPEN2 freopen("out.txt", "w", stdout);
#else
#define FPEN
#define FPEN2
#endif
const int maxn = 200009;
const int MOD = 1e9 + 7;
int N;
int ans = 0;
int black[555][555], dist[555][555];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
void dijk(int x, int y) {
  ans += dist[x][y];
  black[x][y] = 0;
  priority_queue<pair<int, int>> que;
  que.emplace(-dist[x][y], x * N + y);
  while (que.size()) {
    x = que.top().second / N, y = que.top().second % N;
    int Dist = -que.top().first + black[x][y];
    que.pop();
    for (int j = 0; j < 4; ++j) {
      int nx = x + dx[j], ny = y + dy[j];
      if (nx >= 0 && nx < N && ny >= 0 && ny < N && dist[nx][ny] > Dist) {
        dist[nx][ny] = Dist;
        que.emplace(-Dist, nx * N + ny);
      }
    }
  }
}
int main() {
  FPEN FPEN2;
  ios_base::sync_with_stdio(false), cout.tie(0), cin.tie(0);
  cin >> N;
  for (int i = 0; i < N; ++i)
    for (int j = 0; j < N; ++j)
      black[i][j] = 1, dist[i][j] = min(min(N - 1 - i, N - 1 - j), min(i, j));
  for (int i = 0; i < N * N; ++i) {
    int t;
    cin >> t;
    t--;
    dijk(t / N, t % N);
  }
  cout << ans << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#define FPEN freopen("in.txt", "r", stdin);
#define FPEN2 freopen("out.txt", "w", stdout);
#else
#define FPEN
#define FPEN2
#endif
const int maxn = 200009;
const int MOD = 1e9 + 7;
int N;
int ans = 0;
int black[555][555], vis[555][555];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
void bfs(int x, int y) {
  vector<pair<int, int>> que1, que2;
  que1.emplace_back(0, 0);
  memset(vis, -1, sizeof vis);
  vis[0][0] = 0;
  while (1) {
    que2.clear();
    for (int i = 0; i < que1.size(); ++i) {
      for (int j = 0; j < 4; ++j) {
        int nx = que1[i].first + dx[j], ny = que1[i].second + dy[j];
        if (nx >= 0 && nx <= N + 1 && ny >= 0 && ny <= N + 1 &&
            vis[nx][ny] == -1) {
          if (black[nx][ny]) {
            vis[nx][ny] = vis[que1[i].first][que1[i].second] + 1;
            que2.emplace_back(nx, ny);
            if (nx == x && ny == y) {
              ans += vis[nx][ny];
              return;
            }
          } else {
            vis[nx][ny] = vis[que1[i].first][que1[i].second];
            que1.emplace_back(nx, ny);
            if (nx == x && ny == y) {
              ans += vis[nx][ny];
              return;
            }
          }
        }
      }
    }
    swap(que1, que2);
  }
}
int main() {
  FPEN FPEN2;
  ios_base::sync_with_stdio(false), cout.tie(0), cin.tie(0);
  cin >> N;
  for (int i = 1; i <= N; ++i)
    for (int j = 1; j <= N; ++j)
      black[i][j] = 1;
  for (int i = 0; i < N * N; ++i) {
    int t;
    cin >> t;
    t--;
    black[t / N + 1][t % N + 1] = 0;
    bfs(t / N + 1, t % N + 1);
  }
  cout << ans << '\\n';
  return 0;
}
Problem p02670 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define endl '\\n'
#define ll long long
using namespace std;
vector<vector<int>> ofo;
vector<vector<bool>> kayn;
int n;
void update(int a, int b) {
  int sd = kayn[a][b] + ofo[a][b];
  if (a > 0) {
    if (ofo[a - 1][b] > sd) {
      ofo[a - 1][b] = sd;
      update(a - 1, b);
    }
  }
  if (a < n - 1) {
    if (ofo[a + 1][b] > sd) {
      ofo[a + 1][b] = sd;
      update(a + 1, b);
    }
  }
  if (b > 0) {
    if (ofo[a][b - 1] > sd) {
      ofo[a][b - 1] = sd;
      update(a, b - 1);
    }
  }
  if (b < n - 1) {
    if (ofo[a][b + 1] > sd) {
      ofo[a][b + 1] = sd;
      update(a, b + 1);
    }
  }
}
void lok() {
  cin >> n;
  ofo.resize(n);
  kayn.resize(n);
  for (int i = 0; i < n; i++) {
    ofo[i].resize(n);
    for (int o = 0; o < n; o++) {
      int ba = min(o, n - o - 1);
      int da = min(i, n - i - 1);
      ofo[i][o] = min(ba, da);
    }
  }
  for (int i = 0; i < n; i++) {
    kayn[i].resize(n);
    kayn[i].assign(kayn[i].size(), true);
  }
  int p;
  ll ans = 0;
  for (ll o = 0; o < n * n; o++) {
    cin >> p;
    int y = (p - 1) / n;
    int x = (p - 1) % n;
    ans += ofo[x][y];
    kayn[x][y] = false;
    update(x, y);
  }
  cout << ans << endl;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  lok();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define endl '\\n'
#define ll long long
using namespace std;
vector<vector<int>> ofo;
vector<vector<bool>> kayn;
int n;
void update(int a, int b) {
  if (a > 0) {
    if (ofo[a - 1][b] > ofo[a][b]) {
      ofo[a - 1][b] = kayn[a][b] + ofo[a][b];
      update(a - 1, b);
    }
  }
  if (a < n - 1) {
    if (ofo[a + 1][b] > ofo[a][b]) {
      ofo[a + 1][b] = kayn[a][b] + ofo[a][b];
      update(a + 1, b);
    }
  }
  if (b > 0) {
    if (ofo[a][b - 1] > ofo[a][b]) {
      ofo[a][b - 1] = kayn[a][b] + ofo[a][b];
      update(a, b - 1);
    }
  }
  if (b < n - 1) {
    if (ofo[a][b + 1] > ofo[a][b]) {
      ofo[a][b + 1] = kayn[a][b] + ofo[a][b];
      update(a, b + 1);
    }
  }
}
void lok() {
  cin >> n;
  ofo.resize(n);
  kayn.resize(n);
  for (int i = 0; i < n; i++) {
    ofo[i].resize(n);
    for (int o = 0; o < n; o++) {
      int ba = min(o, n - o - 1);
      int da = min(i, n - i - 1);
      ofo[i][o] = min(ba, da);
    }
  }
  for (int i = 0; i < n; i++) {
    kayn[i].resize(n);
    kayn[i].assign(kayn[i].size(), true);
  }
  int p;
  ll ans = 0;
  for (ll o = 0; o < n * n; o++) {
    cin >> p;
    int y = (p - 1) / n;
    int x = (p - 1) % n;
    ans += ofo[x][y];
    kayn[x][y] = false;
    update(x, y);
  }
  cout << ans << endl;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  lok();
  return 0;
}
Problem p02670 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

const int Maxn = 505;
const int Maxm = 255;
const int Maxd = 4;
const int dy[Maxd] = {-1, 0, 1, 0};
const int dx[Maxd] = {0, -1, 0, 1};

int dp[Maxn][Maxn];
int n;
bool fr[Maxn][Maxn];
int a[Maxn * Maxn];
int res;

void Traverse(int r, int c, int bef) {
  if (r < 0 || r >= n || c < 0 || c >= n)
    return;
  if (!fr[r][c])
    bef++;
  if (bef >= dp[r][c])
    return;
  dp[r][c] = bef;
  for (int d = 0; d < Maxd; d++)
    Traverse(r + dy[d], c + dx[d], bef);
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n * n; i++) {
    scanf("%d", &a[i]);
    a[i]--;
  }
  fill((int *)dp, (int *)dp + Maxn * Maxn, Maxm);
  for (int i = 0; i < n; i++) {
    Traverse(i, 0, 0);
    Traverse(i, n - 1, 0);
    Traverse(0, i, 0);
    Traverse(n - 1, i, 0);
  }
  for (int i = 0; i < n * n; i++) {
    int r = a[i] / n, c = a[i] % n;
    int my = dp[r][c] - 1;
    fr[r][c] = true;
    res += my;
    Traverse(r, c, my);
  }
  cout << res << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const int Maxn = 505;
const int Maxm = 255;
const int Maxd = 4;
const int dy[Maxd] = {-1, 0, 1, 0};
const int dx[Maxd] = {0, -1, 0, 1};

struct pos {
  int a, b, c;
  pos(int a = 0, int b = 0, int c = 0) : a(a), b(b), c(c) {}
};

bool dp[Maxn][Maxn][Maxm];
int n;
bool fr[Maxn][Maxn];
int a[Maxn * Maxn];
pos seq[Maxn * Maxn * Maxm];
int slen;
int res;

void Gen(int r, int c, int lvl) {
  if (dp[r][c][lvl])
    return;
  dp[r][c][lvl] = true;
  slen = 0;
  seq[slen++] = pos(r, c, lvl);
  for (int i = 0; i < slen; i++) {
    pos p = seq[i];
    for (int d = 0; d < Maxd; d++) {
      pos u = pos(p.a + dy[d], p.b + dx[d], p.c);
      if (0 <= u.a && u.a < n && 0 <= u.b && u.b < n) {
        if (!fr[u.a][u.b])
          u.c++;
        if (u.c < Maxm && !dp[u.a][u.b][u.c]) {
          dp[u.a][u.b][u.c] = true;
          seq[slen++] = u;
        }
      }
    }
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n * n; i++) {
    scanf("%d", &a[i]);
    a[i]--;
  }
  for (int i = 0; i < n; i++) {
    Gen(i, 0, 1);
    Gen(i, n - 1, 1);
    Gen(0, i, 1);
    Gen(n - 1, i, 1);
  }
  for (int i = 0; i < n * n; i++) {
    int r = a[i] / n, c = a[i] % n;
    int my = 0;
    while (!dp[r][c][my])
      my++;
    my--;
    fr[r][c] = true;
    res += my;
    Gen(r, c, my);
  }
  cout << res << endl;
  return 0;
}
Problem p02670 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef pair<int, int> P;
int n, a, dp[500][500], ans, dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}, nx, ny,
                             A, B, fl[500][500];
void f(int x, int y) {
  queue<P> que;
  fl[x][y] = 1;
  que.push({x, y});
  ans += dp[x][y];
  // cout<<" "<<dp[x][y]<<endl;
  while (que.size()) {
    P q = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      nx = q.first + dx[i], ny = q.second + dy[i];
      if (0 <= nx && nx < n && 0 <= ny && ny < n &&
          dp[q.first][q.second] + (fl[q.first][q.second] == 0) < dp[nx][ny]) {
        dp[nx][ny]--;
        que.push({nx, ny});
      }
    }
  }
  return;
}
int main(void) {
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      dp[i][j] = min(min(i, n - i - 1), min(j, n - j - 1));
    }
  }
  /*
  for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
          cout<<dp[i][j];
      }
      cout<<endl;
  }*/
  for (int i = 0; i < n * n; i++) {
    cin >> a;
    A = (a - 1) / n;
    B = (a - 1) % n;
    // cout<<A<<" "<<B<<endl;
    f(A, B);

    /*
    for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
        cout<<dp[i][j];
    }
    cout<<endl;
}
*/
  }
  /*
  for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
          cout<<dp[i][j];
      }
      cout<<endl;
  }*/
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
ll n, q, a, b, c, dp[2][501][501], A, B, ans, AA[501], BB[501], MIN;
vector<ll> v;

void bitadd(ll kk, ll key, ll a) {
  while (a <= 100000) {
    dp[kk][key][a] += 1;
    a += a & (-a);
  }
  return;
}
ll bitsum(ll k, ll key, ll a) {
  ll tot = 0;
  while (a > 0) {
    tot += dp[k][key][a];
    a -= a & (-a);
  }
  return tot;
}
int main(void) {
  cin >> n;
  for (int i = 0; i < n * n; i++) {
    cin >> a;
    v.push_back(a);
  }
  reverse(v.begin(), v.end());
  for (int i = 0; i < n * n; i++) {
    A = (v[i] + n - 1) / n;
    B = v[i] % n;
    if (B == 0)
      B = n;
    // cout<<A<<" "<<B<<" "<<v[i]<<endl;
    MIN = min(bitsum(0, A, B), AA[A] - bitsum(0, A, B));
    // cout<<MIN<<endl;
    MIN = min(MIN, min(bitsum(1, B, A), BB[B] - bitsum(1, B, A)));
    // cout<<BB[B]<<" "<<bitsum(1,B,A)<<endl;
    AA[A]++;
    BB[B]++;
    bitadd(0, A, B);
    bitadd(1, B, A);
    // cout<<v[i]<<" "<<MIN<<" "<<A<<" "<<B<<endl;
    ans += MIN;
  }
  cout << ans << endl;
}
Problem p02670 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll mod = 998244353;
ll mul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
ll add(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
ll sub(ll a, ll b) { return ((a % mod) - (b % mod) + mod) % mod; }
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
ll n;
ll out[503][503];
ll occ[503][503];
void dfs(ll x, ll y) {
  ll z = out[x][y] + occ[x][y];
  for (ll i = 0; i < 4; i++) {
    ll x1 = x + dx[i];
    ll y1 = y + dy[i];
    if (out[x1][y1] > z) {
      out[x1][y1] = z;
      dfs(x1, y1);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= n; j++) {
      out[i][j] = min(min(i - 1, abs(n - i)), min(j - 1, abs(n - j)));
      occ[i][j] = 1;
    }
  }
  ll m = n * n;
  ll pr = 0;
  while (m--) {
    ll id;
    cin >> id;
    ll x = (id + n - 1) / n;
    ll y = id % n;
    if (y == 0) {
      y = n;
    }
    occ[x][y] = 0;
    pr += out[x][y];
    dfs(x, y);
  }
  cout << pr;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll mod = 998244353;
ll mul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
ll add(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
ll sub(ll a, ll b) { return ((a % mod) - (b % mod) + mod) % mod; }
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
ll n;
ll out[503][503];
ll occ[503][503];
ll dfs(ll x, ll y) {
  ll z = out[x][y] + occ[x][y];
  for (ll i = 0; i < 4; i++) {
    ll x1 = x + dx[i];
    ll y1 = y + dy[i];
    if (out[x1][y1] > z) {
      out[x1][y1] = z;
      dfs(x1, y1);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= n; j++) {
      out[i][j] = min(min(i - 1, abs(n - i)), min(j - 1, abs(n - j)));
      occ[i][j] = 1;
    }
  }
  ll m = n * n;
  ll pr = 0;
  while (m--) {
    ll id;
    cin >> id;
    ll x = (id + n - 1) / n;
    ll y = id % n;
    if (y == 0) {
      y = n;
    }
    occ[x][y] = 0;
    pr += out[x][y];
    dfs(x, y);
  }
  cout << pr;
}
Problem p02670 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  const int dx[4] = {1, -1, 0, 0};
  const int dy[4] = {0, 0, 1, -1};
  const int INF = INT32_MAX >> 2;
  vector<bool> k(n * n, true);
  vector<int> d(n * n, INF);
  rep(i, n * n) {
    int x = i % n;
    int y = i / n;
    chmin(d[i], x);
    chmin(d[i], y);
    chmin(d[i], n - x - 1);
    chmin(d[i], n - y - 1);
  }
  int ans = 0;
  rep(i, n * n) {
    int p;
    cin >> p;
    p--;
    ans += d[p];
    k[p] = false;
    queue<int> q;
    q.emplace(p);
    while (!q.empty()) {
      int nowp = q.front();
      q.pop();
      int x = nowp % n;
      int y = nowp / n;
      rep(j, 4) {
        int nx = x + dx[j];
        int ny = y + dy[j];
        int np = ny * n + nx;
        if (nx < 0 || nx >= n || ny < 0 || ny >= n)
          continue;
        if (d[np] > d[nowp] + k[nowp]) {
          d[np] = d[nowp] + k[nowp];
          q.emplace(np);
        }
      }
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  const int dx[4] = {1, -1, 0, 0};
  const int dy[4] = {0, 0, 1, -1};
  const int INF = INT32_MAX >> 2;
  vector<bool> k(n, true);
  vector<int> d(n * n, INF);
  rep(i, n * n) {
    int x = i % n;
    int y = i / n;
    chmin(d[i], x);
    chmin(d[i], y);
    chmin(d[i], n - x - 1);
    chmin(d[i], n - y - 1);
  }
  int ans = 0;
  rep(i, n * n) {
    int p;
    cin >> p;
    p--;
    ans += d[p];
    k[p] = false;
    queue<int> q;
    q.emplace(p);
    while (!q.empty()) {
      int nowp = q.front();
      q.pop();
      int x = nowp % n;
      int y = nowp / n;
      rep(j, 4) {
        int nx = x + dx[j];
        int ny = y + dy[j];
        int np = ny * n + nx;
        if (nx < 0 || nx >= n || ny < 0 || ny >= n)
          continue;
        if (d[np] > d[nowp] + k[nowp]) {
          d[np] = d[nowp] + k[nowp];
          q.emplace(np);
        }
      }
    }
  }
  cout << ans << endl;
}