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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>

#ifdef DEBUG
#include "library/Utility/debug.cpp"
#else
#define debug(...)
#endif

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define EL '\\n'
#define print(i) std::cout << (i) << '\\n'
#define all(v) (v).begin(), (v).end()
using lnt = long long;
constexpr lnt INF = 2e18;
/*-*/

int main() {
  int n;
  std::cin >> n;
  std::vector<std::vector<int>> cost(n, std::vector<int>(n));
  rep(i, n) {
    rep(j, n) { cost[i][j] = std::min({i, j, n - i - 1, n - j - 1}); }
  }
  debug(cost);
  std::vector<int> p(n * n);
  rep(i, n * n) std::cin >> p[i];
  rep(i, n * n) p[i]--;
  std::vector<std::vector<int>> alive(n, std::vector<int>(n, true));
  lnt sum = 0;
  // probably n^3
  for (auto q : p) {
    int x = q / n, y = q % n;
    sum += cost[x][y];
    debug(sum);
    std::queue<std::pair<int, std::pair<int, int>>> que;
    alive[x][y] = false;
    for (int dx = -1; dx <= 1; dx++) {
      for (int dy = -1; dy <= 1; dy++) {
        if (dx * dy != 0 || (dx == 0 && dy == 0))
          continue;
        if (x + dx < 0 || x + dx >= n) {
          cost[x][y] = -1;
          continue;
        }
        if (y + dy < 0 || y + dy >= n) {
          cost[x][y] = -1;
          continue;
        }
        cost[x][y] = std::min(cost[x][y], cost[x + dx][y + dy]);
      }
    }
    debug(cost[x][y]);
    que.emplace(n - cost[x][y], std::make_pair(x, y));
    while (!que.empty()) {
      auto xx = que.front();
      que.pop();
      int i = xx.second.first, j = xx.second.second;
      for (int dx = -1; dx <= 1; dx++) {
        for (int dy = -1; dy <= 1; dy++) {
          if (i + dx < 0 || i + dx >= n)
            continue;
          if (j + dy < 0 || j + dy >= n)
            continue;
          if (dx * dy != 0 || (dx == 0 && dy == 0))
            continue;
          // either alive and over +2 or dead and over +1
          if (alive[i + dx][j + dy] && cost[i + dx][j + dy] > cost[i][j] + 1) {
            cost[i + dx][j + dy] = cost[i][j] + 1;
            que.emplace(n - cost[i + dx][j + dy],
                        std::make_pair(i + dx, j + dy));
          }
          if (!alive[i + dx][j + dy] && cost[i + dx][j + dy] > cost[i][j]) {
            cost[i + dx][j + dy] = cost[i][j];
            que.emplace(n - cost[i + dx][j + dy],
                        std::make_pair(i + dx, j + dy));
          }
        }
      }
    }
    debug(x, y);
    debug(cost);
    debug(alive);
  }
  print(sum);
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>

#ifdef DEBUG
#include "library/Utility/debug.cpp"
#else
#define debug(...)
#endif

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define EL '\\n'
#define print(i) std::cout << (i) << '\\n'
#define all(v) (v).begin(), (v).end()
using lnt = long long;
constexpr lnt INF = 2e18;
/*-*/

int main() {
  int n;
  std::cin >> n;
  std::vector<std::vector<int>> cost(n, std::vector<int>(n));
  rep(i, n) {
    rep(j, n) { cost[i][j] = std::min({i, j, n - i - 1, n - j - 1}); }
  }
  debug(cost);
  std::vector<int> p(n * n);
  rep(i, n * n) std::cin >> p[i];
  rep(i, n * n) p[i]--;
  std::vector<std::vector<int>> alive(n, std::vector<int>(n, true));
  lnt sum = 0;
  // probably n^3
  for (auto q : p) {
    int x = q / n, y = q % n;
    sum += cost[x][y];
    debug(sum);
    std::priority_queue<std::pair<int, std::pair<int, int>>> que;
    alive[x][y] = false;
    for (int dx = -1; dx <= 1; dx++) {
      for (int dy = -1; dy <= 1; dy++) {
        if (dx * dy != 0 || (dx == 0 && dy == 0))
          continue;
        if (x + dx < 0 || x + dx >= n) {
          cost[x][y] = -1;
          continue;
        }
        if (y + dy < 0 || y + dy >= n) {
          cost[x][y] = -1;
          continue;
        }
        cost[x][y] = std::min(cost[x][y], cost[x + dx][y + dy]);
      }
    }
    debug(cost[x][y]);
    que.emplace(n - cost[x][y], std::make_pair(x, y));
    while (!que.empty()) {
      auto xx = que.top();
      que.pop();
      int i = xx.second.first, j = xx.second.second;
      for (int dx = -1; dx <= 1; dx++) {
        for (int dy = -1; dy <= 1; dy++) {
          if (i + dx < 0 || i + dx >= n)
            continue;
          if (j + dy < 0 || j + dy >= n)
            continue;
          if (dx * dy != 0 || (dx == 0 && dy == 0))
            continue;
          // either alive and over +2 or dead and over +1
          if (alive[i + dx][j + dy] && cost[i + dx][j + dy] > cost[i][j] + 1) {
            cost[i + dx][j + dy] = cost[i][j] + 1;
            que.emplace(n - cost[i + dx][j + dy],
                        std::make_pair(i + dx, j + dy));
          }
          if (!alive[i + dx][j + dy] && cost[i + dx][j + dy] > cost[i][j]) {
            cost[i + dx][j + dy] = cost[i][j];
            que.emplace(n - cost[i + dx][j + dy],
                        std::make_pair(i + dx, j + dy));
          }
        }
      }
    }
    debug(x, y);
    debug(cost);
    debug(alive);
  }
  print(sum);
}