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

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pll pair<ll, ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define lb(c, x) distance(c.begin(), lower_bound(all(c), x))
#define ub(c, x) distance(c.begin(), upper_bound(all(c), x))

using namespace std;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const ll INF = 1e9 + 7;

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

int main() {
  ll n;
  cin >> n;
  vector<ll> p(n * n);
  ll sum = 0;
  vector<vector<pll>> ans(n, vector<pll>(n, make_pair(0, 1)));
  rep(i, n * n) {
    cin >> p[i];
    p[i]--;
  }
  rep(i, n) {
    rep(j, n) {
      ans[i][j].fi = min(min(i, n - 1 - i), min(j, n - 1 - j));
      // cout << ans[i][j].fi << " ";
    }
    // cout << endl;
  }
  rep(k, n * n) {
    ll i = p[k];
    sum += ans[i / n][i % n].fi;
    queue<pll> que;
    que.push(make_pair(i / n, i % n));
    ans[i / n][i % n].se = 0;
    // vector<vector<ll>> s(n,vector<ll>(n));
    while (!que.empty()) {
      pll x = que.front();
      que.pop();
      // s[x.fi][x.se]=1;
      if (ans[x.fi][x.se].se == 0) {
        rep(j, 4) {
          ll nx = x.fi + dx[j];
          ll ny = x.se + dy[j];
          if (0 <= nx && nx <= n - 1 && 0 <= ny && ny <= n - 1 &&
              ans[nx][ny].fi > ans[x.fi][x.se].fi) {
            ans[nx][ny].fi = ans[x.fi][x.se].fi;
            que.push(make_pair(nx, ny));
          }
        }
      } else {
        rep(j, 4) {
          ll nx = x.fi + dx[j];
          ll ny = x.se + dy[j];
          if (0 <= nx && nx <= n - 1 && 0 <= ny && ny <= n - 1 &&
              ans[nx][ny].fi > ans[x.fi][x.se].fi + 1) {
            ans[nx][ny].fi = ans[x.fi][x.se].fi + 1;
            que.push(make_pair(nx, ny));
          }
        }
      }
    }
  }
  /*cout << endl;
  rep(i,n){
      rep(j,n){
          //ans[i][j].fi=min(min(i,n-1-i),min(j,n-1-j));
          cout << ans[i][j].fi << " ";
      }
      cout << endl;
  }
  cout << endl;
  rep(i,n){
      rep(j,n){
          //ans[i][j].fi=min(min(i,n-1-i),min(j,n-1-j));
          cout << ans[i][j].se << " ";
      }
      cout << endl;
  }*/
  cout << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pll pair<ll, ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define lb(c, x) distance(c.begin(), lower_bound(all(c), x))
#define ub(c, x) distance(c.begin(), upper_bound(all(c), x))

using namespace std;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const ll INF = 1e9 + 7;

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

int main() {
  ll n;
  cin >> n;
  vector<ll> p(n * n);
  ll sum = 0;
  vector<vector<pll>> ans(n, vector<pll>(n, make_pair(0, 1)));
  rep(i, n * n) {
    cin >> p[i];
    p[i]--;
  }
  rep(i, n) {
    rep(j, n) {
      ans[i][j].fi = min(min(i, n - 1 - i), min(j, n - 1 - j));
      // cout << ans[i][j].fi << " ";
    }
    // cout << endl;
  }
  rep(k, n * n) {
    ll i = p[k];
    sum += ans[i / n][i % n].fi;
    queue<pll> que;
    que.push(make_pair(i / n, i % n));
    ans[i / n][i % n].se = 0;
    vector<vector<ll>> s(n, vector<ll>(n));
    while (!que.empty()) {
      pll x = que.front();
      que.pop();
      s[x.fi][x.se] = 1;
      if (ans[x.fi][x.se].se == 0) {
        rep(j, 4) {
          ll nx = x.fi + dx[j];
          ll ny = x.se + dy[j];
          if (0 <= nx && nx <= n - 1 && 0 <= ny && ny <= n - 1 &&
              s[nx][ny] == 0 && ans[nx][ny].fi > ans[x.fi][x.se].fi) {
            ans[nx][ny].fi = ans[x.fi][x.se].fi;
            que.push(make_pair(nx, ny));
          }
        }
      } else {
        rep(j, 4) {
          ll nx = x.fi + dx[j];
          ll ny = x.se + dy[j];
          if (0 <= nx && nx <= n - 1 && 0 <= ny && ny <= n - 1 &&
              s[nx][ny] == 0 && ans[nx][ny].fi > ans[x.fi][x.se].fi + 1) {
            ans[nx][ny].fi = ans[x.fi][x.se].fi + 1;
            que.push(make_pair(nx, ny));
          }
        }
      }
    }
  }
  /*cout << endl;
  rep(i,n){
      rep(j,n){
          //ans[i][j].fi=min(min(i,n-1-i),min(j,n-1-j));
          cout << ans[i][j].fi << " ";
      }
      cout << endl;
  }
  cout << endl;
  rep(i,n){
      rep(j,n){
          //ans[i][j].fi=min(min(i,n-1-i),min(j,n-1-j));
          cout << ans[i][j].se << " ";
      }
      cout << endl;
  }*/
  cout << sum << endl;
  return 0;
}