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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

typedef long long ll;

int main() {
  int N, ans = 0;
  cin >> N;
  vector<int> P(N * N), d(N * N);
  rep(i, N) {
    rep(j, N) {
      int k = i * N + j;
      cin >> P[k];
      P[k]--;
      d[k] = min({i, j, N - 1 - i, N - 1 - j});
    }
  }
  vector<bool> done(N * N, false);
  rep(k, N * N) {
    ans += d[P[k]];
    done[P[k]] = true;
    stack<pair<int, int>> q;
    q.push(make_pair(P[k], d[P[k]]));
    while (!q.empty()) {
      auto p = q.top();
      q.pop();
      int k0 = p.first, i0 = k0 / N, j0 = k0 % N, d0 = p.second;
      d[k0] = d0;
      int di[4] = {1, 0, -1, 0}, dj[4] = {0, 1, 0, -1};
      rep(l, 4) {
        int ni = i0 + di[l], nj = j0 + dj[l], nk = ni * N + nj;
        if (ni < 0 || ni >= N || nj < 0 || nj >= N)
          continue;
        if (done[k0]) {
          if (d[nk] > d0) {
            q.push(make_pair(nk, d0));
          }
        } else {
          if (d[nk] > d0 + 1) {
            q.push(make_pair(nk, d0 + 1));
          }
        }
      }
    }
  }
  cout << ans << "\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

typedef long long ll;

int main() {
  int N, ans = 0;
  cin >> N;
  int N2 = N * N;
  vector<int> P(N2), d(N2);
  rep(i, N) {
    rep(j, N) {
      int k = i * N + j;
      cin >> P[k];
      P[k]--;
      d[k] = min({i, j, N - 1 - i, N - 1 - j});
    }
  }
  vector<bool> done(N2, false);
  rep(i, N) {
    rep(j, N) {
      int k = i * N + j;
      // cout << d[P[k]] << " ";
      ans += d[P[k]];
      done[P[k]] = true;
      stack<vector<int>> q;
      q.push({P[k] / N, P[k] % N, d[P[k]]});
      while (!q.empty()) {
        auto p = q.top();
        q.pop();
        int i0 = p[0], j0 = p[1], d0 = p[2], k0 = i0 * N + j0;
        d[k0] = d0;
        // cout << "d[" << k0 << "]=" << d[k0] << "\\n";
        int di[4] = {1, 0, -1, 0}, dj[4] = {0, 1, 0, -1};
        rep(l, 4) {
          int ni = i0 + di[l], nj = j0 + dj[l], nk = ni * N + nj;
          if (ni < 0 || ni >= N || nj < 0 || nj >= N)
            continue;
          if (done[k0]) {
            if (d[nk] > d0) {
              q.push({ni, nj, d0});
            }
          } else {
            if (d[nk] > d0 + 1) {
              q.push({ni, nj, d0 + 1});
            }
          }
        }
      }
      /*
      rep(x, N) {
          rep(y, N) cout << d[x * N + y];
          cout << "\\n";
      }
      cout << "\\n";
      */
    }
  }
  cout << ans << "\\n";
}
Problem p02670 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int N;
vector<int> P(500 * 500 + 5);
bool a[505][505];
int d[505][505];

void dfs(int i, int j, int dst) {
  if (dst >= d[i][j])
    return;
  d[i][j] = dst;
  if (i > 0)
    dfs(i - 1, j, dst + (1 - a[i - 1][j]));
  if (i < N - 1)
    dfs(i + 1, j, dst + (1 - a[i + 1][j]));
  if (j > 0)
    dfs(i, j - 1, dst + (1 - a[i][j - 1]));
  if (j < N - 1)
    dfs(i, j + 1, dst + (1 - a[i][j + 1]));
}

void print() {
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cout << d[i][j] << " ";
    }
    cout << "\\n";
  }
}

