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

#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,avx512f")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <functional>
#include <immintrin.h>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#ifdef DEBUG
#include "./Lib/Timer.hpp"
#include "./Lib/debug_VC.hpp"
#include "./Lib/sample.hpp"
#else
#define dump(...)
#endif

/* macro */
#define FOR(i, b, e) for (ll i = (ll)(b); i < (ll)(e); ++i)
#define RFOR(i, b, e) for (ll i = (ll)(e - 1); i >= (ll)(b); --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define REPC(x, c) for (const auto &x : (c))
#define REPI2(it, b, e) for (auto it = (b); it != (e); ++it)
#define REPI(it, c) REPI2(it, (c).begin(), (c).end())
#define RREPI(it, c) REPI2(it, (c).rbegin(), (c).rend())
#define REPI_ERACE2(it, b, e) for (auto it = (b); it != (e);)
#define REPI_ERACE(it, c) REPI_ERACE2(it, (c).begin(), (c).end())
#define ALL(x) (x).begin(), (x).end()
#define cauto const auto &
/* macro func */
template <class T> inline auto sort(T &t) { std::sort(ALL(t)); }
template <class T> inline auto rsort(T &t) {
  std::sort((t).rbegin(), (t).rend());
}
template <class T> inline auto unique(T &t) {
  (t).erase(unique((t).begin(), (t).end()), (t).end());
}
template <class T, class S> inline auto chmax(T &t, const S &s) {
  if (s > t) {
    t = s;
    return true;
  }
  return false;
}
template <class T, class S> inline auto chmin(T &t, const S &s) {
  if (s < t) {
    t = s;
    return true;
  }
  return false;
}
inline auto BR() { std::cout << "\\n"; }

/* type define */
using ll = long long;
using PAIR = std::pair<ll, ll>;
using VS = std::vector<std::string>;
using VL = std::vector<long long>;
using VVL = std::vector<VL>;
using VVVL = std::vector<VVL>;
using VD = std::vector<double>;
template <class T> using V = std::vector<T>;

/* using std */
using std::cout;
constexpr char endl = '\\n';
using std::bitset;
using std::cin;
using std::list;
using std::map;
using std::multimap;
using std::multiset;
using std::pair;
using std::priority_queue;
using std::queue;
using std::set;
using std::stack;
using std::string;
using std::unordered_map;
using std::unordered_multimap;
using std::unordered_multiset;
using std::unordered_set;
using std::vector;

/* Initial processing  */
struct Preprocessing {
  Preprocessing() {
    std::cin.tie(0);
    std::ios::sync_with_stdio(0);
  };
} _Preprocessing;

/* Remove the source of the bug */
const signed pow(signed, signed) {
  assert(false);
  return -1;
}

/* define hash */
namespace std {
template <> class hash<std::pair<ll, ll>> {
public:
  size_t operator()(const std::pair<ll, ll> &x) const {
    return hash<ll>()(1000000000 * x.first + x.second);
  }
};
} // namespace std

