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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PP;
#define MOD 1000000007
// #define MOD 998244353
#define INF 2305843009213693951
// #define INF 810114514
#define PI 3.141592653589
#define setdouble setprecision
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define OREP(i, n) for (ll i = 1; i <= (n); ++i)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; --i)
#define all1(i) begin(i), end(i)
#define GOODBYE                                                                \\
  do {                                                                         \\
    cout << "0" << endl;                                                       \\
    return 0;                                                                  \\
  } while (false)
#define MM << " " <<
#define Endl endl
#define debug false
#define debug2 false

int main(void) {

  ll N, p;
  vector<ll> P;
  cin >> N;
  REP(i, N * N) {
    cin >> p;
    p--;
    P.push_back(p);
  }

  ll Ans = 0;
  ll D[514][514];
  bool F[514][514];
  bool G[514][514];

  REP(i, N) {
    REP(j, N) {
      D[i][j] = min({i, j, N - i - 1, N - j - 1});
      F[i][j] = false;
      G[i][j] = false;
    }
  }

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

  /*
  REP(i,N){
      REP(j,N){
          cout << D[i][j] << " ";
      }cout << endl;
  }cout << endl;*/

  REP(_, P.size()) {
    ll i = P[_] / N;
    ll j = P[_] % N;

    Ans += D[i][j];
    F[i][j] = true;
    vector<pair<ll, ll>> g;

    queue<pair<ll, ll>> Q;
    Q.push({i, j});
    G[i][j] = true;
    g.clear();
    g.push_back({i, j});

    while (!Q.empty()) {
      ll u = Q.front().first;
      ll v = Q.front().second;
      Q.pop();

      g.push_back({u, v});
      G[u][v] = true;
      REP(k, 4) {
        ll y = u + dy[k];
        ll x = v + dx[k];
        if (!(0 <= x && x < N))
          continue;
        if (!(0 <= y && y < N))
          continue;
        if (G[y][x])
          continue;
        if (D[y][x] <= D[u][v] + 1 - (ll)F[u][v])
          continue;

        Q.push({y, x});
        D[y][x] = D[u][v] + 1 - (ll)F[u][v];
      }
    }

    REP(k, g.size()) { G[g[k].first][g[k].second] = false; }
    g.clear();
    /*
    cout << i+1 MM j+1 << endl;
    REP(a,N){
        REP(b,N){
            if(F[a][b]){cout << "t";}else{cout << " ";}
            cout << D[a][b] << " ";
        }cout << endl;
    }cout << endl;
    */
  }

  cout << Ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PP;
#define MOD 1000000007
// #define MOD 998244353
#define INF 2305843009213693951
// #define INF 810114514
#define PI 3.141592653589
#define setdouble setprecision
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define OREP(i, n) for (ll i = 1; i <= (n); ++i)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; --i)
#define all1(i) begin(i), end(i)
#define GOODBYE                                                                \\
  do {                                                                         \\
    cout << "0" << endl;                                                       \\
    return 0;                                                                  \\
  } while (false)
#define MM << " " <<
#define Endl endl
#define debug false
#define debug2 false

int main(void) {

  ll N, p;
  vector<ll> P;
  cin >> N;
  REP(i, N * N) {
    cin >> p;
    p--;
    P.push_back(p);
  }

  ll Ans = 0;
  ll D[514][514];
  bool F[514][514];

  REP(i, N) {
    REP(j, N) {
      D[i][j] = min({i, j, N - i - 1, N - j - 1});
      F[i][j] = false;
    }
  }

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

  /*
  REP(i,N){
      REP(j,N){
          cout << D[i][j] << " ";
      }cout << endl;
  }cout << endl;*/

  REP(_, P.size()) {
    ll i = P[_] / N;
    ll j = P[_] % N;

    Ans += D[i][j];
    F[i][j] = true;
    map<pair<ll, ll>, bool> M;

    queue<pair<ll, ll>> Q;
    Q.push({i, j});
    M[{i, j}] = true;

    while (!Q.empty()) {
      ll u = Q.front().first;
      ll v = Q.front().second;
      Q.pop();

      M[{u, v}] = true;
      REP(k, 4) {
        ll y = u + dy[k];
        ll x = v + dx[k];
        if (!(0 <= x && x < N))
          continue;
        if (!(0 <= y && y < N))
          continue;
        if (M[{y, x}])
          continue;
        if (D[y][x] <= D[u][v] + 1 - (ll)F[u][v])
          continue;

        Q.push({y, x});
        D[y][x] = D[u][v] + 1 - (ll)F[u][v];
      }
    }

    /*
    cout << i+1 MM j+1 << endl;
    REP(a,N){
        REP(b,N){
            if(F[a][b]){cout << "t";}else{cout << " ";}
            cout << D[a][b] << " ";
        }cout << endl;
    }cout << endl;
    */
  }

  cout << Ans << endl;
  return 0;
}