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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#define REP(_i, _a, _n) for (int _i = _a; _i <= _n; ++_i)
#define PER(_i, _a, _n) for (int _i = _n; _i >= _a; --_i)
#define hr putchar(10)
#define pb push_back
#define lc (o << 1)
#define rc (lc | 1)
#define mid ((l + r) >> 1)
#define ls lc, l, mid
#define rs rc, mid + 1, r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\\n'
#define DB(_a)                                                                 \\
  ({                                                                           \\
    REP(_i, 1, n) cout << _a[_i] << ',';                                       \\
    hr;                                                                        \\
  })
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int P = 1e9 + 7, INF = 0x3f3f3f3f;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll qpow(ll a, ll n) {
  ll r = 1 % P;
  for (a %= P; n; a = a * a % P, n >>= 1)
    if (n & 1)
      r = r * a % P;
  return r;
}
ll inv(ll x) { return x <= 1 ? 1 : inv(P % x) * (P - P / x) % P; }
inline int rd() {
  int x = 0;
  char p = getchar();
  while (p < '0' || p > '9')
    p = getchar();
  while (p >= '0' && p <= '9')
    x = x * 10 + p - '0', p = getchar();
  return x;
}
// head

const int N = 555;
const int dx[] = {0, 0, -1, 1};
const int dy[] = {-1, 1, 0, 0};
int n, a[N][N], vis[N * N], ans[N * N];
pii pos[N * N];
queue<int> q;
int main() {
  scanf("%d", &n);
  int now = 0;
  REP(i, 1, n) REP(j, 1, n) {
    a[i][j] = ++now, vis[now] = 1;
    pos[now] = pii(i, j);
    ans[now] = min({i - 1, j - 1, n - i, n - j});
  }
  int tot = 0;
  REP(i, 1, n * n) {
    int t;
    scanf("%d", &t);
    tot += ans[t], vis[t] = 0;
    q.push(t);
    while (q.size()) {
      int u(q.front());
      q.pop();
      REP(k, 0, 3) {
        int x = pos[u].x + dx[k], y = pos[u].y + dy[k];
        if (1 <= x && x <= n && 1 <= y && y <= n &&
            ans[a[x][y]] > ans[u] + vis[u]) {
          ans[a[x][y]] = ans[u] + vis[u];
          q.push(a[x][y]);
        }
      }
    }
  }
  printf("%d\\n", tot);
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#define REP(_i, _a, _n) for (int _i = _a; _i <= _n; ++_i)
#define PER(_i, _a, _n) for (int _i = _n; _i >= _a; --_i)
#define hr putchar(10)
#define pb push_back
#define lc (o << 1)
#define rc (lc | 1)
#define mid ((l + r) >> 1)
#define ls lc, l, mid
#define rs rc, mid + 1, r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\\n'
#define DB(_a)                                                                 \\
  ({                                                                           \\
    REP(_i, 1, n) cout << _a[_i] << ',';                                       \\
    hr;                                                                        \\
  })
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int P = 1e9 + 7, INF = 0x3f3f3f3f;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll qpow(ll a, ll n) {
  ll r = 1 % P;
  for (a %= P; n; a = a * a % P, n >>= 1)
    if (n & 1)
      r = r * a % P;
  return r;
}
ll inv(ll x) { return x <= 1 ? 1 : inv(P % x) * (P - P / x) % P; }
inline int rd() {
  int x = 0;
  char p = getchar();
  while (p < '0' || p > '9')
    p = getchar();
  while (p >= '0' && p <= '9')
    x = x * 10 + p - '0', p = getchar();
  return x;
}
// head

const int N = 555;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {-1, 1, 0, 0};
int n, a[N][N], vis[N * N], d[N * N], c[N * N], clk;
int ans[N][N];
pii pos[N * N];
deque<int> q;
int main() {
  scanf("%d", &n);
  int now = 0;
  REP(i, 1, n) REP(j, 1, n) {
    a[i][j] = ++now;
    pos[now] = pii(i, j);
    c[now] = 1;
    int u = min(i - 1, j - 1);
    int v = min(n - i, n - j);
    ans[i][j] = min(u, v);
  }
  int tot = 0;
  REP(t, 1, n * n) {
    q.clear();
    int s;
    scanf("%d", &s);
    int x = pos[s].x, y = pos[s].y;
    c[s] = 0;
    if (!ans[x][y])
      continue;
    q.push_back(s);
    vis[s] = ++clk, d[s] = 0;
    while (q.size()) {
      int u(q.back());
      q.pop_back();
      if (!d[u] && !ans[pos[u].x][pos[u].y] || pos[u].x == 1 || pos[u].x == n ||
          pos[u].y == 1 || pos[u].y == n) {
        tot += d[u];
        if (!d[u]) {
          REP(k, 0, 3) {
            int xx = x + dx[k], yy = y + dy[k];
            ans[xx][yy] = 0;
          }
        }
        break;
      }
      REP(k, 0, 3) {
        int x = pos[u].x + dx[k], y = pos[u].y + dy[k];
        if (1 <= x && x <= n && 1 <= y && y <= n && vis[a[x][y]] != clk) {
          d[a[x][y]] = d[u] + c[a[x][y]];
          vis[a[x][y]] = clk;
          if (c[a[x][y]])
            q.push_front(a[x][y]);
          else
            q.push_back(a[x][y]);
        }
      }
    }
  }
  printf("%d\\n", tot);
}