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

// #define NDEBUG
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <utility>
#include <vector>

namespace n91 {

using i8 = std::int_fast8_t;
using i32 = std::int_fast32_t;
using i64 = std::int_fast64_t;
using u8 = std::uint_fast8_t;
using u32 = std::uint_fast32_t;
using u64 = std::uint_fast64_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;

struct rep {
  struct itr {
    usize i;
    constexpr itr(const usize i) noexcept : i(i) {}
    void operator++() noexcept { ++i; }
    constexpr usize operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr x) const noexcept { return i != x.i; }
  };
  const itr f, l;
  constexpr rep(const usize f, const usize l) noexcept
      : f(std::min(f, l)), l(l) {}
  constexpr auto begin() const noexcept { return f; }
  constexpr auto end() const noexcept { return l; }
};
struct revrep {
  struct itr {
    usize i;
    constexpr itr(const usize i) noexcept : i(i) {}
    void operator++() noexcept { --i; }
    constexpr usize operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr x) const noexcept { return i != x.i; }
  };
  const itr f, l;
  constexpr revrep(const usize f, const usize l) noexcept
      : f(l - 1), l(std::min(f, l) - 1) {}
  constexpr auto begin() const noexcept { return f; }
  constexpr auto end() const noexcept { return l; }
};
template <class T> auto md_vec(const usize n, const T &value) {
  return std::vector<T>(n, value);
}
template <class... Args> auto md_vec(const usize n, Args... args) {
  return std::vector<decltype(md_vec(args...))>(n, md_vec(args...));
}
template <class T> constexpr T difference(const T &a, const T &b) noexcept {
  return a < b ? b - a : a - b;
}
template <class T> void chmin(T &a, const T &b) noexcept {
  if (b < a)
    a = b;
}
template <class T> void chmax(T &a, const T &b) noexcept {
  if (a < b)
    a = b;
}
template <class F> class rec_lambda {
  F f;

public:
  rec_lambda(F &&f) : f(std::move(f)) {}
  template <class... Args> auto operator()(Args &&...args) const {
    return f(*this, std::forward<Args>(args)...);
  }
};
template <class F> auto make_rec(F &&f) { return rec_lambda<F>(std::move(f)); }
template <class T> T scan() {
  T ret;
  std::cin >> ret;
  return ret;
}
constexpr char eoln = '\\n';
template <class T> T ceildiv(const T &l, const T &r) {
  return l / r + (l % r != 0 ? 1 : 0);
}

} // namespace n91

#include <deque>

namespace n91 {

usize nxtp2(const usize n) {
  usize ret = 1;
  while (ret < n) {
    ret *= 2;
  }
  return ret;
}

struct my_deque {
  const usize m;
  std::vector<usize> c;
  usize f, b;

  my_deque(usize n) : m(nxtp2(n) - 1), c(m + 1), f(0), b(0) {}

  void push_front(const usize x) {
    --f;
    c[f & m] = x;
  }
  void push_back(const usize x) {
    c[b & m] = x;
    ++b;
  }

  usize pop_front() {
    ++f;
    return c[(f - 1) & m];
  }

  bool empty() { return f == b; }
};

void main_() {
  //*
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  //*/

  const usize n = scan<usize>();
  const usize m = n * n;

  std::vector<usize> dist(m, 0);
  for (const usize i : rep(0, m)) {
    const usize x = i % n;
    const usize y = i / n;
    dist[i] = std::min({x, n - x - 1, y, n - y - 1});
  }

  u64 ans = 0;

  my_deque deq(m);

  std::vector<usize> live(m, 1);

  for (const usize loop : rep(0, m)) {
    const usize p = scan<usize>() - 1;

    { /*
       for (const auto i : rep(0, n)) {
         for (const auto j : rep(0, n)) {
           std::cout << dist[(i * n + j) * 2] << " ";
         }
         std::cout << eoln;
       }
       std::cout << eoln;
     */
    }

    ans += dist[p];
    live[p] = 0;

    deq.push_back(p);
    while (!deq.empty()) {
      const usize i = deq.pop_front();
      usize c = dist[i];

      const usize x = i % n;
      const usize y = i / n;

      if (live[i]) {
        c += 1;
        if (x != 0) {
          const usize to = i - 1;
          auto &d = dist[to];
          if (d > c) {
            d = c;
            deq.push_back(to);
          }
        }
        if (x != n - 1) {
          const usize to = i + 1;
          auto &d = dist[to];
          if (d > c) {
            d = c;
            deq.push_back(to);
          }
        }
        if (y != 0) {
          const usize to = i - n;
          auto &d = dist[to];
          if (d > c) {
            d = c;
            deq.push_back(to);
          }
        }
        if (y != n - 1) {
          const usize to = i + n;
          auto &d = dist[to];
          if (d > c) {
            d = c;
            deq.push_back(to);
          }
        }
      } else {
        if (x != 0) {
          const usize to = i - 1;
          auto &d = dist[to];
          if (d > c) {
            d = c;
            deq.push_front(to);
          }
        }
        if (x != n - 1) {
          const usize to = i + 1;
          auto &d = dist[to];
          if (d > c) {
            d = c;
            deq.push_front(to);
          }
        }
        if (y != 0) {
          const usize to = i - n;
          auto &d = dist[to];
          if (d > c) {
            d = c;
            deq.push_front(to);
          }
        }
        if (y != n - 1) {
          const usize to = i + n;
          auto &d = dist[to];
          if (d > c) {
            d = c;
            deq.push_front(to);
          }
        }
      }
    }

    { /*
       std::cout <<p + 1 << " left"<<eoln<<eoln;
     */
    }
  }

  std::cout << ans << eoln;
}

} // namespace n91

