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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

typedef long long ll;
typedef uint64_t ull;
typedef pair<int, int> P;
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

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

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

  int n;
  cin >> n;
  int sz = n * n;
  vector<int> p(sz);
  for (int i = 0; i < sz; ++i)
    cin >> p[i];
  vector<P> pos(sz + 1);
  int cur = 1;
  vector<vector<int>> board(n, vector<int>(n));
  vector<vector<int>> dist(n, vector<int>(n, INF));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      board[i][j] = cur;
      pos[cur++] = P(i, j);
      chmin(dist[i][j], i);
      chmin(dist[i][j], n - 1 - i);
      chmin(dist[i][j], j);
      chmin(dist[i][j], n - 1 - j);
    }
  }

  int ans = 0;
  for (int val : p) {
    int sx = pos[val].first, sy = pos[val].second;
    ans += dist[sx][sy]--;
    board[sx][sy] = 0;
    deque<P> deq;
    deq.push_back(P(sx, sy));
    while (!deq.empty()) {
      P point = deq.front();
      deq.pop_front();
      int x = point.first, y = point.second;
      for (int i = 0; i < 4; ++i) {
        int nx = x + dx[i], ny = y + dy[i];
        if (nx < 0 || nx >= n || ny < 0 || ny >= n)
          continue;
        if (board[nx][ny]) {
          if (dist[nx][ny] > dist[x][y] + 1) {
            dist[nx][ny] = dist[x][y] + 1;
            deq.push_back(P(nx, ny));
          }
        } else {
          if (dist[nx][ny] > dist[x][y]) {
            dist[nx][ny] = dist[x][y];
            deq.push_front(P(nx, ny));
          }
        }
      }
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

typedef long long ll;
typedef uint64_t ull;
typedef pair<int, int> P;
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

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

int board[501][501];
int dist[501][501];

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

  int n;
  cin >> n;
  int sz = n * n;
  vector<int> p(sz);
  for (int i = 0; i < sz; ++i)
    cin >> p[i];
  vector<P> pos(sz + 1);
  int cur = 1;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      pos[cur++] = P(i, j);
    }
  }

  int ans = 0;
  for (int i = sz - 1; i >= 0; --i) {
    int sx = pos[p[i]].first, sy = pos[p[i]].second;
    if (sx == 0 || sx == n - 1 || sy == 0 || sy == n - 1) {
      board[sx][sy] = p[i];
      continue;
    }
    for (int j = 0; j < n; ++j) {
      for (int k = 0; k < n; ++k) {
        dist[j][k] = INF;
      }
    }
    deque<P> deq;
    dist[sx][sy] = 0;
    deq.push_back(P(sx, sy));
    int mindist = INF;
    while (!deq.empty()) {
      P point = deq.front();
      deq.pop_front();
      int x = point.first, y = point.second;
      if (x == 0 || x == n - 1 || y == 0 || y == n - 1) {
        chmin(mindist, dist[x][y]);
        break;
      }
      for (int j = 0; j < 4; ++j) {
        int nx = x + dx[j], ny = y + dy[j];
        if (board[nx][ny] > 0) {
          if (dist[nx][ny] > dist[x][y] + 1) {
            dist[nx][ny] = dist[x][y] + 1;
            deq.push_back(P(nx, ny));
          }
        } else {
          if (dist[nx][ny] > dist[x][y]) {
            dist[nx][ny] = dist[x][y];
            deq.push_front(P(nx, ny));
          }
        }
      }
    }
    board[sx][sy] = p[i];
    ans += mindist;
  }
  cout << ans << endl;
}