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

// 四則演算 #pragma GCC target("avx")
// 並列計算 #pragma GCC optimize("O3")
// 条件分岐を減らす #pragma GCC optimize("unroll-loops")
// 浮動小数点演算 #pragma GCC
// target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using VI = vector<int>;
using VL = vector<ll>;
using VS = vector<string>;
template <class T> using PQ = priority_queue<T, vector<T>, greater<T>>;
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define eFOR(i, a, n) for (int i = (a); i <= (n); ++i)
#define rFOR(i, a, n) for (int i = (n)-1; i >= (a); --i)
#define erFOR(i, a, n) for (int i = (n); i >= (a); --i)
#define each(i, a) for (auto &i : a)
#define SORT(i) sort(i.begin(), i.end())
#define rSORT(i) sort(i.rbegin(), i.rend())
#define fSORT(i, a) sort(i.begin(), i.end(), a)
#define all(i) i.begin(), i.end()
#define out(y, x, h, w) ((y) < 0 || (h) <= (y) || (x) < 0 || (w) <= (x))
#define line cout << "-----------------------------\\n"
#define ENDL(i, n) ((i) == (n)-1 ? "\\n" : " ")
#define stop system("pause")
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
constexpr ld eps = 1e-10;
constexpr ld pi = 3.1415926535897932;
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
inline void init() {
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}
template <class T> inline istream &operator>>(istream &is, vector<T> &v) {
  for (auto &a : v)
    is >> a;
  return is;
}
template <class T> inline istream &operator>>(istream &is, deque<T> &v) {
  for (auto &a : v)
    is >> a;
  return is;
}
template <class T, class U>
inline istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <class T> inline vector<T> vec(size_t a) { return vector<T>(a); }
template <class T> inline vector<T> defvec(T def, size_t a) {
  return vector<T>(a, def);
}
template <class T, class... Ts> inline auto vec(size_t a, Ts... ts) {
  return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...));
}
template <class T, class... Ts> inline auto defvec(T def, size_t a, Ts... ts) {
  return vector<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...));
}
inline void print() { cout << "\\n"; }
template <class T, class... Ts> inline void print(const T &a, const Ts &...ts) {
  cout << a << " ";
  print(ts...);
}
template <class T> inline void print(const vector<T> &v) {
  for (int i = 0; i < v.size(); ++i)
    cout << v[i] << (i == v.size() - 1 ? "\\n" : " ");
}
template <class T> inline void print(const vector<vector<T>> &v) {
  for (auto &a : v)
    print(a);
}
inline string reversed(const string &s) {
  string t = s;
  reverse(all(t));
  return t;
}

int main() {
  init();

  int n;
  cin >> n;
  VI p(n * n);
  cin >> p;
  auto a = vec<int>(n + 2, n + 2);
  FOR(i, 0, n) FOR(j, 0, n) a[i + 1][j + 1] = i * n + j + 1;

  vector<pair<int, int>> wh(n * n);
  FOR(i, 0, n * n) wh[i] = make_pair(i / n + 1, i % n + 1);
  VI cost(n * n + 1, 1);
  cost[0] = 0;

  auto dis = defvec<int>(INF, n + 2, n + 2);
  queue<pair<int, int>> bfs;
  int dy[] = {-1, 0, 0, 1}, dx[] = {0, -1, 1, 0};
  function<void()> solve = [&]() {
    while (!bfs.empty()) {
      int y, x;
      tie(y, x) = bfs.front();
      bfs.pop();
      FOR(i, 0, 4) {
        int ny = y + dy[i], nx = x + dx[i];
        if (out(ny, nx, n + 2, n + 2))
          continue;
        if (!chmin(dis[ny][nx], dis[y][x] + cost[a[ny][nx]]))
          continue;
        bfs.emplace(ny, nx);
      }
    }
  };
  dis[0][0] = 0;
  bfs.emplace(0, 0);
  solve();

  ll ans = 0;
  FOR(i, 0, n * n) {
    auto [y, x] = wh[p[i] - 1];
    int d = INF;
    FOR(j, 0, 4) {
      int ny = y + dy[j], nx = x + dx[j];
      chmin(d, dis[ny][nx]);
    }
    cost[p[i]] = 0;
    dis[y][x] = d;
    bfs.emplace(y, x);
    solve();
    ans += dis[y][x];
  }

  print(ans);

  return 0;
}


Error: TLE

Fixed submission:

