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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;

vector<int> dx = {0, 0, -1, 1};
vector<int> dy = {-1, 1, 0, 0};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif
  int n;
  cin >> n;
  vector<int> p(n * n);
  for (int i = 0; i < n * n; ++i) {
    cin >> p[i];
    --p[i];
  }
  vector<vector<int>> best(n, vector<int>(n, n));
  vector<vector<int>> used(n, vector<int>(n, 1));
  {
    queue<pair<int, int>> q;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        if (i == 0 || j == 0 || i == n - 1 || j == n - 1) {
          best[i][j] = 1;
          q.push({i, j});
        }
      }
    }
    while (!q.empty()) {
      auto p = q.front();
      q.pop();
      int x = p.first, y = p.second;
      for (int k = 0; k < 4; ++k) {
        int nx = x + dx[k], ny = y + dy[k];
        if (0 <= nx && nx < n && 0 <= ny && ny < n &&
            best[x][y] + used[nx][ny] < best[nx][ny]) {
          best[nx][ny] = best[x][y] + used[nx][ny];
          q.push({nx, ny});
        }
      }
    }
  }

  int ans = 0;
  for (int i = 0; i < n * n; ++i) {
    int x = p[i] / n, y = p[i] % n;
    ans += best[x][y] - 1;
    used[x][y] = 0;
    --best[x][y];
    queue<pair<int, int>> q;
    q.push({x, y});
    while (!q.empty()) {
      auto p = q.front();
      q.pop();
      int x = p.first, y = p.second;
      for (int k = 0; k < 4; ++k) {
        int nx = x + dx[k], ny = y + dy[k];
        if (0 <= nx && nx < n && 0 <= ny && ny < n &&
            best[x][y] + used[nx][ny] < best[nx][ny]) {
          best[nx][ny] = best[x][y] + used[nx][ny];
          q.push({nx, ny});
        }
      }
    }
  }

  cout << ans << '\\n';
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;

vector<int> dx = {0, 0, -1, 1};
vector<int> dy = {-1, 1, 0, 0};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif
  int n;
  cin >> n;
  vector<int> p(n * n);
  for (int i = 0; i < n * n; ++i) {
    cin >> p[i];
    --p[i];
  }
  vector<vector<int>> best(n, vector<int>(n, n));
  vector<vector<int>> used(n, vector<int>(n, 1));
  function<void(int, int, int)> update = [&](int i, int j, int x) {
    if (x >= best[i][j])
      return;
    best[i][j] = x;
    for (int k = 0; k < 4; ++k) {
      int nx = i + dx[k], ny = j + dy[k];
      if (0 <= nx && nx < n && 0 <= ny && ny < n) {
        update(nx, ny, best[i][j] + used[nx][ny]);
      }
    }
  };

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (i == 0 || j == 0 || i == n - 1 || j == n - 1) {
        update(i, j, 1);
      }
    }
  }

  int ans = 0;
  for (int i = 0; i < n * n; ++i) {
    int x = p[i] / n, y = p[i] % n;
    ans += best[x][y] - 1;
    used[x][y] = 0;
    --best[x][y];
    queue<pair<int, int>> q;
    q.push({x, y});
    while (!q.empty()) {
      auto p = q.front();
      q.pop();
      int x = p.first, y = p.second;
      for (int k = 0; k < 4; ++k) {
        int nx = x + dx[k], ny = y + dy[k];
        if (0 <= nx && nx < n && 0 <= ny && ny < n &&
            best[x][y] + used[nx][ny] < best[nx][ny]) {
          best[nx][ny] = best[x][y] + used[nx][ny];
          q.push({nx, ny});
        }
      }
    }
  }

  cout << ans << '\\n';
}