int main() {

  cin >> N;
  for (int i = 0; i < N * N; i++)
    cin >> P[i];

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      d[i][j] = min(min(i, N - i - 1), min(j, N - j - 1)) + 1;
    }
  }

  int s = 0;

  for (int r = 0; r < N * N; r++) {
    int i = (P[r] - 1) / N;
    int j = (P[r] - 1) % N;
    a[i][j] = true;
    int dst = d[i][j] - 1;
    if (i > 0)
      dst = min(dst, d[i - 1][j]);
    if (i < N - 1)
      dst = min(dst, d[i + 1][j]);
    if (j > 0)
      dst = min(dst, d[i][j - 1]);
    if (j < N - 1)
      dst = min(dst, d[i][j + 1]);
    s += dst;
    // cout << r+1 << " " << i << " " << j << " " << dst << "\\n";
    dfs(i, j, dst);
    // if (r == 7) print();
  }

  cout << s << "\\n";

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int N;
vector<int> P(505);
bool a[505][505];
int d[505][505];

void dfs(int i, int j, int dst) {
  if (dst >= d[i][j])
    return;
  d[i][j] = dst;
  if (i > 0)
    dfs(i - 1, j, dst + (1 - a[i - 1][j]));
  if (i < N - 1)
    dfs(i + 1, j, dst + (1 - a[i + 1][j]));
  if (j > 0)
    dfs(i, j - 1, dst + (1 - a[i][j - 1]));
  if (j < N - 1)
    dfs(i, j + 1, dst + (1 - a[i][j + 1]));
}

void print() {
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cout << d[i][j] << " ";
    }
    cout << "\\n";
  }
}

int main() {

  cin >> N;
  for (int i = 0; i < N * N; i++)
    cin >> P[i];

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      d[i][j] = min(min(i, N - i - 1), min(j, N - j - 1)) + 1;
    }
  }

  int s = 0;

  for (int r = 0; r < N * N; r++) {
    int i = (P[r] - 1) / N;
    int j = (P[r] - 1) % N;
    a[i][j] = true;
    int dst = d[i][j] - 1;
    if (i > 0)
      dst = min(dst, d[i - 1][j]);
    if (i < N - 1)
      dst = min(dst, d[i + 1][j]);
    if (j > 0)
      dst = min(dst, d[i][j - 1]);
    if (j < N - 1)
      dst = min(dst, d[i][j + 1]);
    s += dst;
    // cout << r+1 << " " << i << " " << j << " " << dst << "\\n";
    dfs(i, j, dst);
    // if (r == 7) print();
  }

  cout << s << "\\n";

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

#include <bits/stdc++.h>

#define ll long long
#define fornum(A, B, C) for (A = B; A < C; ++A)
#define pii pair<int, int>
#define pll pair<ll, ll>

using namespace std;

/////////////////////////////////////////////////////

ll N, P[1000 * 1000];

