Problem p02670 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

int D[502][502];
bool E[502][502];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  rep(i, N + 2) rep(j, N + 2) D[i][j] = -10;
  rep1(i, N) rep1(j, N) {
    D[i][j] = min(i - 1, j - 1);
    int kari = min(N - i, N - j);
    chmin(D[i][j], kari);
    E[i][j] = 1;
  }

  ll kotae = 0;
  rep(k, N * N) {
    int p;
    cin >> p;
    p--;
    int i = p / N + 1;
    int j = p % N + 1;

    kotae += D[i][j];

    D[i][j]--;
    E[i][j] = 0;

    queue<pair<int, int>> Q;
    Q.push(mp(i, j));
    while (Q.size()) {
      auto p = Q.front();
      Q.pop();

      i = p.first;
      j = p.second;
      int d = D[i][j];

      if (D[i - 1][j] > d + E[i - 1][j]) {
        D[i - 1][j] = d + E[i - 1][j];
        Q.push(mp(i - 1, j));
      }
      if (D[i + 1][j] > d + E[i + 1][j]) {
        D[i + 1][j] = d + E[i + 1][j];
        Q.push(mp(i + 1, j));
      }
      if (D[i][j - 1] > d + E[i][j - 1]) {
        D[i][j - 1] = d + E[i][j - 1];
        Q.push(mp(i, j - 1));
      }
      if (D[i][j + 1] > d + E[i][j + 1]) {
        D[i][j + 1] = d + E[i][j + 1];
        Q.push(mp(i, j + 1));
      }
    }
  }

  co(kotae);

  Would you please return 0;
}

Error: 0

Fixed submission:

#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

int D[502][502];
bool E[502][502];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  rep(i, N + 2) rep(j, N * 2) D[i][j] = -10;
  rep1(i, N) rep1(j, N) {
    D[i][j] = min(i - 1, j - 1);
    int kari = min(N - i, N - j);
    chmin(D[i][j], kari);
    E[i][j] = 1;
  }

  ll kotae = 0;
  rep(k, N * N) {
    int p;
    cin >> p;
    p--;
    int i = p / N + 1;
    int j = p % N + 1;

    kotae += D[i][j];

    D[i][j]--;
    E[i][j] = 0;

    queue<pair<int, int>> Q;
    Q.push(mp(i, j));
    while (Q.size()) {
      auto p = Q.front();
      Q.pop();

      i = p.first;
      j = p.second;
      int d = D[i][j];

      if (D[i - 1][j] > d + E[i - 1][j]) {
        D[i - 1][j] = d + E[i - 1][j];
        Q.push(mp(i - 1, j));
      }
      if (D[i + 1][j] > d + E[i + 1][j]) {
        D[i + 1][j] = d + E[i + 1][j];
        Q.push(mp(i + 1, j));
      }
      if (D[i][j - 1] > d + E[i][j - 1]) {
        D[i][j - 1] = d + E[i][j - 1];
        Q.push(mp(i, j - 1));
      }
      if (D[i][j + 1] > d + E[i][j + 1]) {
        D[i][j + 1] = d + E[i][j + 1];
        Q.push(mp(i, j + 1));
      }
    }
  }

  co(kotae);

  Would you please return 0;
}