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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#ifdef ENABLE_DEBUG
#define dump(a) cerr << #a << "=" << a << endl
#define dumparr(a, n) cerr << #a << "[" << n << "]=" << a[n] << endl
#else
#define dump(a)
#define dumparr(a, n)
#endif
#define FOR(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define For(i, a) FOR(i, 0, a)
#define REV(i, a, b) for (ll i = (ll)b - 1LL; i >= (ll)a; i--)
#define Rev(i, a) REV(i, 0, a)
#define REP(a) For(i, a)
#define SIGN(a) (a == 0 ? 0 : (a > 0 ? 1 : -1))

typedef long long int ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> ppll;
typedef vector<ll> vll;
typedef long double ld;
typedef pair<ld, ld> pdd;

const ll INF = (1LL << 60);
#if __cplusplus < 201700L
ll gcd(ll a, ll b) {
  a = abs(a);
  b = abs(b);
  if (a == 0)
    return b;
  if (b == 0)
    return a;
  if (a < b)
    return gcd(b, a);
  ll r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}
#endif
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class S, class T>
std::ostream &operator<<(std::ostream &os, pair<S, T> a) {
  os << "(" << a.first << "," << a.second << ")";
  return os;
}
template <class T> std::ostream &operator<<(std::ostream &os, vector<T> a) {
  os << "[ ";
  REP(a.size()) { os << a[i] << " "; }
  os << " ]" << endl;
  ;
  return os;
}

int main() {
  cout << setprecision(1000);
  ll N;
  cin >> N;
  vector<ll> P(N * N);
  REP(N * N) {
    cin >> P[i];
    --P[i];
  }
  vector<vector<ll>> ans(N, vector<ll>(N));
  For(i, N) {
    For(j, N) { ans[i][j] = min(min(i, j), min(N - 1 - i, N - 1 - j)); }
  }
  ll sumans = 0;
  bool freed[N][N];
  For(i, N) For(j, N) freed[i][j] = false;
  for (auto &&i : P) {
    ll x = i / N, y = i % N;
    sumans += ans[x][y];
    freed[x][y] = true;
    stack<tuple<ll, ll, ll>> q;
    q.push(make_tuple(x, y, ans[x][y]));
    while (!q.empty()) {
      ll i, j, num;
      tie(i, j, num) = q.top();
      q.pop();
      for (auto &&ii :
           {pll(i - 1, j), pll(i + 1, j), pll(i, j - 1), pll(i, j + 1)}) {
        ll k = ii.first, l = ii.second;
        if (0 <= k && k < N && 0 <= l && l < N) {
          if (freed[i][j] && ans[k][l] > num) {
            q.push(make_tuple(k, l, num));
            ans[k][l] = num;
          } else if (ans[k][l] > num + 1) {
            q.push(make_tuple(k, l, num + 1));
            ans[k][l] = num + 1;
          }
        }
      }
    }
    /*For(i,N){
      For(j,N){
        fprintf(stderr,"%2lld ",ans[i][j]);
      }
      fprintf(stderr,"\\n");
    }
    fprintf(stderr,"\\n");*/
  }
  cout << sumans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#ifdef ENABLE_DEBUG
#define dump(a) cerr << #a << "=" << a << endl
#define dumparr(a, n) cerr << #a << "[" << n << "]=" << a[n] << endl
#else
#define dump(a)
#define dumparr(a, n)
#endif
#define FOR(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define For(i, a) FOR(i, 0, a)
#define REV(i, a, b) for (ll i = (ll)b - 1LL; i >= (ll)a; i--)
#define Rev(i, a) REV(i, 0, a)
#define REP(a) For(i, a)
#define SIGN(a) (a == 0 ? 0 : (a > 0 ? 1 : -1))

typedef long long int ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> ppll;
typedef vector<ll> vll;
typedef long double ld;
typedef pair<ld, ld> pdd;

const ll INF = (1LL << 60);
#if __cplusplus < 201700L
ll gcd(ll a, ll b) {
  a = abs(a);
  b = abs(b);
  if (a == 0)
    return b;
  if (b == 0)
    return a;
  if (a < b)
    return gcd(b, a);
  ll r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}
#endif
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class S, class T>
std::ostream &operator<<(std::ostream &os, pair<S, T> a) {
  os << "(" << a.first << "," << a.second << ")";
  return os;
}
template <class T> std::ostream &operator<<(std::ostream &os, vector<T> a) {
  os << "[ ";
  REP(a.size()) { os << a[i] << " "; }
  os << " ]" << endl;
  ;
  return os;
}

int main() {
  cout << setprecision(1000);
  ll N;
  cin >> N;
  vector<ll> P(N * N);
  REP(N * N) {
    cin >> P[i];
    --P[i];
  }
  vector<vector<ll>> ans(N, vector<ll>(N));
  For(i, N) {
    For(j, N) { ans[i][j] = min(min(i, j), min(N - 1 - i, N - 1 - j)); }
  }
  ll sumans = 0;
  set<pll> returned;
  for (auto &&i : P) {
    ll x = i / N, y = i % N;
    sumans += ans[x][y];
    returned.insert(pll(x, y));
    stack<tuple<ll, ll, ll>> q;
    q.push(make_tuple(x, y, ans[x][y]));
    while (!q.empty()) {
      ll i, j, num;
      tie(i, j, num) = q.top();
      q.pop();
      for (auto &&ii :
           {pll(i - 1, j), pll(i + 1, j), pll(i, j - 1), pll(i, j + 1)}) {
        ll k = ii.first, l = ii.second;
        if (0 <= k && k < N && 0 <= l && l < N) {
          if (returned.count(pll(i, j)) && ans[k][l] > num) {
            q.push(make_tuple(k, l, num));
            ans[k][l] = num;
          } else if (ans[k][l] > num + 1) {
            q.push(make_tuple(k, l, num + 1));
            ans[k][l] = num + 1;
          }
        }
      }
    }
    /*For(i,N){
      For(j,N){
        fprintf(stderr,"%2lld ",ans[i][j]);
      }
      fprintf(stderr,"\\n");
    }
    fprintf(stderr,"\\n");*/
  }
  cout << sumans << endl;
  return 0;
}