alexjercan/bugnet huggingface dataset:
Problem p02670 (C++) — buggy submission, verdict: Runtime Error

#ifdef DEBUG
#include "redirect.h"
#endif

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

int f[N][N], g[N][N], n;

int upd(int i, int j) {
  int tmp = f[i][j];
  if (i == 0 || i == n + 1 || j == 0 || j == n + 1)
    return 0;
  f[i][j] = min(f[i][j], f[i - 1][j] + g[i - 1][j]);
  f[i][j] = min(f[i][j], f[i + 1][j] + g[i + 1][j]);
  f[i][j] = min(f[i][j], f[i][j - 1] + g[i][j - 1]);
  f[i][j] = min(f[i][j], f[i][j + 1] + g[i][j + 1]);
  if (f[i][j] < tmp)
    return 1;
  else
    return 0;
}

void dfs(int i, int j) {
  if (upd(i - 1, j))
    dfs(i - 1, j);
  if (upd(i + 1, j))
    dfs(i + 1, j);
  if (upd(i, j - 1))
    dfs(i, j - 1);
  if (upd(i, j + 1))
    dfs(i, j + 1);
}

int main(int argc, char *argv[]) {
#ifdef DEBUG
  init(argc, argv);
#endif
  register int i, j, k, p, ans = 0;
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++)
      f[i][j] = N, g[i][j] = 1;
  for (k = 1; k <= n; k++) {
    for (i = 1; i <= n; i++)
      for (j = 1; j <= n; j++)
        upd(i, j);
  }

  for (k = 1; k <= n * n; k++) {
    scanf("%d", &p);
    p--;
    i = p / n + 1;
    j = p % n + 1;
    ans += f[i][j];
    g[i][j] = 0;
    dfs(i, j);
  }
  printf("%d", ans);
  return 0;
}


Error: WA

Fixed submission:

#ifdef DEBUG
#include "redirect.h"
#endif

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

int f[N][N], g[N][N], n;

inline int upd(int i, int j) {
  int tmp = f[i][j];
  if (i == 0 || i == n + 1 || j == 0 || j == n + 1)
    return 0;
  f[i][j] = min(f[i][j], f[i - 1][j] + g[i - 1][j]);
  f[i][j] = min(f[i][j], f[i + 1][j] + g[i + 1][j]);
  f[i][j] = min(f[i][j], f[i][j - 1] + g[i][j - 1]);
  f[i][j] = min(f[i][j], f[i][j + 1] + g[i][j + 1]);
  if (f[i][j] < tmp)
    return 1;
  else
    return 0;
}

int dfs(int i, int j) {
  if (upd(i - 1, j))
    dfs(i - 1, j);
  if (upd(i + 1, j))
    dfs(i + 1, j);
  if (upd(i, j - 1))
    dfs(i, j - 1);
  if (upd(i, j + 1))
    dfs(i, j + 1);
}

int main(int argc, char *argv[]) {
#ifdef DEBUG
  init(argc, argv);
#endif
  register int i, j, k, p, ans = 0;
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++)
      f[i][j] = N, g[i][j] = 1;
  for (k = 1; k <= n; k++) {
    for (i = 1; i <= n; i++)
      for (j = 1; j <= n; j++)
        upd(i, j);
  }

  for (k = 1; k <= n * n; k++) {
    scanf("%d", &p);
    p--;
    i = p / n + 1;
    j = p % n + 1;
    ans += f[i][j];
    g[i][j] = 0;
    dfs(i, j);
  }
  printf("%d", ans);
  return 0;
}
Problem p02670 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <ctype.h>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <stack>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <vector>

#define ll long long

using namespace std;

