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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

/* debug macros */
#ifdef WAFDAYO
#define DBG_LINE()                                                             \\
  { std::cerr << "\\e[2m[L" << __LINE__ << "]\\e[m "; }
#define DBG_PRINT(s, t, u)                                                     \\
  { std::cerr << (s) << " \\e[2m=\\e[m \\e[1m" << (t) << "\\e[m" << (u); }
#define SELECT_7TH(x1, x2, x3, x4, x5, x6, x7, ...) x7
#define dbg1(x1) DBG_PRINT(#x1, x1, std::endl)
#define dbg2(x1, x2) DBG_PRINT(#x1, x1, ", ") dbg1(x2)
#define dbg3(x1, x2, x3) DBG_PRINT(#x1, x1, ", ") dbg2(x2, x3)
#define dbg4(x1, x2, x3, x4) DBG_PRINT(#x1, x1, ", ") dbg3(x2, x3, x4)
#define dbg5(x1, x2, x3, x4, x5) DBG_PRINT(#x1, x1, ", ") dbg4(x2, x3, x4, x5)
#define dbg6(x1, x2, x3, x4, x5, x6)                                           \\
  DBG_PRINT(#x1, x1, ", ") dbg5(x2, x3, x4, x5, x6)
#define dbg(...)                                                               \\
  DBG_LINE()                                                                   \\
  SELECT_7TH(__VA_ARGS__, dbg6, dbg5, dbg4, dbg3, dbg2, dbg1)(__VA_ARGS__)
#else
#define dbg(...)                                                               \\
  {}
#endif

/* utility functions */
struct read_item {
  read_item(){};
  template <class T> operator T() {
    T t;
    std::cin >> t;
    return t;
  }
};
char splf(int i, int n) { return (i + 1 < n) ? ' ' : '\\n'; };
template <bool up> struct _RI {
  int i;
  _RI(int a) : i(a) {}
  int operator*() { return i; }
  void operator++() { i += (up ? 1 : -1); }
  bool operator!=(const _RI &r) { return up ? i < r.i : i >= r.i; }
};
template <bool up> struct _RX {
  _RI<up> x, y;
  _RI<up> begin() { return x; }
  _RI<up> end() { return y; }
  _RX(int a, int b) : x(up ? a : (a - 1)), y(b) {}
  _RX(int a) : _RX(up ? 0 : a, up ? a : 0) {}
};
typedef _RX<true> range;
typedef _RX<false> revrange;

/* types and constants */
typedef int64_t i64;
typedef uint64_t u64;
// const i64 inf = (i64)1.05e18;
// const int inf = (int)1.05e9;

using namespace std;

const int DXS[] = {0, -1, 1, 0};
const int DYS[] = {-1, 0, 0, 1};

int main() {

  const int n = read_item();
  vector<int> ps(n * n);
  for (auto i : range(n * n)) {
    ps[i] = read_item();
    ps[i] = ps[i] - 1;
  }

  vector<vector<int>> dist(n);
  vector<vector<bool>> seat(n);
  for (auto &d : dist) {
    d.resize(n, 0);
  }
  for (auto &s : seat) {
    s.resize(n, true);
  }
  for (auto i : range(n)) {
    for (auto j : range(n)) {
      dist[i][j] = min(min(i, n - 1 - i), min(j, n - 1 - j));
    }
  }

  i64 ans = 0;
  deque<pair<pair<int, int>, int>> q;

  for (auto p_it : range(n * n)) {

    int p = ps[p_it];
    int sx = p / n;
    int sy = p % n;

    ans += dist[sx][sy];
    seat[sx][sy] = false;

    for (auto i : range(4)) {
      int x = sx + DXS[i];
      int y = sy + DYS[i];
      if (x >= 0 && y >= 0 && x < n && y < n) {
        q.push_back({{x, y}, dist[sx][sy]});
      }
    }

    while (!q.empty()) {

      int cx = q.front().first.first;
      int cy = q.front().first.second;
      int nd = q.front().second;
      q.pop_front();

      if (dist[cx][cy] <= nd) {
        continue;
      }
      dist[cx][cy] = nd;

      for (auto i : range(4)) {
        int x = cx + DXS[i];
        int y = cy + DYS[i];
        if (x >= 0 && y >= 0 && x < n && y < n) {
          if (seat[cx][cy]) {
            q.push_back({{x, y}, nd + 1});
          } else {
            q.push_front({{x, y}, nd});
          }
        }
      }
    }
  }

  printf("%lld\\n", ans);

  return 0;
}

/* wafdayo~~~ */


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

/* debug macros */
#ifdef WAFDAYO
#define DBG_LINE()                                                             \\
  { std::cerr << "\\e[2m[L" << __LINE__ << "]\\e[m "; }
#define DBG_PRINT(s, t, u)                                                     \\
  { std::cerr << (s) << " \\e[2m=\\e[m \\e[1m" << (t) << "\\e[m" << (u); }
#define SELECT_7TH(x1, x2, x3, x4, x5, x6, x7, ...) x7
#define dbg1(x1) DBG_PRINT(#x1, x1, std::endl)
#define dbg2(x1, x2) DBG_PRINT(#x1, x1, ", ") dbg1(x2)
#define dbg3(x1, x2, x3) DBG_PRINT(#x1, x1, ", ") dbg2(x2, x3)
#define dbg4(x1, x2, x3, x4) DBG_PRINT(#x1, x1, ", ") dbg3(x2, x3, x4)
#define dbg5(x1, x2, x3, x4, x5) DBG_PRINT(#x1, x1, ", ") dbg4(x2, x3, x4, x5)
#define dbg6(x1, x2, x3, x4, x5, x6)                                           \\
  DBG_PRINT(#x1, x1, ", ") dbg5(x2, x3, x4, x5, x6)
#define dbg(...)                                                               \\
  DBG_LINE()                                                                   \\
  SELECT_7TH(__VA_ARGS__, dbg6, dbg5, dbg4, dbg3, dbg2, dbg1)(__VA_ARGS__)
#else
#define dbg(...)                                                               \\
  {}
#endif

/* utility functions */
struct read_item {
  read_item(){};
  template <class T> operator T() {
    T t;
    std::cin >> t;
    return t;
  }
};
char splf(int i, int n) { return (i + 1 < n) ? ' ' : '\\n'; };
template <bool up> struct _RI {
  int i;
  _RI(int a) : i(a) {}
  int operator*() { return i; }
  void operator++() { i += (up ? 1 : -1); }
  bool operator!=(const _RI &r) { return up ? i < r.i : i >= r.i; }
};
template <bool up> struct _RX {
  _RI<up> x, y;
  _RI<up> begin() { return x; }
  _RI<up> end() { return y; }
  _RX(int a, int b) : x(up ? a : (a - 1)), y(b) {}
  _RX(int a) : _RX(up ? 0 : a, up ? a : 0) {}
};
typedef _RX<true> range;
typedef _RX<false> revrange;

/* types and constants */
typedef int64_t i64;
typedef uint64_t u64;
// const i64 inf = (i64)1.05e18;
// const int inf = (int)1.05e9;

using namespace std;

const int DXS[] = {0, -1, 1, 0};
const int DYS[] = {-1, 0, 0, 1};

int main() {

  const int n = read_item();
  vector<int> ps(n * n);
  for (auto i : range(n * n)) {
    ps[i] = read_item();
    ps[i] = ps[i] - 1;
  }

  vector<vector<int>> dist(n);
  vector<vector<bool>> seat(n);
  for (auto &d : dist) {
    d.resize(n, 0);
  }
  for (auto &s : seat) {
    s.resize(n, true);
  }
  for (auto i : range(n)) {
    for (auto j : range(n)) {
      dist[i][j] = min(min(i, n - 1 - i), min(j, n - 1 - j));
    }
  }

  i64 ans = 0;

  for (auto p_it : range(n * n)) {

    int p = ps[p_it];
    int sx = p / n;
    int sy = p % n;

    ans += dist[sx][sy];
    seat[sx][sy] = false;

    deque<pair<int, int>> q;

    for (auto i : range(4)) {
      int x = sx + DXS[i];
      int y = sy + DYS[i];
      if (x >= 0 && y >= 0 && x < n && y < n) {
        q.push_back({x, y});
      }
    }

    while (!q.empty()) {

      int cx = q.front().first;
      int cy = q.front().second;
      q.pop_front();

      int d = dist[cx][cy];

      for (auto i : range(4)) {
        int x = cx + DXS[i];
        int y = cy + DYS[i];
        if (x >= 0 && y >= 0 && x < n && y < n) {
          int nd = dist[x][y] + (seat[x][y] ? 1 : 0);
          d = min(d, nd);
        }
      }

      if (d >= dist[cx][cy]) {
        continue;
      }
      dist[cx][cy] = d;

      for (auto i : range(4)) {
        int x = cx + DXS[i];
        int y = cy + DYS[i];
        if (x >= 0 && y >= 0 && x < n && y < n) {
          if (seat[cx][cy]) {
            q.push_back({x, y});
          } else {
            q.push_front({x, y});
          }
        }
      }
    }
  }

  printf("%lld\\n", ans);

  return 0;
}

/* wafdayo~~~ */