ll mk[1000 * 1000];
ll mk2[1000 * 1000];
ll g[8];
ll i, j, k;
int main() {
  scanf("%lld", &N);
  fornum(i, 0, N * N) {
    scanf("%lld", &P[i]);
    P[i]--;
  }
  fornum(i, 0, N) {
    fornum(j, 0, N) {
      mk[i * N + j] = min(min(i, N - i - 1), min(j, N - j - 1));
    }
  }
  g[0] = 1;
  g[1] = N;
  g[2] = -1;
  g[3] = -N;
  ll ans = 0;
  fornum(i, 0, N * N) {
    // printf("%lld ", P[i]);
    ans += mk[P[i]];
    mk2[P[i]] = 1;
    queue<ll> q;
    q.push(P[i]);
    // printf("\\n%lld:\\n", P[i]);
    while (q.size()) {
      ll qa = q.front();
      // printf("%lld,", qa);
      q.pop();
      fornum(j, 0, 4) {
        ll nx = qa + g[j];
        if (nx < 0 || N * N <= nx || (qa % N != nx % N && qa / N != nx / N))
          continue;
        // printf("%lld,%lld,%lld,%lld\\n", qa, nx, mk[qa] + 1 - mk2[qa],
        // mk[nx]);
        if (mk[qa] + 1 - mk2[qa] < mk[nx]) {
          mk[nx] = mk[qa] + 1 - mk2[qa];
          q.push(nx);
        }
      }
    }
  }
  printf("%lld\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define ll long long
#define fornum(A, B, C) for (A = B; A < C; ++A)
#define pii pair<int, int>
#define pll pair<ll, ll>

using namespace std;

/////////////////////////////////////////////////////

struct UnionFind {
  vector<int> nxt;
  vector<int> numm;
  void init(int x) {
    nxt.clear();
    numm.assign(x, 1);
    nxt.resize(x);
    for (int i = 0; i < x; i++) {
      nxt[i] = i;
    }
  }
  int find(int x) {
    if (nxt[x] == x)
      return x;
    return nxt[x] = find(nxt[x]);
  }
  int size(int x) { return numm[x]; }
  inline void unite(int x, int y) {
    if (x == y)
      return;
    numm[find(x)] += numm[find(y)];
    nxt[find(y)] = find(x);
  }
};

ll N, P[1000 * 1000];
UnionFind uf;

ll mk[1000 * 1000];
ll mk2[1000 * 1000];
ll g[8];
ll i, j, k;
int main() {
  scanf("%lld", &N);
  fornum(i, 0, N * N) {
    scanf("%lld", &P[i]);
    P[i]--;
  }
  fornum(i, 0, N) {
    fornum(j, 0, N) {
      mk[i * N + j] = min(min(i, N - i - 1), min(j, N - j - 1));
    }
  }
  g[0] = 1;
  g[1] = N;
  g[2] = -1;
  g[3] = -N;
  uf.init(N * N + 5);
  ll ans = 0;
  fornum(i, 0, N * N) {
    fornum(j, 0, 4) {
      ll nx = P[i] + g[j];
      if (nx < 0 && N * N <= nx)
        continue;
      if (!mk2[nx])
        continue;
      mk[P[i]] = min(mk[P[i]], mk[uf.find(nx)]);
      uf.unite(P[i], nx);
    }
    ans += mk[uf.find(P[i])];
    mk2[P[i]] = 1;
  }
  printf("%lld\\n", ans);
  return 0;
}
Problem p02670 (C++) — buggy submission, verdict: Runtime Error

#ifndef Local
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx")
#endif

#include <bits/stdc++.h>

#include <ext/numeric>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
using namespace std;

#define popCnt(x) (__builtin_popcountll(x))

typedef long long Long;
const int N = 505;
int dist[N][N];
bool vis[N][N];

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

void dfs(int i, int j, int d) {
  if (d >= dist[i][j])
    return;
  dist[i][j] = d;
  d += !vis[i][j];
  for (int x = 0; x < 4; ++x) {
    dfs(i + dx[x], j + dy[x], d);
  }
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifdef Local
  freopen("test.in", "r", stdin);
#else
#define endl '\\n'
#endif

  int n;
  cin >> n;

  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      dist[i][j] = min(min(i - 1, j - 1), min(n - i, n - j));
    }
  }
  for (int i = 0; i <= n + 1; ++i) {
    vis[0][i] = vis[n + 1][i] = vis[i][0] = vis[i][n + 1] = true;
  }
  int res = 0;
  for (int i = 1; i <= n * n; ++i) {
    int c;
    cin >> c;
    --c;
    int x = c / n + 1, y = c % n + 1;
    res += dist[x][y];
    vis[x][y] = true;
    for (int d = 0; d < 4; ++d) {
      dfs(x + dx[d], y + dy[d], dist[x][y]);
    }
  }
  cout << res << endl;

  return 0;
}

Error: 0

Fixed submission:

#ifndef Local
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx")
#endif

#include <bits/stdc++.h>

#include <ext/numeric>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
using namespace std;

#define popCnt(x) (__builtin_popcountll(x))

typedef long long Long;
const int N = 501;
int dist[N][N];
bool vis[N][N];

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