int v[505][505] = {0};
int h[505][505] = {0};
int n;
int diff[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

void dfs(int i, int j) {
  for (int k = 0; k < 4; k++) {
    int x = i + diff[k][0];
    int y = j + diff[k][1];

    if (v[x][y] - h[x][y] > v[i][j]) {
      v[x][y]--;
      dfs(x, y);
    }
  }
}

void solve() {
  cin >> n;

  int len = n + 2;
  for (int i = 0; i < len; i++)
    for (int j = 0; j < len; j++) {
      v[i][j] = min(min(i, len - 1 - i), min(j, len - 1 - j)) - 1;
      h[i][j] = 1;
    }

  int ans = 0;
  int p;
  for (int k = 0; k < n * n; k++) {
    cin >> p;

    int i = (p - 1) / n + 1;
    int j = (p - 1) % n + 1;
    ans += v[i][j];
    v[i][j]--;
    h[i][j] = 0;
    dfs(i, j);
  }

  cout << ans << endl;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);

  ll t = 1;
  // cin>>t;

  while (t > 0) {
    t--;
    solve();
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <ctype.h>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <stack>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <vector>

#define ll long long

using namespace std;

void solve() {
  int n;
  cin >> n;

  int v[505][505] = {0};
  int h[505][505] = {0};

  int len = n + 2;
  for (int i = 0; i < len; i++)
    for (int j = 0; j < len; j++) {
      v[i][j] = min(min(i, len - 1 - i), min(j, len - 1 - j)) - 1;
      h[i][j] = 1;
    }

  int ans = 0;
  int p;
  int diff[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
  for (int k = 0; k < n * n; k++) {
    cin >> p;

    int i = (p - 1) / n + 1;
    int j = (p - 1) % n + 1;
    ans += v[i][j];

    map<pair<int, int>, bool> vis;
    queue<pair<int, int>> data;
    data.push({i, j});
    vis[{i, j}] = true;
    h[i][j] = 0;

    while (data.size() > 0) {
      auto val = data.front();
      data.pop();
      v[val.first][val.second]--;

      for (int k = 0; k < 4; k++) {
        int x = val.first + diff[k][0];
        int y = val.second + diff[k][1];

        if (v[x][y] - 1 - h[x][y] == v[val.first][val.second] && !vis[{x, y}]) {
          vis[{x, y}] = true;
          data.push({x, y});
        }
      }
    }

    /*
    printf("(%d, %d) += %d\\n", i, j, ans);
    for (int i=0; i<n+2; i++) {
      for (int j=0; j<n+2; j++)
        printf("%4d", v[i][j]);
      cout<<endl;
    }
    */
  }

  cout << ans << endl;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);

  ll t = 1;
  // cin>>t;

  while (t > 0) {
    t--;
    solve();
  }

  return 0;
}
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 rrep(i, a, b) for (int i = (a); i >= (b); --i)
#define PB push_back
#define ar2 array<int, 2>
typedef long long LL;
const LL P = 998244353;
const int N = 505;
mt19937 rng(time(0));

int n, x, y, a[N * N], dp[N][N], v[N][N];
int dx[] = {-1, 0, 0, 1}, dy[] = {0, -1, 1, 0};

void update(int x, int y) {
  v[x][y] = 0;
  queue<ar2> Q;
  Q.push({x, y});
  while (!Q.empty()) {
    ar2 ele = Q.front();
    Q.pop();
    int x = ele[0], y = ele[1];
    rep(i, 0, 3) {
      int x1 = x + dx[i], y1 = y + dy[i];
      if (dp[x][y] + v[x][y] < dp[x1][y1]) {
        dp[x1][y1] = dp[x][y] + v[x][y];
        Q.push({x1, y1});
      }
    }
  }
}

int main() {
#ifndef ONLINE_JUDGE
// freopen("test.in","r",stdin);
#endif
  scanf("%d", &n);
  rep(i, 1, n * n) scanf("%d", a + i);

  rep(i, 1, n) rep(j, 1, n) dp[i][j] =
      min(min(i - 1, n - i), min(j - 1, n - j)),
                            v[i][j] = 1;

  int ans = 0;
  rep(q, 1, n * n) {
    x = a[q] - 1;
    y = x % n + 1, x = x / n + 1;

    ans += dp[x][y];
    update(x, y);
  }
  cout << ans << 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 rrep(i, a, b) for (int i = (a); i >= (b); --i)
#define PB push_back
#define ar2 array<int, 2>
typedef long long LL;
const LL P = 998244353;
const int N = 505;
mt19937 rng(time(0));

int n, x, y, a[N], dp[N][N], v[N][N];
int dx[] = {-1, 0, 0, 1}, dy[] = {0, -1, 1, 0};

void update(int x, int y) {
  v[x][y] = 0;
  queue<ar2> Q;
  Q.push({x, y});
  while (!Q.empty()) {
    ar2 ele = Q.front();
    Q.pop();
    int x = ele[0], y = ele[1];
    rep(i, 0, 3) {
      int x1 = x + dx[i], y1 = y + dy[i];
      if (dp[x][y] + v[x][y] < dp[x1][y1]) {
        dp[x1][y1] = dp[x][y] + v[x][y];
        Q.push({x1, y1});
      }
    }
  }
}

int main() {
#ifndef ONLINE_JUDGE
// freopen("test.in","r",stdin);
#endif
  scanf("%d", &n);
  rep(i, 1, n * n) scanf("%d", a + i);

  rep(i, 1, n) rep(j, 1, n) dp[i][j] =
      min(min(i - 1, n - i), min(j - 1, n - j)),
                            v[i][j] = 1;

  int ans = 0;
  rep(q, 1, n * n) {
    x = a[q] - 1;
    y = x % n + 1, x = x / n + 1;

    ans += dp[x][y];
    update(x, y);
  }
  cout << ans << endl;
  return 0;
}
Problem p02670 (C++) — buggy submission, verdict: Runtime Error

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

const int n = 500 + 10;

int h[n][n], a[n][n];

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

void dfs(int x, int y) {
  int z = h[x][y] + a[x][y];
  for (int k = 0; k < 4; k++) {
    int tx = x + dx[k], ty = y + dy[k];
    if (h[tx][ty] > z) {
      h[tx][ty] = z;
      dfs(tx, ty);
    }
  }
}

int main() {
  int n;
  scanf("%d", &n);

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      h[i][j] = min(min(i - 1, n - i), min(j - 1, n - j));
      a[i][j] = 1;
    }
  }

  int ans = 0;
  for (int i = 0; i < n * n; i++) {
    int pos;
    scanf("%d", &pos);
    int x = (pos - 1) / n + 1, y = (pos - 1) % n + 1;
    ans += h[x][y];
    a[x][y] = 0;
    dfs(x, y);
  }
  printf("%d\\n", ans);
}


Error: 0
sh: 1: pause: not found


Fixed submission:

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

const int N = 500 + 10;

int p[N * N], a[N][N], h[N][N], n;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

void dfs(int x, int y) {
  int cost = h[x][y] + a[x][y];
  for (int k = 0; k < 4; k++) {
    int tx = x + dx[k], ty = y + dy[k];
    if (h[tx][ty] > cost) {
      h[tx][ty] = cost;
      dfs(tx, ty);
    }
  }
}

int main() {
  scanf("%d", &n);
  int ans = 0;
  for (int i = 1; i <= n * n; i++) {
    for (int j = 1; j <= n * n; j++) {
      h[i][j] = min(min(i - 1, n - i), min(j - 1, n - j));
      a[i][j] = 1;
    }
  }
  for (int i = 1; i <= n * n; i++) {
    int pos;
    scanf("%d", &pos);
    int row = (pos - 1) / n + 1, col = (pos - 1) % n + 1;
    ans += h[row][col];
    a[row][col] = 0;
    dfs(row, col);
  }
  printf("%d\\n", ans);
#ifndef ONLINE_JUDGE
  system("pause");
#endif
  return 0;
}
Problem p02670 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;
typedef pair<int, int> P;
int n, ans, T[500][500], p;
bool ab[500][500];

int main() {
  P d[4] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      T[i][j] = min(min(i, n - 1 - i), min(j, n - 1 - j));
  for (int i = 0; i < n * n; i++) {
    cin >> p;
    p--;
    int x = p / n, y = p % n;
    ans += T[x][y];
    ab[x][y] = true;
    queue<P> q;
    q.push({x, y});
    while (!q.empty()) {
      int vx = q.front().first, vy = q.front().second;
      q.pop();
      int a = T[vx][vy];
      if (!ab[vx][vy])
        a++; //! abなら(wx,wy)は(vx,vy)に嫌われる
      for (int r = 0; r < 4; r++) {
        int wx = vx + d[r].first, wy = vy + d[r].second;
        if (wx >= 0 && wx < n && wy >= 0 && wy < n &&
            T[wx][wy] > a) { // Tが更新されるときだけqにpushする
          T[wx][wy] = a;
          q.push({wx, wy});
        }
      }
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;
using Graph = vector<vector<int>>;

int main() {
  int n;
  cin >> n;
  int p[n * n + 1];
  Graph G(n * n + 1);
  for (int i = 1; i <= n * n; i++)
    cin >> p[i];
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < n; j++) {
      G[j * n + i].push_back(j * n + i + 1);
      G[j * n + i + 1].push_back(j * n + i);
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < n - 1; j++) {
      G[j * n + i].push_back((j + 1) * n + i);
      G[(j + 1) * n + i].push_back(j * n + i);
    }
  }
  for (int i = 1; i <= n; i++) {
    G[i].push_back(0);
    G[0].push_back(i);
    G[(n - 1) * n + i].push_back(0);
    G[0].push_back((n - 1) * n + i);
  }
  for (int i = 1; i < n - 1; i++) {
    G[i * n + 1].push_back(0);
    G[0].push_back(i * n + 1);
    G[i * n + n].push_back(0);
    G[0].push_back(i * n + n);
  }
  ll ans = 0;
  set<int> pass;
  pass.insert(0);
  for (int i = 1; i <= n * n; i++) {
    vector<int> dist(n * n + 1, -1);
    queue<int> que;
    dist[p[i]] = 0;
    que.push(p[i]);
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      for (int nv : G[v]) {
        if (dist[nv] != -1 && dist[nv] <= dist[v] + (!pass.count(nv)))
          continue;
        dist[nv] = dist[v] + (!pass.count(nv));
        que.push(nv);
      }
    }
    ans += dist[0];
    pass.insert(p[i]);
  }
  cout << ans << endl;
}
Problem p02670 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define N 505
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + c - '0';
    c = getchar();
  }
  return x * f;
}
int a[N][N], n, ans, p[N * N], vis[N][N];
const int d[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
inline bool check(int x, int y) { return x >= 1 && y >= 1 && x <= n && y <= n; }
void dfs(int x, int y) {
  for (int i = 0; i < 4; ++i) {
    int nx = x + d[i][0], ny = y + d[i][1];
    if (check(nx, ny) && a[nx][ny] > a[x][y] + (!vis[x][y])) {
      a[nx][ny] = a[x][y] + (!vis[x][y]);
      dfs(nx, ny);
    }
  }
}
int main() {
  n = read();
  for (int i = 1; i <= n * n; ++i) {
    p[i] = read();
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      a[i][j] = min(min(n - i, i - 1), min(n - j, j - 1));
    }
  }
  for (int i = 1; i <= n * n; ++i) {
    int x = (p[i] - 1) / n + 1, y = (p[i] - 1) % n + 1;
    ans += a[x][y];
    vis[x][y] = 1;
    for (int k = 0; k < 4; ++k) {
      int nx = x + d[k][0], ny = y + d[k][1];
      if (check(nx, ny) && a[nx][ny] > a[x][y]) {
        a[nx][ny] = a[x][y], dfs(nx, ny);
      }
    }
  }
  printf("%d\\n", ans);
  return 0;
}


Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define N 505
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + c - '0';
    c = getchar();
  }
  return x * f;
}
int a[N][N], n, ans, p[N], vis[N][N];
const int d[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
inline bool check(int x, int y) { return x >= 1 && y >= 1 && x <= n && y <= n; }
void dfs(int x, int y) {
  for (int i = 0; i < 4; ++i) {
    int nx = x + d[i][0], ny = y + d[i][1];
    if (check(nx, ny) && a[nx][ny] > a[x][y] + (!vis[x][y])) {
      a[nx][ny] = a[x][y] + (!vis[x][y]);
      dfs(nx, ny);
    }
  }
}
int main() {
  n = read();
  for (int i = 1; i <= n * n; ++i) {
    p[i] = read();
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      a[i][j] = min(min(n - i, i - 1), min(n - j, j - 1));
    }
  }
  for (int i = 1; i <= n * n; ++i) {
    int x = (p[i] - 1) / n + 1, y = (p[i] - 1) % n + 1;
    ans += a[x][y];
    vis[x][y] = 1;
    for (int k = 0; k < 4; ++k) {
      int nx = x + d[k][0], ny = y + d[k][1];
      if (check(nx, ny) && a[nx][ny] > a[x][y]) {
        a[nx][ny] = a[x][y], dfs(nx, ny);
      }
    }
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p02670 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;

int n;
ll dist[502][502];
bool used[502][502];

ll cnt[502][502] = {};
void upd(int x, int y, int z) {
  if (!used[x][y])
    z++;
  if (dist[x][y] <= z)
    return;
  // cnt[x][y]++;
  // if(x==40&&y==40)cerr<<dist[x][y]<<" "<<z<<endl;
  dist[x][y] = z;
  if (x > 0)
    upd(x - 1, y, z);
  if (x < n - 1)
    upd(x + 1, y, z);
  if (y > 0)
    upd(x, y - 1, z);
  if (y < n - 1)
    upd(x, y + 1, z);
}

int main() {
  static int p[250010];
  scanf("%d", &n);
  for (int i = 0; i < n * n; i++)
    scanf("%d", &p[i]);
  // n=500;
  // for(int i=0;i<n*n;i++)p[i]=i+1;
  // random_shuffle(p,p+(n*n));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      dist[i][j] = 100000;
      used[i][j] = false;
    }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      dist[i][j] = 1 + min(min(i, n - 1 - i), min(j, n - 1 - j));
    }

  ll ret = 0;
  for (int i = 0; i < n * n; i++) {
    int x = (p[i] - 1) / n;
    int y = (p[i] - 1) % n;
    ret += dist[x][y] - 1;
    used[x][y] = true;
    upd(x, y, dist[x][y] - 1);
    // cerr<<i<<" "<<ret<<endl;
  }
  /*for(int x=0;x<n;x++)for(int y=0;y<n;y++){
          cerr<<cnt[x][y]<<endl;
  }*/
  cout << ret << endl;
}


