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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i <= (n); i++)
#define zep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x, m, n)                                                        \\
  for (int i = (m); i <= n; i++) {                                             \\
    cout << (x[i]) << " ";                                                     \\
  }                                                                            \\
  cout << endl;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n;
  cin >> n;
  ll p[n * n];
  zep(i, 0, n * n) {
    cin >> p[i];
    p[i]--;
  }

  ll cnt[n][n];
  zep(i, 0, n) zep(j, 0, n) cnt[i][j] =
      min(min(i, n - 1 - i), min(j, n - 1 - j));

  ll ans = 0;
  bool out[n][n];
  memset(out, 0, sizeof(out));
  zep(i, 0, n * n) {
    ll x = p[i] / n, y = p[i] % n;
    out[x][y] = true;

    ans += cnt[x][y];

    queue<vector<ll>> q;
    q.push({x, y, cnt[x][y]});

    while (!q.empty()) {
      auto v = q.front();
      q.pop();
      if (!out[v[0]][v[1]])
        v[2]++;

      if (v[0] != 0 && cnt[v[0] - 1][v[1]] > v[2]) {
        cnt[v[0] - 1][v[1]] = v[2];
        q.push({v[0] - 1, v[1], v[2]});
      }
      if (v[0] != n - 1 && cnt[v[0] + 1][v[1]] > v[2]) {
        q.push({v[0] + 1, v[1], v[2]});
        cnt[v[0] + 1][v[1]] = v[2];
      }
      if (v[1] != 0 && cnt[v[0]][v[1] - 1] > v[2]) {
        q.push({v[0], v[1] - 1, v[2]});
        cnt[v[0]][v[1] - 1] = v[2];
      }
      if (v[1] != n - 1 && cnt[v[0]][v[1] + 1] > v[2]) {
        q.push({v[0], v[1] + 1, v[2]});
        cnt[v[0]][v[1] + 1] = v[2];
      }
    }
  }
  print(ans)

      return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i <= (n); i++)
#define zep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x, m, n)                                                        \\
  for (int i = (m); i <= n; i++) {                                             \\
    cout << (x[i]) << " ";                                                     \\
  }                                                                            \\
  cout << endl;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n;
  cin >> n;
  ll p[n * n];
  zep(i, 0, n * n) {
    cin >> p[i];
    p[i]--;
  }

  ll cnt[n][n];
  zep(i, 0, n) zep(j, 0, n) cnt[i][j] =
      min(min(i, n - 1 - i), min(j, n - 1 - j));

  ll ans = 0;
  bool out[n][n];
  memset(out, 0, sizeof(out));
  zep(i, 0, n * n) {
    ll x = p[i] / n, y = p[i] % n;
    out[x][y] = true;

    ans += cnt[x][y];

    queue<vector<ll>> q;
    q.push({x, y, cnt[x][y]});

    while (!q.empty()) {
      auto v = q.front();
      q.pop();
      cnt[v[0]][v[1]] = v[2];
      if (!out[v[0]][v[1]])
        v[2]++;

      if (v[0] != 0 && cnt[v[0] - 1][v[1]] > v[2])
        q.push({v[0] - 1, v[1], v[2]});
      if (v[0] != n - 1 && cnt[v[0] + 1][v[1]] > v[2])
        q.push({v[0] + 1, v[1], v[2]});
      if (v[1] != 0 && cnt[v[0]][v[1] - 1] > v[2])
        q.push({v[0], v[1] - 1, v[2]});
      if (v[1] != n - 1 && cnt[v[0]][v[1] + 1] > v[2])
        q.push({v[0], v[1] + 1, v[2]});
    }
  }
  print(ans)

      return 0;
}