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

#include <bits/stdc++.h>
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
#define lli long long int
#define uli unsigned long long int
#define INF 9999999999
#define rep(i, m, n) for (lli i = m; i < n; i++)
#define rrep(i, m, n) for (lli i = m - 1; i >= n; i--)
#define pb(n) push_back(n)
#define UE(N) N.erase(unique(N.begin(), N.end()), N.end());
#define Sort(n) sort(n.begin(), n.end())
#define Rev(n) reverse(n.begin(), n.end())
#define Out(S) cout << S << endl
#define NeOut(S) cout << S
#define HpOut(S) cout << setprecision(25) << S << endl
#define Vec(K, L, N, S) vector<L> K(N, S)
#define DV(K, L, N, M, S) vector<vector<L>> K(N, vector<L>(M, S))
#define TV(K, L, N, M, R, S)                                                   \\
  vector<vector<vector<L>>> K(N, vector<vector<L>>(M, vector<L>(R, S)))
#define pint pair<lli, lli>
#define paf(L, R) pair<L, R>
#define mod 998244353
#define MAX 10000000
#define ALL(a) a.begin(), a.end()
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  lli N, A, B, C, D, E, F, M, K, L, X, Y, Z, H, W, sum = 0, num = 0, flag = 0;
  string S, T;
  cin >> N;
  lli d[4] = {-1, 1, N, 0 - N};
  Vec(P, lli, N * N, 0);
  Vec(G, lli, N * N, 0);
  Vec(Fl, bool, N *N, 1);
  rep(i, 0, N * N) cin >> P[i];
  Vec(dist, lli, N * N, 0);
  rep(i, 0, N * N) {
    A = i % N;
    B = i / N;
    dist[i] = min(min(A, N - A - 1), min(B, N - B - 1));
  }
  rep(i, 0, N * N) {
    sum += dist[P[i] - 1];
    queue<lli> que;
    que.push(P[i] - 1);
    Fl[P[i] - 1] = 0;
    while (!que.empty()) {
      auto v = que.front();
      que.pop();
      rep(j, 0, 4) {
        auto nv = v + d[j];
        if (0 > nv || nv >= N * N)
          continue;
        if (dist[nv] <= dist[v] + Fl[v])
          continue;
        dist[nv] = dist[v] + Fl[v];
        que.push(nv);
      }
    }
  }
  Out(sum);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
#define lli long long int
#define uli unsigned long long int
#define INF 9999999999
#define rep(i, m, n) for (lli i = m; i < n; i++)
#define rrep(i, m, n) for (lli i = m - 1; i >= n; i--)
#define pb(n) push_back(n)
#define UE(N) N.erase(unique(N.begin(), N.end()), N.end());
#define Sort(n) sort(n.begin(), n.end())
#define Rev(n) reverse(n.begin(), n.end())
#define Out(S) cout << S << endl
#define NeOut(S) cout << S
#define HpOut(S) cout << setprecision(25) << S << endl
#define Vec(K, L, N, S) vector<L> K(N, S)
#define DV(K, L, N, M, S) vector<vector<L>> K(N, vector<L>(M, S))
#define TV(K, L, N, M, R, S)                                                   \\
  vector<vector<vector<L>>> K(N, vector<vector<L>>(M, vector<L>(R, S)))
#define pint pair<lli, lli>
#define paf(L, R) pair<L, R>
#define mod 998244353
#define MAX 10000000
#define ALL(a) a.begin(), a.end()
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  lli N, A, B, C, D, E, F, M, K, L, X, Y, Z, H, W, sum = 0, num = 0, flag = 0;
  string S, T;
  cin >> N;
  lli d[4] = {-1, 1, N, 0 - N};
  Vec(P, lli, N * N, 0);
  Vec(G, lli, N * N, 0);
  Vec(Fl, bool, N *N, 0);
  rep(i, 0, N * N) cin >> P[i];
  Vec(dist, lli, N * N, 0);
  rep(i, 0, N * N) {
    A = i % N;
    B = i / N;
    dist[i] = min(min(A, N - A - 1), min(B, N - B - 1));
  }
  rep(i, 0, N * N) {
    sum += dist[P[i] - 1];
    queue<lli> que;
    que.push(P[i] - 1);
    Fl[P[i] - 1] = 1;
    while (!que.empty()) {
      auto v = que.front();
      que.pop();
      rep(j, 0, 4) {
        auto nv = v + d[j];
        if (0 > nv || nv >= N * N)
          continue;
        if (dist[nv] < dist[v])
          continue;
        chmin(dist[nv], dist[v]);
        if (Fl[nv])
          que.push(nv);
      }
    }
  }
  Out(sum);
}