Problem p02670 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define ll long long
#define lst(x) x.size() - 1
#define ld long double
#define pr pair<ll, ll>
#define loop(i, n) for (int i = 0; i < n; ++i)
#define rep(i, x, n) for (int i = x; i <= n; ++i)
#define iteloop(type, data, name, it)                                          \\
  for (type<data>::iterator it = name.begin(); it != name.end(); ++it)

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using os =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int D = 5, D2 = 5e2 + 5;
const ll N = D2, M = 1e1 + 5, MOD = 1e9 + 7, OO = 1e9;
int arr[N * N], n;
int vec[D], vecSz;
void conn(int x) {
  vecSz = 0;
  if (x % n != 0)
    vec[vecSz++] = x + 1;
  if ((x - 1) % n != 0)
    vec[vecSz++] = x - 1;
  if (x + n <= n * n)
    vec[vecSz++] = x + n;
  if (x - n >= 1)
    vec[vecSz++] = x - n;
}
pair<int, int> getInd(int x) {
  int row = (x - 1) / n;
  int col = x - row * n - 1;
  return {row, col};
}
int ans[N][N];
bool taken[N][N];
int main() {
  cin.sync_with_stdio(false), cin.tie(0), cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("std/in.txt", "rt", stdin);
  // freopen("in.txt", "wt" , stdout);
#endif // ONLINE_JUDGE
  scanf("%d", &n);
  loop(i, n * n) { scanf("%d", &arr[i]); }
  loop(i, n) {
    loop(j, n) { ans[i][j] = min({i, j, n - i - 1, n - j - 1}); }
  }
  int res = 0;
  queue<int> q;
  loop(i, n * n) {
    int x = arr[i];
    auto p = getInd(x);
    taken[p.first][p.second] = true;
    int cost = ans[p.first][p.second];
    res += cost;
    conn(x);
    loop(k, vecSz) {
      int g = vec[k];
      auto pg = getInd(g);
      int &r = ans[pg.first][pg.second];
      if (r > cost) {
        r = cost;
        q.push(g);
      }
    }
    while (!q.empty()) {
      int cur = q.front();
      q.pop();
      p = getInd(cur);
      conn(cur);
      cost = ans[p.first][p.second] + !taken[p.first][p.second];
      loop(k, vecSz) {
        int g = vec[k];
        auto pg = getInd(g);
        int &r = ans[pg.first][pg.second];
        if (r > cost) {
          r = cost;
          q.push(g);
        }
      }
    }
  }
  printf("%d\\n", res);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define ll long long
#define lst(x) x.size() - 1
#define ld long double
#define pr pair<ll, ll>
#define loop(i, n) for (int i = 0; i < n; ++i)
#define rep(i, x, n) for (int i = x; i <= n; ++i)
#define iteloop(type, data, name, it)                                          \\
  for (type<data>::iterator it = name.begin(); it != name.end(); ++it)

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using os =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int D = 5, D2 = 5e2 + 5;
const ll N = D2, M = 1e1 + 5, MOD = 1e9 + 7, OO = 1e9;
int arr[N * N], n;
vector<int> conn(int x) {
  vector<int> res;
  if (x % n != 0)
    res.push_back(x + 1);
  if ((x - 1) % n != 0)
    res.push_back(x - 1);
  if (x + n <= n * n)
    res.push_back(x + n);
  if (x - n >= 1)
    res.push_back(x - n);
  return res;
}
pair<int, int> getInd(int x) {
  int row = (x - 1) / n;
  int col = x - row * n - 1;
  return {row, col};
}
int ans[N][N];
bool taken[N][N];
int main() {
  cin.sync_with_stdio(false), cin.tie(0), cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("std/in.txt", "rt", stdin);
  // freopen("in.txt", "wt" , stdout);
#endif // ONLINE_JUDGE
  scanf("%d", &n);
  loop(i, n * n) { scanf("%d", &arr[i]); }
  loop(i, n) {
    loop(j, n) { ans[i][j] = min({i, j, n - i - 1, n - j - 1}); }
  }
  int res = 0;
  queue<int> q;
  loop(i, n * n) {
    int x = arr[i];
    auto p = getInd(x);
    taken[p.first][p.second] = true;
    int cost = ans[p.first][p.second];
    res += cost;
    auto nbrs = conn(x);
    for (int g : nbrs) {
      auto pg = getInd(g);
      int &r = ans[pg.first][pg.second];
      if (r > cost) {
        r = cost;
        q.push(g);
      }
    }
    while (!q.empty()) {
      int cur = q.front();
      q.pop();
      p = getInd(cur);
      nbrs = conn(cur);
      cost = ans[p.first][p.second] + !taken[p.first][p.second];
      for (int g : nbrs) {
        auto pg = getInd(g);
        int &r = ans[pg.first][pg.second];
        if (r > cost) {
          r = cost;
          q.push(g);
        }
      }
    }
  }
  printf("%d\\n", res);
}