Error: TLE

Fixed submission:

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

typedef long long ll;

int n;
ll dist[502][502];
bool used[502][502];
void upd(int x, int y, int z) {
  if (!used[x][y])
    z++;
  if (dist[x][y] <= z)
    return;
  dist[x][y] = z;
  if (x > 0)
    upd(x - 1, y, z);
  if (x < n)
    upd(x + 1, y, z);
  if (y > 0)
    upd(x, y - 1, z);
  if (y < n)
    upd(x, y + 1, z);
}

int main() {
  static int p[250010];
  scanf("%d", &n);
  for (int i = 0; i < n * n; i++)
    scanf("%d", &p[i]);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      dist[i][j] = 100000;
      used[i][j] = false;
    }
  for (int i = 0; i < n; i++) {
    upd(0, i, 0);
    upd(n - 1, i, 0);
    upd(i, 0, 0);
    upd(i, n - 1, 0);
  }
  ll ret = 0;
  for (int i = 0; i < n * n; i++) {
    int x = (p[i] - 1) / n;
    int y = (p[i] - 1) % n;
    ret += dist[x][y] - 1;
    used[x][y] = true;
    upd(x, y, dist[x][y] - 1);
    // cerr<<i<<" "<<ret<<endl;
  }
  cout << ret << endl;
}
Problem p02670 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define pii std::pair<int, int>
#define mp std::make_pair
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int n, x[250005], y[250005], G[505][505], d[505][505];
int ans = 0;

