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

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
const int INF = 100010001;
const ll LINF = (ll)INF * INF * 10;

int di[4] = {0, 1, 0, -1};
int dj[4] = {1, 0, -1, 0};

int main() {
  int n;
  cin >> n;
  vi p(n * n);
  rep(i, n * n) {
    cin >> p[i];
    --p[i];
  }

  vector<vector<int>> iru(n, vector<int>(n, 1));
  vvi mat(n, vi(n));
  rep(i, n) {
    rep(j, n) { mat[i][j] = min({i, j, n - 1 - i, n - 1 - j}); }
  }

  int ans = 0;
  vector<P> que;
  vector<vector<bool>> used(n, vector<bool>(n, false));
  rep(iii, n * n) {
    int i = p[iii] / n, j = p[iii] % n;
    ans += mat[i][j];
    iru[i][j] = 0;

    que.clear();
    que.emplace_back(i, j);
    used[i][j] = true;
    rep(v, (int)que.size()) {
      int ni, nj;
      ni = que[v].first;
      nj = que[v].second;
      used[ni][nj] = false;

      int val = mat[ni][nj] + iru[ni][nj];
      rep(k, 4) {
        int nni = ni + di[k], nnj = nj + dj[k];
        if (nni < 0 || nnj < 0 || nni > n - 1 || nnj > n - 1)
          continue;
        if (mat[nni][nnj] > val) {
          mat[nni][nnj] = val;
          if (!used[nni][nnj]) {
            que.emplace_back(nni, nnj);
            used[nni][nnj] = true;
          }
        }
      }
    }
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
const int INF = 100010001;
const ll LINF = (ll)INF * INF * 10;

int di[4] = {0, 1, 0, -1};
int dj[4] = {1, 0, -1, 0};

int main() {
  int n;
  cin >> n;
  vi p(n * n);
  rep(i, n * n) {
    cin >> p[i];
    --p[i];
  }

  vector<vector<bool>> iru(n, vector<bool>(n, true));
  vvi mat(n, vi(n));
  rep(i, n) {
    rep(j, n) { mat[i][j] = min({i, j, n - 1 - i, n - 1 - j}); }
  }

  int ans = 0;
  rep(iii, n * n) {
    int i = p[iii] / n, j = p[iii] % n;
    ans += mat[i][j];
    iru[i][j] = false;

    vector<P> que(1);
    que.emplace_back(i, j);
    int v = 0;
    vector<vector<bool>> used(n, vector<bool>(n, false));
    used[i][j] = true;
    rep(v, (int)que.size()) {
      int ni, nj;
      ni = que[v].first;
      nj = que[v].second;

      int val = mat[ni][nj] + iru[ni][nj];
      rep(k, 4) {
        int nni = ni + di[k], nnj = nj + dj[k];
        if (nni < 0 or nnj < 0 or nni > n - 1 or nnj > n - 1)
          continue;
        if (used[nni][nnj])
          continue;
        if (mat[nni][nnj] > val) {
          mat[nni][nnj] = val;
          que.emplace_back(nni, nnj);
          used[nni][nnj] = true;
        }
      }
    }

    /*cout << p[iii]+1 << endl;
    rep(i, n) {
        rep(j, n) {
            cout << mat[i][j] << (j == n-1 ? '\\n':' ');
        }
    }
    cout << endl;*/
  }

  cout << ans << endl;
}