Problem p02670 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end)                                                     \
  for (lint i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \
  for (lint i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T1, typename T2>
pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) {
  return make_pair(l.first + r.first, l.second + r.second);
}
template <typename T1, typename T2>
pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) {
  return make_pair(l.first - r.first, l.second - r.second);
}
const lint MOD = 1e9 + 7;

lint score[500][500], N;
bool isexist[500][500];
lint arr[250000];
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cin >> N;
  REP(i, N * N) {
    cin >> arr[i];
    arr[i]--;
  }
  REP(i, N) {
    REP(j, N) {
      score[i][j] = min(min(i, N - i - 1), min(j, N - j - 1));
      isexist[i][j] = true;
    }
  }
  lint cnt = 0;
  lint dx[4] = {0, -1, 0, 1}, dy[4] = {1, 0, -1, 0};
  REP(i, N * N) {
    lint row = arr[i] / N, col = arr[i] % N;
    cnt += score[row][col];
    queue<plint> que;
    que.push(plint(row, col));
    isexist[row][col] = false;
    while (!que.empty()) {
      plint curr = que.front();
      que.pop();
      lint y = curr.first, x = curr.second;
      REP(i, 4) {
        lint nextx = x + dx[i], nexty = y + dy[i];
        if (0 <= nexty && nexty < N && 0 <= nextx && nextx < N &&
            ((isexist[y][x] && score[nexty][nextx] >= score[y][x] + 1) ||
             (!isexist[y][x] && score[nexty][nextx] > score[y][x]))) {
          if (!isexist[y][x])
            score[nexty][nextx] = score[y][x];
          else
            score[nexty][nextx] = score[y][x] + 1;
          que.push(plint(nexty, nextx));
        }
      }
    }
  }
  cout << cnt;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end)                                                     \
  for (lint i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \
  for (lint i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T1, typename T2>
pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) {
  return make_pair(l.first + r.first, l.second + r.second);
}
template <typename T1, typename T2>
pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) {
  return make_pair(l.first - r.first, l.second - r.second);
}
const lint MOD = 1e9 + 7;

lint score[500][500], N;
bool isexist[500][500];
lint arr[250000];
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cin >> N;
  REP(i, N * N) {
    cin >> arr[i];
    arr[i]--;
  }
  REP(i, N) {
    REP(j, N) {
      score[i][j] = min(min(i, N - i - 1), min(j, N - j - 1));
      isexist[i][j] = true;
    }
  }
  lint cnt = 0;
  lint dx[4] = {0, -1, 0, 1}, dy[4] = {1, 0, -1, 0};
  REP(i, N * N) {
    lint row = arr[i] / N, col = arr[i] % N;
    cnt += score[row][col];
    queue<plint> que;
    que.push(plint(row, col));
    isexist[row][col] = false;
    while (!que.empty()) {
      plint curr = que.front();
      que.pop();
      lint y = curr.first, x = curr.second;
      REP(i, 4) {
        lint nextx = x + dx[i], nexty = y + dy[i];
        if (0 <= nexty && nexty < N && 0 <= nextx && nextx < N &&
            ((isexist[y][x] && score[nexty][nextx] > score[y][x] + 1) ||
             (!isexist[y][x] && score[nexty][nextx] > score[y][x]))) {
          if (!isexist[y][x])
            score[nexty][nextx] = score[y][x];
          else
            score[nexty][nextx] = score[y][x] + 1;
          que.push(plint(nexty, nextx));
        }
      }
    }
  }
  cout << cnt;
}