// 四則演算 #pragma GCC target("avx")
// 並列計算 #pragma GCC optimize("O3")
// 条件分岐を減らす #pragma GCC optimize("unroll-loops")
// 浮動小数点演算 #pragma GCC
// target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using VI = vector<int>;
using VL = vector<ll>;
using VS = vector<string>;
template <class T> using PQ = priority_queue<T, vector<T>, greater<T>>;
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define eFOR(i, a, n) for (int i = (a); i <= (n); ++i)
#define rFOR(i, a, n) for (int i = (n)-1; i >= (a); --i)
#define erFOR(i, a, n) for (int i = (n); i >= (a); --i)
#define each(i, a) for (auto &i : a)
#define SORT(i) sort(i.begin(), i.end())
#define rSORT(i) sort(i.rbegin(), i.rend())
#define fSORT(i, a) sort(i.begin(), i.end(), a)
#define all(i) i.begin(), i.end()
#define out(y, x, h, w) ((y) < 0 || (h) <= (y) || (x) < 0 || (w) <= (x))
#define line cout << "-----------------------------\\n"
#define ENDL(i, n) ((i) == (n)-1 ? "\\n" : " ")
#define stop system("pause")
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
constexpr ld eps = 1e-10;
constexpr ld pi = 3.1415926535897932;
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
inline void init() {
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}
template <class T> inline istream &operator>>(istream &is, vector<T> &v) {
  for (auto &a : v)
    is >> a;
  return is;
}
template <class T> inline istream &operator>>(istream &is, deque<T> &v) {
  for (auto &a : v)
    is >> a;
  return is;
}
template <class T, class U>
inline istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <class T> inline vector<T> vec(size_t a) { return vector<T>(a); }
template <class T> inline vector<T> defvec(T def, size_t a) {
  return vector<T>(a, def);
}
template <class T, class... Ts> inline auto vec(size_t a, Ts... ts) {
  return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...));
}
template <class T, class... Ts> inline auto defvec(T def, size_t a, Ts... ts) {
  return vector<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...));
}
inline void print() { cout << "\\n"; }
template <class T, class... Ts> inline void print(const T &a, const Ts &...ts) {
  cout << a << " ";
  print(ts...);
}
template <class T> inline void print(const vector<T> &v) {
  for (int i = 0; i < v.size(); ++i)
    cout << v[i] << (i == v.size() - 1 ? "\\n" : " ");
}
template <class T> inline void print(const vector<vector<T>> &v) {
  for (auto &a : v)
    print(a);
}
inline string reversed(const string &s) {
  string t = s;
  reverse(all(t));
  return t;
}

int main() {
  init();

  int n;
  cin >> n;
  VI p(n * n);
  cin >> p;
  auto a = vec<int>(n + 2, n + 2);
  FOR(i, 0, n) FOR(j, 0, n) a[i + 1][j + 1] = i * n + j + 1;

  vector<pair<int, int>> wh(n * n);
  FOR(i, 0, n * n) wh[i] = make_pair(i / n + 1, i % n + 1);
  VI cost(n * n + 1, 1);
  cost[0] = 0;

  auto dis = defvec<int>(INF, n + 2, n + 2);
  PQ<tuple<ll, int, int>> dik;
  int dy[] = {-1, 0, 0, 1}, dx[] = {0, -1, 1, 0};
  function<void()> solve = [&]() {
    while (!dik.empty()) {
      int d, y, x;
      tie(d, y, x) = dik.top();
      dik.pop();
      if (dis[y][x] < d)
        continue;
      FOR(i, 0, 4) {
        int ny = y + dy[i], nx = x + dx[i];
        if (out(ny, nx, n + 2, n + 2))
          continue;
        if (!chmin(dis[ny][nx], d + cost[a[ny][nx]]))
          continue;
        dik.emplace(d + cost[a[ny][nx]], ny, nx);
      }
    }
  };
  dis[0][0] = 0;
  dik.emplace(0, 0, 0);
  solve();

  ll ans = 0;
  FOR(i, 0, n * n) {
    auto [y, x] = wh[p[i] - 1];
    int d = INF;
    FOR(j, 0, 4) {
      int ny = y + dy[j], nx = x + dx[j];
      if (out(ny, nx, n + 2, n + 2))
        continue;
      chmin(d, dis[ny][nx]);
    }
    cost[p[i]] = 0;
    dis[y][x] = d;
    dik.emplace(d, y, x);
    solve();
    ans += dis[y][x];
  }

  print(ans);

  return 0;
}