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

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using pi = pair<int, int>;
using pii = pair<int, pi>;

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n;
  cin >> n;
  vector<int> vd = {-n, +1, +n, -1};
  vector<bool> seki(n * n, true);
  ll ans = 0;
  int p;
  vector<int> dp(n * n, n * n);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      dp[i * n + j] = min(i, dp[i * n + j]);
      dp[(n - 1 - i) * n + j] = min(i, dp[(n - 1 - i) * n + j]);
    }
  }
  for (int j = 0; j < n; ++j) {
    for (int i = 0; i < n; ++i) {
      dp[j * n + i] = min(i, dp[j * n + i]);
      dp[j * n + (n - 1 - i)] = min(i, dp[j * n + (n - 1 - i)]);
    }
  }
  int pp = -1;
  for (int i = 0; i < n * n; ++i) {
    cin >> p;
    --p;
    ans += dp[p];
    deque<pi> deq;
    --dp[p];
    deq.emplace_front(dp[p], p);
    int tans;
    while (!deq.empty()) {
      auto t = deq.front();
      deq.pop_front();
      // cerr<<p<<" "<<t.first<<" "<<t.second<<endl;
      for (int i = 0; i < 4; ++i) {
        int np = t.second + vd[i];
        int res = t.second % n;
        int nc = t.first;
        if (np < 0 || n * n <= np || (res == n - 1 && i == 1) ||
            (res == 0 && i == 3)) {
          continue;
        }
        if (seki[np])
          ++nc;
        if (dp[np] <= nc)
          continue;
        dp[np] = nc;
        if (seki[np]) {
          deq.emplace_back(nc, np);
        } else
          deq.emplace_front(nc, np);
      }
    }
  eol:;
    seki[p] = false;
  }
  cout << ans << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using pi = pair<int, int>;
using pii = pair<int, pi>;

int main() {
  int n;
  cin >> n;
  vector<int> vd = {-n, +1, +n, -1};
  vector<bool> seki(n * n, true);
  ll ans = 0;
  int p;
  vector<int> dp(n * n);
  for (int i = 0; i < n * n; ++i) {
    cin >> p;
    --p;
    deque<pi> deq;
    dp.assign(n * n, n * n);
    dp[p] = 0;
    deq.push_front(pi(0, p));
    int tans;
    while (!deq.empty()) {
      auto t = deq.front();
      deq.pop_front();
      // cerr<<p<<" "<<t.first<<" "<<t.second<<endl;
      for (int i = 0; i < 4; ++i) {
        int np = t.second + vd[i];
        if (np < 0 || n * n <= np) {
          tans = t.first;
          if (!deq.empty())
            deq.clear();
          break;
        }
        if (t.second % n == n - 1 && i == 1) {
          tans = t.first;
          if (!deq.empty())
            deq.clear();
          break;
        }
        if (t.second % n == 0 && i == 3) {
          tans = t.first;
          if (!deq.empty())
            deq.clear();
          break;
        }
        int nc = t.first;
        if (seki[np])
          ++nc;
        if (dp[np] <= nc)
          continue;
        dp[np] = nc;
        if (seki[np]) {
          deq.push_back(pi(nc, np));
        } else
          deq.push_front(pi(nc, np));
      }
    }
    // cerr<<tans<<endl;
    ans += tans;
    seki[p] = false;
  }
  cout << ans << endl;
  return 0;
}