/*

void bfs(int x,int y){
        std::deque<pii>q;
        q.push_back(mp(x,y));
        while (!q.empty()) {
                pii u = q.front(); q.pop_back();
                for (int dr=0;dr<4;++dr) {
                        int x1 = u.first + dx[dr];
                        int y1 = u.second + dy[dr];
                        if (x1 < 1 || x1 > n || y1 < 1 || y1 > n) continue;
                        if (d[x1][y1] > d[u.first][u.second] + G[x1][y1]) {
                                d[x1][y1] = d[u.first][u.second] + G[x1][y1];
                                if (G[x1][y1]) q.push_back(mp(x1,y1));
                                else q.push_front(mp(x1,y1));
                        }
                }
        }
}

*/

void dfs(int x, int y) {
  for (int dr = 0; dr < 4; ++dr) {
    int x1 = x + dx[dr];
    int y1 = y + dy[dr];
    if (x1 < 1 || x1 > n || y1 < 1 || y1 > n)
      continue;
    if (d[x1][y1] > d[x][y] + G[x1][y1]) {
      d[x1][y1] = d[x][y] + G[x1][y1];
      dfs(x1, y1);
    }
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n * n; ++i) {
    int p;
    scanf("%d", &p);
    x[i] = (p - 1) / n + 1;
    y[i] = (p - 1) % n + 1;
  }
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) {
      d[i][j] = std::min({i, j, n - i + 1, n - j + 1});
      G[i][j] = 1;
    }
  for (int i = 1; i <= n * n; i++) {
    d[x[i]][y[i]]--;
    G[x[i]][y[i]] = 0;
    ans += d[x[i]][y[i]];
    dfs(x[i], y[i]);
  }
  printf("%d", ans);
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define pii std::pair<int, int>
#define mp std::make_pair
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int n, x[505], y[505], G[505][505], d[505][505];
int ans = 0;