int main() {
  n91::main_();
  return 0;
}


Error: TLE

Fixed submission:

// #define NDEBUG
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <utility>
#include <vector>

namespace n91 {

using i8 = std::int_fast8_t;
using i32 = std::int_fast32_t;
using i64 = std::int_fast64_t;
using u8 = std::uint_fast8_t;
using u32 = std::uint_fast32_t;
using u64 = std::uint_fast64_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;

struct rep {
  struct itr {
    usize i;
    constexpr itr(const usize i) noexcept : i(i) {}
    void operator++() noexcept { ++i; }
    constexpr usize operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr x) const noexcept { return i != x.i; }
  };
  const itr f, l;
  constexpr rep(const usize f, const usize l) noexcept
      : f(std::min(f, l)), l(l) {}
  constexpr auto begin() const noexcept { return f; }
  constexpr auto end() const noexcept { return l; }
};
struct revrep {
  struct itr {
    usize i;
    constexpr itr(const usize i) noexcept : i(i) {}
    void operator++() noexcept { --i; }
    constexpr usize operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr x) const noexcept { return i != x.i; }
  };
  const itr f, l;
  constexpr revrep(const usize f, const usize l) noexcept
      : f(l - 1), l(std::min(f, l) - 1) {}
  constexpr auto begin() const noexcept { return f; }
  constexpr auto end() const noexcept { return l; }
};
template <class T> auto md_vec(const usize n, const T &value) {
  return std::vector<T>(n, value);
}
template <class... Args> auto md_vec(const usize n, Args... args) {
  return std::vector<decltype(md_vec(args...))>(n, md_vec(args...));
}
template <class T> constexpr T difference(const T &a, const T &b) noexcept {
  return a < b ? b - a : a - b;
}
template <class T> void chmin(T &a, const T &b) noexcept {
  if (b < a)
    a = b;
}
template <class T> void chmax(T &a, const T &b) noexcept {
  if (a < b)
    a = b;
}
template <class F> class rec_lambda {
  F f;

public:
  rec_lambda(F &&f) : f(std::move(f)) {}
  template <class... Args> auto operator()(Args &&...args) const {
    return f(*this, std::forward<Args>(args)...);
  }
};
template <class F> auto make_rec(F &&f) { return rec_lambda<F>(std::move(f)); }
template <class T> T scan() {
  T ret;
  std::cin >> ret;
  return ret;
}
constexpr char eoln = '\\n';
template <class T> T ceildiv(const T &l, const T &r) {
  return l / r + (l % r != 0 ? 1 : 0);
}

} // namespace n91

#include <deque>

namespace n91 {

usize nxtp2(const usize n) {
  usize ret = 1;
  while (ret < n) {
    ret *= 2;
  }
  return ret;
}

struct my_deque {
  const usize m;
  std::vector<usize> c;
  usize f, b;

  my_deque(usize n) : m(nxtp2(n) - 1), c(m + 1), f(0), b(0) {}

  void push_front(const usize x) {
    --f;
    c[f & m] = x;
  }
  void push_back(const usize x) {
    c[b & m] = x;
    ++b;
  }

  usize pop_front() {
    ++f;
    return c[(f - 1) & m];
  }

  bool empty() { return f == b; }
};

void main_() {
  //*
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  //*/

  const usize n = scan<usize>();
  const usize m = n * n;

  std::vector<usize> dist(2 * m, 0);
  struct edge {
    usize to;
    usize cost;
  };
  auto g = md_vec(m * 2, 0, edge());
  for (const usize i : rep(0, m)) {
    const usize x = i % n;
    const usize y = i / n;
    dist[2 * i] = std::min({x, n - x - 1, y, n - y - 1});
    dist[2 * i + 1] = dist[2 * i] + 1;
    g[2 * i].push_back({2 * i + 1, 1});
    if (x != 0) {
      g[2 * i + 1].push_back({2 * (i - 1), 0});
    }
    if (x + 1 != n) {
      g[2 * i + 1].push_back({2 * (i + 1), 0});
    }
    if (y != 0) {
      g[2 * i + 1].push_back({2 * (i - n), 0});
    }
    if (y + 1 != n) {
      g[2 * i + 1].push_back({2 * (i + n), 0});
    }
  }

  u64 ans = 0;

  my_deque deq(m * 2);

  for (const usize loop : rep(0, m)) {
    const usize p = scan<usize>() - 1;

    { /*
       for (const auto i : rep(0, n)) {
         for (const auto j : rep(0, n)) {
           std::cout << dist[(i * n + j) * 2] << " ";
         }
         std::cout << eoln;
       }
       std::cout << eoln;
     */
    }

    ans += dist[2 * p];

    g[2 * p][0].cost = 0;

    deq.push_back(2 * p);
    while (!deq.empty()) {
      const usize v = deq.pop_front();
      const usize c = dist[v];

      for (const auto &e : g[v]) {
        if (dist[e.to] > c + e.cost) {
          dist[e.to] = c + e.cost;
          if (e.cost == 0) {
            deq.push_front(e.to);
          } else {
            deq.push_back(e.to);
          }
        }
      }
    }

    { /*
       std::cout <<p + 1 << " left"<<eoln<<eoln;
     */
    }
  }

  std::cout << ans << eoln;
}

} // namespace n91

int main() {
  n91::main_();
  return 0;
}