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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
using namespace std;
using ll = long long;
constexpr ll INF = 3000000000000000000;

template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

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

int main() {
  int n;
  cin >> n;
  vector<int> p(n * n), q(n * n);
  REP(i, n * n) {
    int num;
    cin >> num;
    --num;
    p[i] = num / n;
    q[i] = num % n;
  }

  vector<vector<int>> s(n, vector<int>(n, 0));
  REP(i, n) REP(j, n) { s[i][j] = min({i, j, n - i - 1, n - j - 1}); }
  vector<vector<bool>> t(n, vector<bool>(n, false));
  int ans = 0;
  REP(i, n * n) {
    ans += s[p[i]][q[i]];
    t[p[i]][q[i]] = true;
    queue<pair<int, int>> que;
    que.push(pair<int, int>(p[i], q[i]));
    while (!que.empty()) {
      int x = que.front().first;
      int y = que.front().second;
      que.pop();
      REP(j, 4) {
        if (x + dx[j] >= 0 && x + dx[j] < n && y + dy[j] >= 0 &&
            y + dy[j] < n) {
          if (chmin(s[x + dx[j]][y + dy[j]], s[x][y] + (t[x][y] ? 0 : 1))) {
            que.push(pair<int, int>(x + dx[j], y + dy[j]));
          }
        }
      }
    }
  }

  cout << ans << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
using namespace std;
using ll = long long;
constexpr ll INF = 3000000000000000000;

template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

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

int main() {
  int n;
  cin >> n;
  vector<int> p(n * n);
  REP(i, n * n) {
    cin >> p[i];
    --p[i];
  }

  vector<vector<int>> s(n, vector<int>(n, 0));
  REP(i, n) REP(j, n) { s[i][j] = min({i, j, n - i - 1, n - j - 1}); }
  vector<vector<bool>> t(n, vector<bool>(n, false));
  int ans = 0;
  REP(i, n * n) {
    ans += s[p[i] / n][p[i] % n];
    t[p[i] / n][p[i] % n] = true;
    queue<int> q;
    REP(j, 4) {
      if (p[i] / n + dx[j] >= 0 && p[i] / n + dx[j] < n &&
          p[i] % n + dy[j] >= 0 && p[i] % n + dy[j] < n) {
        q.push((p[i] / n + dx[j]) * n + p[i] % n + dy[j]);
      }
    }
    while (!q.empty()) {
      int num = q.front();
      q.pop();
      int x = num / n, y = num % n;
      bool flag = false;
      REP(j, 4) {
        if (x + dx[j] >= 0 && x + dx[j] < n && y + dy[j] >= 0 &&
            y + dy[j] < n) {
          if (chmin(s[x][y], s[x + dx[j]][y + dy[j]] +
                                 (t[x + dx[j]][y + dy[j]] ? 0 : 1)))
            flag = true;
        }
      }
      if (flag) {
        REP(j, 4) {
          if (x + dx[j] >= 0 && x + dx[j] < n && y + dy[j] >= 0 &&
              y + dy[j] < n) {
            q.push((x + dx[j]) * n + (y + dy[j]));
          }
        }
      }
    }
  }

  cout << ans << "\\n";
  return 0;
}