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

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vll = vector<long long>;
using vp = vector<pair<int, int>>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using vvll = vector<vector<long long>>;
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 (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

const int dy[] = {1, 0, -1, 0};
const int dx[] = {0, 1, 0, -1};

vvi grid;
vector<vector<bool>> present;
int n;

void bfs(int sy, int sx) {
  queue<P> q;
  q.emplace(sy, sx);
  present[sy][sx] = false;
  while (!q.empty()) {
    int y = q.front().fi, x = q.front().se;
    q.pop();
    rep(i, 4) {
      int ny = y + dy[i], nx = x + dx[i];
      if (ny < 0 || n <= ny || nx < 0 || n <= nx)
        continue;
      if (chmin(grid[ny][nx], grid[y][x] + present[y][x]))
        q.emplace(ny, nx);
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> n;
  grid.resize(n, vi(n));
  present.resize(n, vb(n, true));
  rep(i, n) rep(j, n) grid[i][j] = min({i, n - 1 - i, j, n - 1 - j});
  int ans = 0;
  rep(i, n * n) {
    int p;
    cin >> p;
    p--;
    int sy = p / n;
    int sx = p % n;
    ans += grid[sy][sx];
    bfs(sy, sx);
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vll = vector<long long>;
using vp = vector<pair<int, int>>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using vvll = vector<vector<long long>>;
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 (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
vvi grid;
int n;

int bfs(int s) {
  int y = s / n;
  int x = s % n;
  grid[y][x] = 0;
  vvi d(n, vi(n, -1));
  d[y][x] = 0;
  using PP = pair<int, P>;
  priority_queue<PP, vector<PP>, greater<PP>> q;
  q.emplace(0, P(y, x));
  int res = 1001001001;
  if (y == 0 || x == 0 || y == n - 1 || x == n - 1)
    return 0;
  while (!q.empty()) {
    int vy = q.top().se.fi, vx = q.top().se.se;
    q.pop();
    rep(i, 4) {
      int ny = vy + dy[i], nx = vx + dx[i];
      if (ny < 0 || ny >= n || nx < 0 || nx >= n)
        continue;
      if (d[ny][nx] >= 0)
        continue;
      d[ny][nx] = d[vy][vx] + grid[ny][nx];
      if (ny == 0 || nx == 0 || ny == n - 1 || nx == n - 1)
        chmin(res, d[ny][nx]);
      q.emplace(d[ny][nx], P(ny, nx));
    }
  }
  return res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n;
  vi p(n * n);
  rep(i, n * n) cin >> p[i];
  grid.resize(n, vi(n, 1));
  ll ans = 0;
  rep(i, n * n) ans += bfs(--p[i]);
  cout << ans << endl;
}