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

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using ll = unsigned long long;
#define double long double
using datas = pair<ll, ll>;
using ddatas = pair<double, double>;
using tdata = pair<ll, datas>;
using vec = vector<ll>;
using mat = vector<vec>;
using pvec = vector<datas>;
using pmat = vector<pvec>;
// using
// llset=tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>;
#define For(i, a, b) for (i = a; i < (ll)b; ++i)
#define bFor(i, b, a) for (i = b, --i; i >= (ll)a; --i)
#define rep(i, N) For(i, 0, N)
#define rep1(i, N) For(i, 1, N)
#define brep(i, N) bFor(i, N, 0)
#define brep1(i, N) bFor(i, N, 1)
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define vsort(v) sort(all(v))
#define vrsort(v) sort(allr(v))
#define endl "\\n"
#define eb emplace_back
#define print(v) cout << v << endl
#define printyes cout << "Yes" << endl
#define printno cout << "No" << endl
#define printYES cout << "YES" << endl
#define printNO cout << "NO" << endl
#define output(v)                                                              \\
  do {                                                                         \\
    bool f = 0;                                                                \\
    for (auto outi : v) {                                                      \\
      cout << (f ? " " : "") << outi;                                          \\
      f = 1;                                                                   \\
    }                                                                          \\
    cout << endl;                                                              \\
  } while (0)
#define matoutput(v)                                                           \\
  do {                                                                         \\
    for (auto outimat : v)                                                     \\
      output(outimat);                                                         \\
  } while (0)
const ll mod = 1000000007;
// const ll mod=998244353;
const ll inf = 1LL << 60;
const double PI = acos(-1);
const double eps = 1e-9;
template <class T> inline bool chmax(T &a, T b) {
  bool x = a < b;
  if (x)
    a = b;
  return x;
}
template <class T> inline bool chmin(T &a, T b) {
  bool x = a > b;
  if (x)
    a = b;
  return x;
}

void startupcpp() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}
using P = pair<int, int>;
int d[4] = {1, -1};
int dist[250001], ans;
bool ch[250001];
int main() {
  int now, N, M;
  scanf("%d", &N);
  d[2] = N;
  d[3] = -N;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      dist[i * N + j] = min({i, j, N - i - 1, N - j - 1});
    }
  }
  M = N * N;
  for (int times = 0; times < M; ++times) {
    now -= scanf("%d", &now);
    ans += dist[now];
    queue<P> que;
    que.push(P(dist[now] - 1, now));
    while (!que.empty()) {
      P x = que.front();
      que.pop();
      if (!chmin(dist[x.second], x.first))
        continue;
      for (int i = 0; i < 4; ++i) {
        if (x.second + d[i] >= 0 && x.second + d[i] < M)
          que.push(P(x.first + 1 - ch[x.second + d[i]], x.second + d[i]));
      }
    }
    ch[now] = true;
  }
  printf("%d\\n", ans);
}

Error: TLE

Fixed submission:

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
// #include<ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/tree_policy.hpp>
// #include<ext/pb_ds/tag_and_trait.hpp>
// using namespace __gnu_pbds;
// #include<boost/multiprecision/cpp_int.hpp>
// namespace multiprecisioninteger = boost::multiprecision;
// using cint=multiprecisioninteger::cpp_int;
using namespace std;
using ll = long long;
#define double long double
using datas = pair<ll, ll>;
using ddatas = pair<double, double>;
using tdata = pair<ll, datas>;
using vec = vector<ll>;
using mat = vector<vec>;
using pvec = vector<datas>;
using pmat = vector<pvec>;
// using
// llset=tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>;
#define For(i, a, b) for (i = a; i < (ll)b; ++i)
#define bFor(i, b, a) for (i = b, --i; i >= (ll)a; --i)
#define rep(i, N) For(i, 0, N)
#define rep1(i, N) For(i, 1, N)
#define brep(i, N) bFor(i, N, 0)
#define brep1(i, N) bFor(i, N, 1)
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define vsort(v) sort(all(v))
#define vrsort(v) sort(allr(v))
#define endl "\\n"
#define eb emplace_back
#define print(v) cout << v << endl
#define printyes cout << "Yes" << endl
#define printno cout << "No" << endl
#define printYES cout << "YES" << endl
#define printNO cout << "NO" << endl
#define output(v)                                                              \\
  do {                                                                         \\
    bool f = 0;                                                                \\
    for (auto outi : v) {                                                      \\
      cout << (f ? " " : "") << outi;                                          \\
      f = 1;                                                                   \\
    }                                                                          \\
    cout << endl;                                                              \\
  } while (0)
#define matoutput(v)                                                           \\
  do {                                                                         \\
    for (auto outimat : v)                                                     \\
      output(outimat);                                                         \\
  } while (0)
const ll mod = 1000000007;
// const ll mod=998244353;
const ll inf = 1LL << 60;
const double PI = acos(-1);
const double eps = 1e-9;
template <class T> inline bool chmax(T &a, T b) {
  bool x = a < b;
  if (x)
    a = b;
  return x;
}
template <class T> inline bool chmin(T &a, T b) {
  bool x = a > b;
  if (x)
    a = b;
  return x;
}

void startupcpp() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

using P = pair<int, int>;
int v[250001], ord[250001];
int main() {
  startupcpp();
  int i, j, k, N, M, ans = 0, res;
  cin >> N;
  M = N * N;
  rep(i, M) {
    cin >> v[i];
    ord[--v[i]] = i;
  }
  vector<bool> dist(M);
  int d[4] = {1, -1, N, -N};
  rep(k, M) {
    dist.assign(M, false);
    deque<P> que;
    que.eb(0, v[k]);
    while (!que.empty()) {
      P x = que.front();
      que.pop_front();
      if (dist[x.second])
        continue;
      dist[x.second] = true;
      i = x.second / N;
      j = x.second % N;
      if (i == 0 || j == 0 || i == N - 1 || j == N - 1) {
        res = x.first;
        break;
      }
      rep(i, 4) if (!dist[x.second + d[i]]) {
        if (ord[x.second + d[i]] > k) {
          que.eb(x.first + 1, x.second + d[i]);
        } else {
          que.emplace_front(x.first, x.second + d[i]);
        }
      }
    }
    ans += res;
    // print(v[k]<<" "<<res);
  }
  print(ans);
}