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

#include <bits/stdc++.h>
using namespace std;

#define FOR(s, i, n) for (int i = s; i n; i++)
typedef long long ll;
const int INF = 1e9;

#define onEdge(i, j) (i == 0 || i == n - 1 || j == 0 || j == n - 1)
#define inBound(i, j) !(i < 0 || i > n - 1 || j < 0 || j > n - 1)

vector<pair<int, int>> order;
vector<vector<int>> dist;
vector<vector<bool>> seats;
vector<vector<bool>> inq;
const int dx[4] = {0, -1, 0, 1};
const int dy[4] = {-1, 0, 1, 0};
int n;

void updDist(int x1, int y1) {
  vector<pair<int, int>> q;
  q.push_back({x1, y1});
  inq[x1][y1] = 1;
  for (int i = 0; i < q.size(); i++) {
    auto u = q[i];
    int x = u.first;
    int y = u.second;
    inq[x][y] = 0;
    int newDist = dist[x][y] + seats[x][y];
    for (int d = 0; d < 4; d++) {
      int x2, y2;
      x2 = x + dx[d];
      y2 = y + dy[d];
      if (inBound(x2, y2)) {
        if (newDist < dist[x2][y2]) {
          dist[x2][y2] = newDist;
          if (!inq[x2][y2]) {
            inq[x2][y2] = 1;
            q.push_back({x2, y2});
          }
        }
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n;
  order.resize(n * n);
  seats.assign(n, vector<bool>(n, 1));
  dist.assign(n, vector<int>(n, INF));
  inq.assign(n, vector<bool>(n, 0));
  for (auto &it : order) {
    int a;
    cin >> a;
    a--;
    int x = a / n;
    int y = a % n;
    it.first = x;
    it.second = y;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      // if (onEdge(i, j)){
      // 	dist[i][j] = 0;
      // 	updDist(i, j);
      // }
      dist[i][j] = min(min(i, j), min(n - 1 - i, n - 1 - j));
      // cout<<dist[i][j];
    }
    // cout<<'\\n';
  }
  int ans = 0;
  for (auto u : order) {
    ans += dist[u.first][u.second];
    seats[u.first][u.second] = 0;
    updDist(u.first, u.second);
  }
  cout << ans << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define FOR(s, i, n) for (int i = s; i n; i++)
typedef long long ll;
const int INF = 1e9;

#define onEdge(i, j) (i == 0 || i == n - 1 || j == 0 || j == n - 1)
#define inBound(i, j) !(i < 0 || i > n - 1 || j < 0 || j > n - 1)

vector<pair<int, int>> order;
vector<vector<int>> dist;
vector<vector<bool>> seats;
vector<vector<bool>> inq;
const int dx[4] = {0, -1, 0, 1};
const int dy[4] = {-1, 0, 1, 0};
int n;

void updDist(int x1, int y1) {
  vector<pair<int, int>> q;
  q.push_back({x1, y1});
  for (int i = 0; i < q.size(); i++) {
    auto u = q[i];
    int x = u.first;
    int y = u.second;
    inq[x][y] = 0;
    int newDist = dist[x][y] + seats[x][y];
    for (int d = 0; d < 4; d++) {
      int x2, y2;
      x2 = x + dx[d];
      y2 = y + dy[d];
      if (inBound(x2, y2)) {
        if (newDist < dist[x2][y2]) {
          dist[x2][y2] = newDist;
          if (!inq[x2][y2]) {
            inq[x2][y2] = 1;
            q.push_back({x2, y2});
          }
        }
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n;
  order.resize(n * n);
  seats.assign(n, vector<bool>(n, 1));
  dist.assign(n, vector<int>(n, INF));
  inq.assign(n, vector<bool>(n, 0));
  for (auto &it : order) {
    int a;
    cin >> a;
    a--;
    int x = a / n;
    int y = a % n;
    it.first = x;
    it.second = y;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (onEdge(i, j)) {
        dist[i][j] = 0;
        updDist(i, j);
      }
      // cout<<dist[i][j];
    }
    // cout<<'\\n';
  }
  int ans = 0;
  for (auto u : order) {
    ans += dist[u.first][u.second];
    seats[u.first][u.second] = 0;
    updDist(u.first, u.second);
  }
  cout << ans << '\\n';
  return 0;
}