void dfs(int i, int j, int d) {
  if (d >= dist[i][j])
    return;
  dist[i][j] = d;
  d += !vis[i][j];
  for (int x = 0; x < 4; ++x) {
    dfs(i + dx[x], j + dy[x], d);
  }
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifdef Local
  freopen("test.in", "r", stdin);
#else
#define endl '\\n'
#endif

  int n;
  cin >> n;

  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      dist[i][j] = min(min(i - 1, j - 1), min(n - i, n - j));
    }
  }
  for (int i = 0; i <= n + 1; ++i) {
    vis[0][i] = vis[n + 1][i] = vis[i][0] = vis[i][n + 1] = true;
  }
  int res = 0;
  for (int i = 1; i <= n * n; ++i) {
    int c;
    cin >> c;
    --c;
    int x = c / n + 1, y = c % n + 1;
    res += dist[x][y];
    vis[x][y] = true;
    for (int d = 0; d < 4; ++d) {
      dfs(x + dx[d], y + dy[d],
          dist[x + dx[d ^ 1]][y + dy[d ^ 1]] +
              !vis[x + dx[d ^ 1]][y + dy[d ^ 1]]);
    }
  }
  cout << res << endl;

  return 0;
}
Problem p02670 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int dist[505][505], n, p[250005];
bool f[505][505];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int cnt;
inline void Min(int &x, int y) {
  if (x > y)
    x = y;
}
inline void dfs(int x, int y) {
  for (int d = 0; d < 4; ++d) {
    int tx = x + dx[d], ty = y + dy[d];
    if (tx < 1 or tx > n or ty < 1 or ty > n)
      continue;
    if (dist[tx][ty] > dist[x][y] + f[x][y]) {
      dist[tx][ty] = dist[x][y] + f[x][y];
      dfs(tx, ty);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      f[i][j] = 1, dist[i][j] = min(min(i - 1, n - i), min(j - 1, n - j));
  for (int i = 1; i <= n * n; ++i) {
    cin >> p[i];
    int x = (p[i] - 1) / n + 1, y = (p[i] - 1) % n + 1;
    cnt += dist[x][y];
    f[x][y] = 0;
    dfs(x, y);
  }
  cout << cnt << '\\n';
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int dist[505][505], n, p[250005];
bool f[505][505];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int cnt;
int col[505][505], ncol;
inline void go(int vv) {
  int x = (int)((vv - 1) / n) + 1, y = (vv - 1) % n + 1;
  f[x][y] = 0;
  ++ncol;
  priority_queue<pair<int, pair<int, int>>> pq;
  pq.push({0, {x, y}});
  dist[x][y] = 0;
  for (; pq.size();) {
    pair<int, pair<int, int>> p = pq.top();
    pq.pop();
    x = p.second.first, y = p.second.second;
    int d = -p.first;
    if (x == 1 or x == n or y == 1 or y == n) {
      cnt += d;
      return;
    }
    if (dist[x][y] != d)
      continue;
    for (int i = 0; i < 4; ++i) {
      int tx = x + dx[i], ty = y + dy[i];
      if (tx < 1 or tx > n or ty < 1 or ty > n)
        continue;
      if (dist[tx][ty] > dist[x][y] + f[tx][ty] or col[tx][ty] != ncol) {
        col[tx][ty] = ncol;
        dist[tx][ty] = dist[x][y] + f[tx][ty];
        pq.push({-dist[tx][ty], {tx, ty}});
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      f[i][j] = 1;
  for (int i = 1; i <= n * n; ++i) {
    cin >> p[i];
    go(p[i]);
  }
  cout << cnt << '\\n';
}
Problem p02670 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int a[555][555], b[555][555];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      a[i][j] = min({i, n - i - 1, j, n - j - 1});
  long r = 0;
  for (int k = 0; k < n * n; k++) {
    int p;
    scanf("%d", &p);
    p--;
    r += a[p / n][p % n];
    b[p / n][p % n]++;
    queue<pair<int, int>> q;
    q.emplace(p / n, p % n);
    while (!q.empty()) {
      auto [i, j] = q.front();
      q.pop();
      if (i && a[i - 1][j] > a[i][j] + 1 - b[i][j])
        a[i - 1][j] = a[i][j] + 1 - b[i][j], q.emplace(i - 1, j);
      if (i + 1 < n && a[i + 1][j] > a[i][j] + 1 - b[i][j])
        a[i + 1][j] = a[i][j] + 1 - b[i][j], q.emplace(i + 1, j);
      if (j && a[i][j - 1] > a[i][j] + 1 - b[i][j])
        a[i][j - 1] = a[i][j] + 1 - b[i][j], q.emplace(i, j - 1);
      if (j + 1 < n && a[i][j + 1] > a[i][j] + 1 - b[i][j])
        a[i][j + 1] = a[i][j] + 1 - b[i][j], q.emplace(i, j + 1);
    }
  }
  cout << r << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int a[555][555], b[555][555], d[555][555];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      a[i][j] = min({i, n - i - 1, j, n - j - 1});
  long r = 0;
  for (int k = 0; k < n * n; k++) {
    int p;
    scanf("%d", &p);
    p--;
    r += a[p / n][p % n];
    b[p / n][p % n]++;
    queue<pair<int, int>> q;
    q.emplace(p / n, p % n);
    for (int i = 0; i < n; i++)
      fill(d[i], d[i] + n, 0);
    d[p / n][p % n] = 1;
    while (!q.empty()) {
      auto [i, j] = q.front();
      q.pop();
      if (i && !d[i - 1][j] && a[i - 1][j] > a[i][j] + 1 - b[i][j])
        d[i - 1][j] = 1, a[i - 1][j] = a[i][j] + 1 - b[i][j],
              q.emplace(i - 1, j);
      if (i + 1 < n && !d[i + 1][j] && a[i + 1][j] > a[i][j] + 1 - b[i][j])
        d[i + 1][j] = 1, a[i + 1][j] = a[i][j] + 1 - b[i][j],
              q.emplace(i + 1, j);
      if (j && !d[i][j - 1] && a[i][j - 1] > a[i][j] + 1 - b[i][j])
        d[i][j - 1] = 1, a[i][j - 1] = a[i][j] + 1 - b[i][j],
                 q.emplace(i, j - 1);
      if (j + 1 < n && !d[i][j + 1] && a[i][j + 1] > a[i][j] + 1 - b[i][j])
        d[i][j + 1] = 1, a[i][j + 1] = a[i][j] + 1 - b[i][j],
                 q.emplace(i, j + 1);
    }
  }
  cout << r << endl;
  return 0;
}
Problem p02670 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using UL = unsigned int;
using ULL = unsigned long long;
using LL = long long;
#define rep(i, n) for (UL i = 0; i < (n); i++)

struct Problem {
public:
  UL G[502][502];
  bool Save[502][502];
  bool S[502][502] = {};

  void BFS(UL x, UL y, UL d = 0) {
    queue<pair<pair<UL, UL>, UL>> Q;
    Q.push({{x, y}, d});
    while (Q.size()) {
      UL x = Q.front().first.first;
      UL y = Q.front().first.second;
      UL d = Q.front().second;
      Q.pop();
      if (G[x][y] <= d && Save[x][y] == S[x][y])
        continue;
      G[x][y] = d;
      Save[x][y] = S[x][y];
      if (S[x][y])
        d++;
      Q.push({{x - 1, y}, d});
      Q.push({{x + 1, y}, d});
      Q.push({{x, y - 1}, d});
      Q.push({{x, y + 1}, d});
    }
  }

  void Solve() {
    UL N;
    scanf("%d", &N);

    rep(i, N) rep(j, N) S[i + 1][j + 1] = true;
    rep(i, N + 2) rep(j, N + 2) {
      G[i][j] = 10000;
      Save[i][j] = S[i][j];
    }
    rep(i, N + 2) {
      G[0][i] = G[N + 1][i] = 0;
      G[i][0] = G[i][N + 1] = 0;
    }
    rep(x, N) rep(y, N) {
      UL tmp = 10000;
      tmp = min(tmp, x);
      tmp = min(tmp, y);
      tmp = min(tmp, N - 1 - x);
      tmp = min(tmp, N - 1 - y);
      G[x + 1][y + 1] = tmp;
    }

    UL ans = 0;

    rep(i, N * N) {
      UL P;
      scanf("%d", &P);
      UL x = (P - 1) % N + 1;
      UL y = (P - 1) / N + 1;
      UL d = G[x][y];
      ans += G[x][y];
      S[x][y] = false;
      BFS(x, y, d);
    }

    printf("%d", ans);
  }
};

int main() {
  unique_ptr<Problem> p(new Problem());
  p->Solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using UL = unsigned int;
using ULL = unsigned long long;
using LL = long long;
#define rep(i, n) for (UL i = 0; i < (n); i++)

struct Problem {
public:
  UL G[502][502];
  bool Save[502][502];
  bool S[502][502] = {};

  void DFS(UL x, UL y, UL d = 0) {
    if (G[x][y] <= d && Save[x][y] == S[x][y])
      return;
    G[x][y] = d;
    Save[x][y] = S[x][y];
    if (S[x][y])
      d++;
    DFS(x - 1, y, d);
    DFS(x + 1, y, d);
    DFS(x, y - 1, d);
    DFS(x, y + 1, d);
  }

  void Solve() {
    UL N;
    scanf("%d", &N);

    rep(i, N) rep(j, N) S[i + 1][j + 1] = true;
    rep(i, N + 2) rep(j, N + 2) {
      G[i][j] = 10000;
      Save[i][j] = S[i][j];
    }
    rep(i, N + 2) {
      G[0][i] = G[N + 1][i] = 0;
      G[i][0] = G[i][N + 1] = 0;
    }
    rep(i, N) {
      DFS(1, i + 1);
      DFS(N, i + 1);
      DFS(i + 1, 1);
      DFS(i + 1, N);
    }

    UL ans = 0;

    rep(i, N * N) {
      UL P;
      scanf("%d", &P);
      UL x = (P - 1) % N + 1;
      UL y = (P - 1) / N + 1;
      UL d = G[x][y];
      ans += G[x][y];
      S[x][y] = false;
      DFS(x, y, d);
    }

    printf("%d", ans);
  }
};

int main() {
  unique_ptr<Problem> p(new Problem());
  p->Solve();
  return 0;
}
Problem p02670 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fs first
#define sc second
#define task "test"
using namespace std;
const ll N = 500 + 9;
const ll inf = 1e9 + 7;
typedef pair<ll, ll> LL;
ll dx[4] = {1, -1, 0, 0};
ll dy[4] = {0, 0, 1, -1};
ll n, a[N * N], i, lab[N][N], j, minn;
bool chk[N][N];
bool check(ll x, ll y) { return (x <= n && y <= n && x >= 1 && y >= 1); }
ll trans(ll x, ll y) { return (x - 1) * n + y; }
void dfs(ll x, ll y) {
  ll z = lab[x][y] + chk[x][y];
  for (ll i = 0; i < 4; i++) {
    ll u = x + dx[i], v = y + dy[i];
    if (check(u, v) && lab[u][v] > z) {
      lab[u][v] = z;
      dfs(u, v);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  if (fopen(task ".inp", "r")) {
    freopen(task ".inp", "r", stdin);
    // freopen(task".out", "w", stdout);
  }
  cin >> n;
  for (i = 1; i <= n * n; i++)
    cin >> a[i];
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      chk[i][j] = true;
      ll x = min(min(abs(i - 1), abs(i - n)), min(abs(j - 1), abs(j - n)));
      lab[i][j] = x;
    }
  }
  ll ans = 0;
  for (i = 1; i <= n * n; i++) {
    ll p = a[i];
    ll x = ceil((ld)p / n), y = p - (x - 1) * n;
    chk[x][y] = false;
    ans += lab[x][y];
    dfs(x, y);
  }
  cout << ans;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fs first
#define sc second
#define task "tst"
using namespace std;
const ll N = 500 + 9;
const ll inf = 1e9 + 7;
typedef pair<ll, ll> LL;
ll dx[4] = {1, -1, 0, 0};
ll dy[4] = {0, 0, 1, -1};
ll n, a[N], i, lab[N * N], j, minn;
bool chk[N * N];
ll Find(ll u) {
  if (lab[u] < 0)
    return u;
  return lab[u] = Find(lab[u]);
}
void Union(ll r, ll s) {
  lab[s] = max(lab[s], lab[r]);
  lab[r] = s;
}
bool check(ll x, ll y) { return (x <= n && y <= n && x >= 1 && y >= 1); }
ll trans(ll x, ll y) { return (x - 1) * n + y; }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  if (fopen(task ".inp", "r")) {
    freopen(task ".inp", "r", stdin);
    // freopen(task".out", "w", stdout);
  }
  cin >> n;
  for (i = 1; i <= n * n; i++)
    cin >> a[i];
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      ll x = min(min(abs(i - 1), abs(i - n)), min(abs(j - 1), abs(j - n)));
      lab[trans(i, j)] = -x - 1;
    }
  }
  // cout<<lab[6]<<"\\n";
  ll ans = 0;

  for (i = 1; i <= n * n; i++) {
    ll p = a[i];
    minn = -1 * (lab[Find(p)] + 1);
    chk[a[i]] = true;
    ll x = ceil((ld)p / n), y = p - (x - 1) * n;
    for (j = 0; j < 4; j++) {
      ll u = x + dx[j], v = y + dy[j];
      if (check(u, v) && chk[trans(u, v)]) {
        ll r = Find(p), s = Find(trans(u, v));
        if (r != s)
          Union(r, s);
        minn = min(minn, -1 * (lab[Find(p)] + 1));
      }
    }
    // cout<<minn<<"\\n";
    ans += minn;
  }
  cout << ans;
}
Problem p02670 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define F first
#define S second
#define mp make_pair
#define vi vector<int>
#define pb push_back
using namespace std;
const int N = 505;
bool f[N][N];
int n, ans, dist[N][N], cnt;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
inline bool ok(int x, int y) { return (x >= 1 && y >= 1 && x <= n && y <= n); }
void dfs(int x, int y) {
  if (!dist[x][y])
    return;
  dist[x][y]--;
  for (int i = 0; i < 4; i++) {
    int x2 = x + dx[i];
    int y2 = y + dy[i];
    if (ok(x2, y2) && dist[x][y] + 1 + f[x2][y2] == dist[x2][y2]) {
      dfs(x2, y2);
    }
  }
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      dist[i][j] = min(min(i - 1, n - i), min(j - 1, n - j)) + 1,
      f[i][j] = true;
  for (int i = 0; i < n * n; i++) {
    cnt++;
    int x;
    cin >> x;
    int a = x / n + 1 - (x % n == 0), b = x % n;
    if (b == 0)
      b = n;
    dfs(a, b);
    ans += dist[a][b];
    f[a][b] = false;
  }
  cout << ans << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define F first
#define S second
#define mp make_pair
#define vi vector<int>
#define pb push_back
using namespace std;
const int N = 505;
bool f[N][N];
int n, ans, vis[N][N], cnt;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
deque<int> q;
int main() {
  cin >> n;
  for (int i = 0; i < n * n; i++) {
    cnt++;
    int x;
    cin >> x;
    int a = x / n + 1 - (x % n == 0), b = x % n;
    if (b == 0)
      b = n;
    q.push_back(a);
    q.push_back(b);
    q.push_back(0);
    f[a][b] = 1;

    while (!q.empty()) {
      a = q.front();
      q.pop_front();
      b = q.front();
      q.pop_front();
      int move = q.front();
      q.pop_front();
      //			cout<<a<<" "<<b<<" "<<move<<"\\n";
      bool flag = false;
      vis[a][b] = cnt;
      for (int i = 0; i < 4; i++) {
        int A = a + dx[i];
        int B = b + dy[i];
        if (vis[A][B] == cnt)
          continue;
        if ((A >= 1 && B >= 1 && A <= n && B <= n)) {
          vis[A][B] = cnt;
          if (f[A][B]) {
            q.push_front(move);
            q.push_front(B);
            q.push_front(A);
          } else {
            q.push_back(A);
            q.push_back(B);
            q.push_back(move + 1);
          }
        } else {
          flag = true;
          ans += move;
          break;
        }
      }
      if (flag)
        break;
    }
    q.clear();
  }
  cout << ans << "\\n";
  return 0;
}