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

#line 1 "/mnt/c/Users/leafc/dev/compro/lib/template.hpp"

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
#define MM << " " <<

using namespace std;

template <class T> void say(bool val, T yes = "Yes", T no = "No") {
  cout << (val ? yes : no) << "\\n";
}

template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

#line 2 "agc044_b.cpp"

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

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

  int n;
  cin >> n;
  vector<vi> grid(n, vi(n, n));
  {
    queue<pair<int, int>> q;

    REP(i, n) {
      grid[0][i] = 0;
      grid[i][0] = 0;
      grid[n - 1][i] = 0;
      grid[i][n - 1] = 0;
      q.push({0, i});
      q.push({i, 0});
      q.push({n - 1, i});
      q.push({i, n - 1});
    }

    while (!q.empty()) {
      auto [x, y] = q.front();
      q.pop();
      REP(k, 4) {
        int nx = x + dx[k], ny = y + dy[k];
        if (0 <= nx && nx < n && 0 <= ny && ny < n) {
          if (grid[nx][ny] > grid[x][y] + 1) {
            chmin(grid[nx][ny], grid[x][y] + 1);
            q.push({nx, ny});
          }
        }
      }
    }
  }

  int ans = 0;
  vector<vector<bool>> empty(n, vector<bool>(n, false));
  REP(i, n * n) {
    int cur;
    cin >> cur;
    cur--;
    int cx = cur / n, cy = cur % n;
    ans += grid[cx][cy];
    empty[cx][cy] = true;
    queue<pair<int, int>> que;
    que.push({cx, cy});
    while (!que.empty()) {
      auto [x, y] = que.front();
      que.pop();
      REP(k, 4) {
        int nx = x + dx[k], ny = y + dy[k];
        if (0 <= nx && nx < n && 0 <= ny && ny < n) {
          if (grid[nx][ny] > grid[x][y] + (empty[x][y] ? 0 : 1)) {
            chmin(grid[nx][ny], grid[x][y] + (empty[x][y] ? 0 : 1));
            que.push({nx, ny});
          }
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#line 1 "/mnt/c/Users/leafc/dev/compro/lib/template.hpp"

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
#define MM << " " <<

using namespace std;

template <class T> void say(bool val, T yes = "Yes", T no = "No") {
  cout << (val ? yes : no) << "\\n";
}

template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

#line 2 "agc044_b.cpp"

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

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

  int n;
  cin >> n;
  vector<vi> grid(n, vi(n, n));
  {
    queue<pair<int, int>> q;

    REP(i, n) {
      grid[0][i] = 0;
      grid[i][0] = 0;
      grid[n - 1][i] = 0;
      grid[i][n - 1] = 0;
      q.push({0, i});
      q.push({i, 0});
      q.push({n - 1, i});
      q.push({i, n - 1});
    }

    while (!q.empty()) {
      auto [x, y] = q.front();
      q.pop();
      REP(k, 4) {
        int nx = x + dx[k], ny = y + dy[k];
        if (0 <= nx && nx < n && 0 <= ny && ny < n) {
          if (grid[nx][ny] > grid[x][y] + 1) {
            chmin(grid[nx][ny], grid[x][y] + 1);
            q.push({nx, ny});
          }
        }
      }
    }
  }

  int ans = 0;
  vector<vector<bool>> empty(n, vector<bool>(n, false));
  REP(i, n * n) {
    int cur;
    cin >> cur;
    cur--;
    int cx = cur / n, cy = cur % n;
    ans += grid[cx][cy];
    empty[cx][cy] = true;
    queue<pair<int, int>> que;
    que.push({cx, cy});
    while (!que.empty()) {
      auto [x, y] = que.front();
      que.pop();
      REP(k, 4) {
        int nx = x + dx[k], ny = y + dy[k];
        if (0 <= nx && nx < n && 0 <= ny && ny < n) {
          if (grid[nx][ny] > grid[x][y]) {

            chmin(grid[nx][ny], grid[x][y] + (empty[x][y] ? 0 : 1));
            que.push({nx, ny});
          }
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}