void bfs(int x, int y) {
  std::deque<pii> q;
  q.push_back(mp(x, y));
  while (!q.empty()) {
    pii u = q.front();
    q.pop_back();
    for (int dr = 0; dr < 4; ++dr) {
      int x1 = u.first + dx[dr];
      int y1 = u.second + dy[dr];
      if (x1 < 1 || x1 > n || y1 < 1 || y1 > n)
        continue;
      if (d[x1][y1] > d[u.first][u.second] + G[x1][y1]) {
        d[x1][y1] = d[u.first][u.second] + G[x1][y1];
        if (G[x1][y1])
          q.push_back(mp(x1, y1));
        else
          q.push_front(mp(x1, y1));
      }
    }
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n * n; ++i) {
    int p;
    scanf("%d", &p);
    x[i] = (p - 1) / n + 1;
    y[i] = (p - 1) % n + 1;
  }
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) {
      d[i][j] = std::min({i, j, n - i + 1, n - j + 1});
      G[i][j] = 1;
    }
  for (int i = 1; i <= n * n; i++) {
    d[x[i]][y[i]]--;
    G[x[i]][y[i]] = 0;
    ans += d[x[i]][y[i]];
    bfs(x[i], y[i]);
  }
  printf("%d", ans);
  return 0;
}
Problem p02670 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int n, a[250005], is[505][505];
int dx[4] = {0, -1, 0, 1}, dy[4] = {1, 0, -1, 0}, ans = 0;
int dis[505][505] = {0};
struct Pair {
  int x, y;
};
void bfs(int x, int y) {
  deque<Pair> q;
  q.push_back({x, y});
  while (!q.empty()) {
    Pair now = q.front();
    q.pop_front();
    for (int i = 0; i < 4; i++) {
      Pair tmp = {now.x + dx[i], now.y + dy[i]};
      if (tmp.x < 1 || tmp.y > n || tmp.y < 1 || tmp.x > n)
        continue;
      if (is[tmp.x][tmp.y]) {
        if (dis[tmp.x][tmp.y] <= dis[now.x][now.y])
          continue;
        q.push_front(tmp);
        dis[tmp.x][tmp.y] = dis[now.x][now.y];
      } else {
        if (dis[tmp.x][tmp.y] <= dis[now.x][now.y] + 1)
          continue;
        q.push_back(tmp);
        dis[tmp.x][tmp.y] = dis[now.x][now.y] + 1;
      }
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n * n; i++)
    scanf("%d", &a[i]);
  for (int i = 0; i <= n + 1; i++) {
    for (int j = 0; j <= n + 1; j++) {
      dis[i][j] = min(min(i - 1, n - i), min(j - 1, n - j));
    }
  }
  for (int i = 1; i <= n * n; i++) {
    int x = (a[i] - 1) / n + 1, y = (a[i] - 1) % n + 1;
    ans += dis[x][y], is[x][y] = 1;
    for (int j = 0; j < 4; j++)
      dis[x][y] = min(dis[x][y], dis[x + dx[j]][y + dy[j]]);
    // cout<<x<<' '<<y<<' '<<dis[x][y]<<endl;
    bfs(x, y);
    // for(int
    // j=0;j<4;j++)dis[x+dx[j]][y+dy[j]]=min(dis[x+dx[j]][y+dy[j]],dis[x][y]+1);
  }
  cout << ans;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, a[250005], is[505][505], v[505][505];
int dx[4] = {0, -1, 0, 1}, dy[4] = {1, 0, -1, 0}, ans = 0;
int dis[505][505] = {0};
struct Pair {
  int x, y;
};
int bfs(int x, int y, int g) {
  deque<Pair> q;
  q.push_back({x, y}), dis[x][y] = 0, v[x][y] = g;
  while (!q.empty()) {
    Pair now = q.front();
    q.pop_front();
    for (int i = 0; i < 4; i++) {
      Pair tmp = {now.x + dx[i], now.y + dy[i]};
      if (tmp.x < 1 || tmp.y > n || tmp.y < 1 || tmp.x > n)
        return dis[now.x][now.y];
      if (v[tmp.x][tmp.y] == g)
        continue;
      if (is[tmp.x][tmp.y]) {
        q.push_front(tmp);
        dis[tmp.x][tmp.y] = dis[now.x][now.y], v[tmp.x][tmp.y] = g;
      } else {
        q.push_back(tmp);
        dis[tmp.x][tmp.y] = dis[now.x][now.y] + 1, v[tmp.x][tmp.y] = g;
      }
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n * n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n * n; i++) {
    int x = (a[i] - 1) / n + 1, y = (a[i] - 1) % n + 1;
    ans += bfs(x, y, i), is[x][y] = 1;
  }
  cout << ans;
}