/* input */
template <class T> std::istream &operator>>(std::istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

/* constant value */
constexpr ll MOD = 1000000007;
// constexpr ll MOD = 998244353;

//=============================================================================================

signed main() {
  ll n;
  cin >> n;
  VL p(n * n);
  cin >> p;

  VVL cost(n, VL(n));
  REP(k, (n + 1) / 2) FOR(i, k, n - k) {
    cost[k][i] = k;
    cost[i][k] = k;
    cost[i][n - k - 1] = k;
    cost[n - k - 1][i] = k;
  }

  V<PAIR> order;
  order.reserve(n * n);
  VVL table(n, VL(n));
  REP(i, n * n) {
    order.emplace_back((p[i] - 1) / n, (p[i] - 1) % n);
    table[(p[i] - 1) / n][(p[i] - 1) % n] = i;
  }

  ll ans = 0;
  REP(k, n * n) {
    auto [fy, fx] = order[k];
    ans += cost[fy][fx];

    queue<PAIR> q;
    auto ad = [&](ll y, ll x, ll ty, ll tx) {
      if (y < 0 || x < 0 || y >= n || x >= n) {
        return;
      }
      if (cost[y][x] <= 0) {
        return;
      }
      if (cost[y][x] > cost[ty][tx] + (table[ty][tx] > k)) {
        q.emplace(y, x);
        --cost[y][x];
      }
    };
    q.emplace(fy, fx);

    while (!q.empty()) {
      auto [y, x] = q.front();
      q.pop();
      ad(y + 1, x, y, x);
      ad(y - 1, x, y, x);
      ad(y, x - 1, y, x);
      ad(y, x + 1, y, x);
    }
  }
  cout << ans << endl;

  dump(table);
  dump(cost);
}

Error: TLE

Fixed submission:

#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,avx512f")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <functional>
#include <immintrin.h>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#ifdef DEBUG
#include "./Lib/Timer.hpp"
#include "./Lib/debug_VC.hpp"
#include "./Lib/sample.hpp"
#else
#define dump(...)
#endif

/* macro */
#define FOR(i, b, e) for (ll i = (ll)(b); i < (ll)(e); ++i)
#define RFOR(i, b, e) for (ll i = (ll)(e - 1); i >= (ll)(b); --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define REPC(x, c) for (const auto &x : (c))
#define REPI2(it, b, e) for (auto it = (b); it != (e); ++it)
#define REPI(it, c) REPI2(it, (c).begin(), (c).end())
#define RREPI(it, c) REPI2(it, (c).rbegin(), (c).rend())
#define REPI_ERACE2(it, b, e) for (auto it = (b); it != (e);)
#define REPI_ERACE(it, c) REPI_ERACE2(it, (c).begin(), (c).end())
#define ALL(x) (x).begin(), (x).end()
#define cauto const auto &
/* macro func */
template <class T> inline auto sort(T &t) { std::sort(ALL(t)); }
template <class T> inline auto rsort(T &t) {
  std::sort((t).rbegin(), (t).rend());
}
template <class T> inline auto unique(T &t) {
  (t).erase(unique((t).begin(), (t).end()), (t).end());
}
template <class T, class S> inline auto chmax(T &t, const S &s) {
  if (s > t) {
    t = s;
    return true;
  }
  return false;
}
template <class T, class S> inline auto chmin(T &t, const S &s) {
  if (s < t) {
    t = s;
    return true;
  }
  return false;
}
inline auto BR() { std::cout << "\\n"; }

/* type define */
using ll = long long;
using PAIR = std::pair<ll, ll>;
using VS = std::vector<std::string>;
using VL = std::vector<long long>;
using VVL = std::vector<VL>;
using VVVL = std::vector<VVL>;
using VD = std::vector<double>;
template <class T> using V = std::vector<T>;

/* using std */
using std::cout;
constexpr char endl = '\\n';
using std::bitset;
using std::cin;
using std::list;
using std::map;
using std::multimap;
using std::multiset;
using std::pair;
using std::priority_queue;
using std::queue;
using std::set;
using std::stack;
using std::string;
using std::unordered_map;
using std::unordered_multimap;
using std::unordered_multiset;
using std::unordered_set;
using std::vector;

/* Initial processing  */
struct Preprocessing {
  Preprocessing() {
    std::cin.tie(0);
    std::ios::sync_with_stdio(0);
  };
} _Preprocessing;

/* Remove the source of the bug */
const signed pow(signed, signed) {
  assert(false);
  return -1;
}

/* define hash */
namespace std {
template <> class hash<std::pair<ll, ll>> {
public:
  size_t operator()(const std::pair<ll, ll> &x) const {
    return hash<ll>()(1000000000 * x.first + x.second);
  }
};
} // namespace std

/* input */
template <class T> std::istream &operator>>(std::istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

/* constant value */
constexpr ll MOD = 1000000007;
// constexpr ll MOD = 998244353;

//=============================================================================================

signed main() {
  ll n;
  cin >> n;
  VL p(n * n);
  cin >> p;

  VVL cost(n, VL(n));
  REP(k, (n + 1) / 2) FOR(i, k, n - k) {
    cost[k][i] = k;
    cost[i][k] = k;
    cost[i][n - k - 1] = k;
    cost[n - k - 1][i] = k;
  }

  V<PAIR> order;
  order.reserve(n * n);
  VVL table(n, VL(n));
  REP(i, n * n) {
    order.emplace_back((p[i] - 1) / n, (p[i] - 1) % n);
    table[(p[i] - 1) / n][(p[i] - 1) % n] = i;
  }

  dump(cost);
  ll ans = 0;
  REP(k, n * n) {
    auto [fy, fx] = order[k];
    /*
    if (cost[fy][fx] > 0) {
            cout << k << " " << cost[fy][fx] << endl;
    }*/
    ans += cost[fy][fx];

    unordered_set<PAIR> use;
    queue<PAIR> q;
    auto ad = [&](ll y, ll x, ll ty, ll tx) {
      if (y < 0 || x < 0 || y >= n || x >= n) {
        return;
      }
      if (use.find(PAIR{y, x}) != use.end()) {
        return;
      }
      if (cost[y][x] <= 0) {
        return;
      }
      if (cost[y][x] > cost[ty][tx] + (table[ty][tx] > k)) {
        use.emplace(y, x);
        q.emplace(y, x);
        --cost[y][x];
        // cout << table[y][x] << " to " << cost[y][x] << endl;
      }
    };
    q.emplace(fy, fx);
    use.emplace(fy, fx);

    while (!q.empty()) {
      auto [y, x] = q.front();
      q.pop();

      ad(y + 1, x, y, x);
      ad(y - 1, x, y, x);
      ad(y, x - 1, y, x);
      ad(y, x + 1, y, x);
    }
  }
  cout << ans << endl;

  dump(table);
  dump(cost);
}