alexjercan/bugnet huggingface dataset:
Problem p02670 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n;
int cost[555][555];
bool a[555][555];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

int solve(int y, int x) {

  typedef pair<int, int> P;
  queue<P> que;

  que.push(P(y, x));

  while (que.size()) {
    int ny = que.front().first, nx = que.front().second;
    que.pop();

    for (int i = 0; i < 4; i++) {
      int xx = nx + dx[i], yy = ny + dy[i];
      if (xx < 0 || yy < 0 || xx == n || yy == n)
        continue;

      if (cost[yy][xx] > cost[ny][nx] + a[ny][nx]) {
        cost[yy][xx] = cost[ny][nx] + a[ny][nx];
        que.push(P(yy, xx));
      }
    }
  }
  return cost[y][x];
}

int main() {

  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cost[i][j] = min(min(i, j), min(n - i - 1, n - j - 1));
      a[i][j] = 1;
    }
  }

  int ans = 0;
  for (int i = 0; i < n * n; i++) {
    int p;
    cin >> p;
    p--;
    int y = p / n, x = p % n;
    a[y][x] = 0;
    ans += solve(y, x);
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define INF 1 << 28
using namespace std;

int n;
bool a[555][555];
bool b[555][555];
int memo[555][555];

int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};

int solve(int y, int x) {

  typedef pair<int, int> P;
  queue<P> que;

  int cost[555][555];
  int ret = INF;
  fill(cost[0], cost[555], INF);

  cost[y][x] = 0;
  que.push(P(y, x));

  while (que.size()) {
    int ny = que.front().first, nx = que.front().second;
    que.pop();

    for (int i = 0; i < 4; i++) {
      int xx = dx[i] + nx, yy = dy[i] + ny;
      if (xx == 0) {
        ret = min(ret, cost[ny][nx]);
        continue;
      }
      if (yy == 0) {
        ret = min(ret, cost[ny][nx]);
        continue;
      }
      if (xx > n) {
        ret = min(ret, cost[ny][nx]);
        continue;
      }
      if (yy > n) {
        ret = min(ret, cost[ny][nx]);
        continue;
      }

      // cout << ny << " " << nx << " " << yy << " " << xx << " " <<
      // cost[yy][xx] << " " << cost[ny][nx] + a[yy][xx] <<endl;
      if (cost[yy][xx] > cost[ny][nx] + a[yy][xx]) {
        cost[yy][xx] = cost[ny][nx] + a[yy][xx];
        que.push(P(yy, xx));
        // puts("hoge");
      }
    }
  }

  /*
  for(int j=1;j<=n;j++){
    for(int k=1;k<=n;k++) cout << cost[j][k] << " ";
    cout << endl;
  }
  cout << ret << endl;
  */
  return ret;
}

int main() {

  int ans = 0;
  cin >> n;
  for (int i = 0; i < 555 * 555; i++)
    a[i / 555][i % 555] = 1;

  for (int i = 0; i < n * n; i++) {
    int p;
    cin >> p;
    p--;
    int y = p / n, x = p % n;
    y++;
    x++;
    a[y][x] = 0;
    fill(memo[0], memo[555], INF);
    // memset(memo, -1, sizeof(memo));

    ans += solve(y, x);

    /*
    cout << " " << ans << endl;
    for(int j=1;j<=n;j++){
      for(int k=1;k<=n;k++) cout << a[j][k];
      cout << endl;
    }
    */
  }

  cout << ans << endl;
}
Problem p02670 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int n, m, dp[250002], a[250002], ans, b[250002];
#define re register
inline int read() {
  re int t = 0;
  re char v = getchar();
  while (v < '0')
    v = getchar();
  while (v >= '0')
    t = (t << 3) + (t << 1) + v - 48, v = getchar();
  return t;
}
inline int pos(re int x, re int y) { return (x - 1) * n + y; }
inline void dfs(re int x, re int y, re int z = 0) {
  if (y >= dp[x] && z)
    return;
  dp[x] = y;
  if (!b[x])
    ++y;
  if (x > n)
    dfs(x - n, y, 1);
  if (x <= n * n - n)
    dfs(x + n, y, 1);
  if (x % n != 1)
    dfs(x - 1, y, 1);
  if (x % n)
    dfs(x + 1, y, 1);
}
int main() {
  n = read();
  for (re int i = 1; i <= n * n; ++i)
    a[i] = read();
  for (re int i = 1; i <= n; ++i)
    for (re int j = 1; j <= n; ++j)
      dp[(i - 1) * n + j] = min(i - 1, min(j - 1, min(n - i, n - j)));
  for (re int i = 1; i <= n * n; ++i) {
    ans += dp[a[i]];
    b[a[i]] = 1;
    dfs(a[i], dp[a[i]]);
  }
  printf("%d", ans);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, m, dp[250002], a[250002], ans, b[250002];
#define re register
inline int read() {
  re int t = 0;
  re char v = getchar();
  while (v < '0')
    v = getchar();
  while (v >= '0')
    t = (t << 3) + (t << 1) + v - 48, v = getchar();
  return t;
}
inline void dfs(re int x, re int y, re int z = 0) {
  if (!z && y >= dp[x])
    return;
  if (!b[x])
    ++y;
  dp[x] = y;
  if (x > n)
    dfs(x - n, y);
  if (x <= n * n - n)
    dfs(x + n, y);
  if (x % n != 1)
    dfs(x - 1, y);
  if (x % n)
    dfs(x + 1, y);
}
int main() {
  n = read();
  for (re int i = 1; i <= n * n; ++i)
    a[i] = read();
  for (re int i = 1; i <= n; ++i)
    for (re int j = 1; j <= n; ++j)
      dp[(i - 1) * n + j] = min(i - 1, min(j - 1, min(n - i, n - j))) + 1;
  for (re int i = 1; i <= n * n; ++i) {
    ans += dp[a[i]] - 1;
    b[a[i]] = 1;
    dfs(a[i], dp[a[i]] - 1, 1);
    //	for(re int j=1;j<=n*n;++j)printf("%d:%d
    //",j,dp[j]),j%n==0?puts(""):j;if(i>=5)break;
  }
  printf("%d", ans);
}
Problem p02670 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define for_(i, a, b) for (int i = a; i < b; ++i)
#define ALL(a) a.begin(), a.end()
using lint = long long int;

const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
using pii = pair<int, int>;

int main() {
  int N;
  cin >> N;

  vector<vector<bool>> out(N, vector<bool>(N, false));
  vector<vector<int>> step(N, vector<int>(N, N));

  for_(y, 0, N) for_(x, 0, N) {
    step[y][x] = min(step[y][x], x);
    step[y][x] = min(step[y][x], N - 1 - x);
    step[y][x] = min(step[y][x], y);
    step[y][x] = min(step[y][x], N - 1 - y);
  }

  int ans = 0;
  for_(i, 0, N * N) {
    int P;
    cin >> P;
    --P;

    int sx = P % N;
    int sy = P / N;

    ans += step[sy][sx];
    out[sy][sx] = true;

    deque<pii> que;
    que.push_back({sx, sy});
    while (!que.empty()) {
      auto [x, y] = que.front();
      que.pop_front();
      for_(d, 0, 4) {
        int nx = x + dx[d], ny = y + dy[d];
        if (nx < 0 or nx >= N or ny < 0 or ny >= N)
          continue;
        if (step[ny][nx] > step[y][x] + (out[y][x] ? 0 : 1)) {
          step[ny][nx] = step[y][x] + (out[y][x] ? 0 : 1);
          if (out[y][x])
            que.push_front({nx, ny});
          else
            que.push_back({nx, ny});
        }
      }
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

#define for_(i, a, b) for (int i = a; i < b; ++i)
#define ALL(a) a.begin(), a.end()
using lint = long long int;

const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
using pii = pair<int, int>;

struct State {
  int x, y, c;
};

int main() {
  int N;
  cin >> N;

  vector<vector<bool>> out(N, vector<bool>(N, false));
  vector<vector<int>> vis(N, vector<int>(N, -1));

  int ans = 0;
  for_(i, 0, N * N) {
    int P;
    cin >> P;
    --P;

    int sx = P % N;
    int sy = P / N;
    out[sy][sx] = true;

    deque<State> que;
    que.push_back({sx, sy, 0});
    bool end = false;
    while (!que.empty() and !end) {
      auto [x, y, c] = que.front();
      que.pop_front();
      for_(d, 0, 4) {
        int nx = x + dx[d], ny = y + dy[d];
        if (nx < 0 or nx >= N or ny < 0 or ny >= N) {
          ans += c;
          end = true;
          break;
        }
        if (vis[ny][nx] != P) {
          vis[ny][nx] = P;
          if (out[ny][nx])
            que.push_front({nx, ny, c});
          else
            que.push_back({nx, ny, c + 1});
        }
      }
    }
  }
  cout << ans << endl;
}
Problem p02670 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#define maxn 505
#define ll long long
using namespace std;

const int fx[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int n, i, j, k, t, w;
int d[maxn * maxn][2], dis[maxn][maxn], bz[maxn][maxn];
int a[maxn][maxn], x, y;
ll ans;

int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++)
      dis[i][j] = min(min(i - 1, j - 1), min(n - i, n - j)), a[i][j] = 1;
  for (i = 1; i <= n * n; i++) {
    scanf("%d", &k), x = (k - 1) % n + 1, y = (k - 1) / n + 1;
    a[x][y] = 0, t = 0, w = 1, d[w][0] = x, d[w][1] = y, bz[x][y] = 1;
    ans += dis[x][y];
    while (t < w) {
      t++, x = d[t][0], y = d[t][1];
      for (k = 0; k < 4; k++) {
        int xx = x + fx[k][0], yy = y + fx[k][1];
        if (xx >= 1 && xx <= n && yy >= 1 && yy <= n) {
          if (dis[x][y] + a[x][y] < dis[xx][yy]) {
            dis[xx][yy] = dis[x][y] + a[x][y];
            if (!bz[xx][yy])
              w++, d[w][0] = xx, d[w][1] = yy, bz[xx][yy] = 1;
          }
        }
      }
      bz[x][y] = 0;
    }
  }
  printf("%lld", ans);
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#define maxn 505
#define ll long long
using namespace std;

const int fx[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int n, i, j, k, t, w;
int d[maxn * maxn][2], dis[maxn][maxn], bz[maxn][maxn], ti[maxn][maxn];
int a[maxn][maxn], x, y, lim, D[maxn][maxn];
ll ans;

int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++)
      D[i][j] = min(min(i - 1, j - 1), min(n - i, n - j)), a[i][j] = 1;
  for (i = 1; i <= n * n; i++) {
    scanf("%d", &k), x = (k - 1) % n + 1, y = (k - 1) / n + 1, a[x][y] = 0;
    t = 0, w = 1, d[w][0] = x, d[w][1] = y;
    dis[x][y] = 0, bz[x][y] = 1, ti[x][y] = i;
    lim = D[x][y];
    if (i == 13)
      printf("");
    while (t < w) {
      t++, x = d[t][0], y = d[t][1];
      for (k = 0; k < 4; k++) {
        int xx = x + fx[k][0], yy = y + fx[k][1];
        if (xx >= 1 && xx <= n && yy >= 1 && yy <= n) {
          if (ti[xx][yy] < i)
            dis[xx][yy] = 2e9, ti[xx][yy] = i;
          if (dis[x][y] + a[xx][yy] < dis[xx][yy]) {
            dis[xx][yy] = dis[x][y] + a[xx][yy];
            if (dis[xx][yy] < lim && !bz[xx][yy]) {
              w++, d[w][0] = xx, d[w][1] = yy, bz[xx][yy] = 1;
              if (dis[xx][yy] + D[xx][yy] < lim)
                lim = min(lim, dis[xx][yy] + D[xx][yy]);
            }
          }
        } else
          lim = min(lim, dis[x][y]);
      }
      bz[x][y] = 0;
    }
    ans += lim;
  }
  printf("%lld", ans);
}
Problem p02670 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

int n, i, j, k, x, y, gg;
long long dis[505][505], ans;
int bz[505][505];
int p[250005];
int list[2500005][3], head, tail, B[505][505];
int fx1[5], fx2[5];

void update(int x, int y) {
  int xx, yy, t;
  head = 0;
  tail = 1;
  list[1][1] = x;
  list[1][2] = y;
  B[x][y] = 1;
  while (head < tail) {
    head++;
    x = list[head][1];
    y = list[head][2];
    for (t = 1; t <= 4; t++) {
      xx = x + fx1[t];
      yy = y + fx2[t];
      if (dis[xx][yy] > dis[x][y] + (bz[xx][yy] ^ 1)) {
        dis[xx][yy] = dis[x][y] + (bz[xx][yy] ^ 1);
        if (B[xx][yy] == 0) {
          B[xx][yy] = 1;
          tail++;
          list[tail][1] = xx;
          list[tail][2] = yy;
        }
      }
    }
    B[x][y] = 0;
  }
}

int main() {
  //	freopen("read.in","r",stdin);
  fx1[1] = 1;
  fx1[2] = -1;
  fx1[3] = 0;
  fx1[4] = 0;
  fx2[1] = 0;
  fx2[2] = 0;
  fx2[3] = 1;
  fx2[4] = -1;
  scanf("%d", &n);
  for (i = 1; i <= n * n; i++)
    scanf("%d", &p[i]);
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      dis[i][j] = min(min(i, j), min(n - i + 1, n - j + 1));
    }
  }
  for (i = 1; i <= n * n; i++) {
    x = (p[i] - 1) / n + 1;
    y = (p[i] - 1) % n + 1;
    bz[x][y] = 1;
    dis[x][y]--;
    ans += dis[x][y];
    update(x, y);
  }
  printf("%lld", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

int n, i, j, k, x, y, gg;
long long dis[505][505], ans;
int bz[505][505];
int p[250005];
int list[2500005][3], head, tail, B[505][505];
int fx1[5], fx2[5];

void update(int x, int y) {
  int xx, yy, t;
  head = 0;
  tail = 1;
  memset(B, 0, sizeof(B));
  list[1][1] = x;
  list[1][2] = y;
  B[x][y] = 1;
  while (head < tail) {
    head++;
    x = list[head][1];
    y = list[head][2];
    for (t = 1; t <= 4; t++) {
      xx = x + fx1[t];
      yy = y + fx2[t];
      if (dis[xx][yy] > dis[x][y] + (bz[xx][yy] ^ 1)) {
        dis[xx][yy] = dis[x][y] + (bz[xx][yy] ^ 1);
        if (B[xx][yy] == 0) {
          B[xx][yy] = 1;
          tail++;
          list[tail][1] = xx;
          list[tail][2] = yy;
        }
      }
    }
    B[x][y] = 0;
  }
}

int main() {
  //	freopen("read.in","r",stdin);
  fx1[1] = 1;
  fx1[2] = -1;
  fx1[3] = 0;
  fx1[4] = 0;
  fx2[1] = 0;
  fx2[2] = 0;
  fx2[3] = 1;
  fx2[4] = -1;
  scanf("%d", &n);
  for (i = 1; i <= n * n; i++)
    scanf("%d", &p[i]);
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      dis[i][j] = min(min(i, j), min(n - i + 1, n - j + 1));
    }
  }
  for (i = 1; i <= n * n; i++) {
    x = (p[i] - 1) / n + 1;
    y = (p[i] - 1) % n + 1;
    bz[x][y] = 1;
    dis[x][y]--;
    ans += dis[x][y];
    update(x, y);
  }
  printf("%lld", ans);
  return 0;
}
Problem p02670 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<int, int>;
using VI = vector<int>;

const int N = 505;
int dis[N][N];
bool emp[N][N];
int n;

void relax(int si, int sj) {
  deque<PII> ord;
  ord.emplace_back(si, sj);
  while (!ord.empty()) {
    auto [i, j] = ord.front();
    ord.pop_front();
    for (int ii : {i - 1, i, i + 1}) {
      for (int jj : {j - 1, j, j + 1}) {
        if (abs(i - ii) + abs(j - jj) != 1)
          continue;
        if (ii < 1 || ii > n || jj < 1 || jj > n)
          continue;
        if (dis[ii][jj] > dis[i][j] + !emp[i][j]) {
          dis[ii][jj] = dis[i][j] + !emp[i][j];
          if (emp[i][j]) {
            ord.emplace_front(ii, jj);
          } else {
            ord.emplace_back(ii, jj);
          }
        }
      }
    }
  }
}

int main() {
#ifdef LOCAL_EXEC
//	freopen("sample.in", "r", stdin);
//	freopen("sample.out", "w", stdout);
#else
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#endif

  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      dis[i][j] = min({i - 1, j - 1, n - i, n - j});
    }
  }

  int ans = 0;
  for (int it = 0; it < n * n; it++) {
    int x;
    cin >> x;
    int i = ((x - 1) / n) + 1;
    int j = ((x - 1) % n) + 1;

    ans += dis[i][j];
    emp[i][j] = true;
    relax(i, j);
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<int, int>;
using VI = vector<int>;

const int N = 505;
int dis[N][N];
bool emp[N][N];
int n;

void relax(int si, int sj) {
  priority_queue<pair<int, PII>> heap;
  heap.emplace(dis[si][sj], PII{si, sj});
  while (!heap.empty()) {
    auto [i, j] = heap.top().second;
    heap.pop();
    for (int ii : {i - 1, i, i + 1}) {
      for (int jj : {j - 1, j, j + 1}) {
        if (abs(i - ii) + abs(j - jj) != 1)
          continue;
        if (ii < 1 || ii > n || jj < 1 || jj > n)
          continue;
        if (dis[ii][jj] > dis[i][j] + !emp[i][j]) {
          dis[ii][jj] = dis[i][j] + !emp[i][j];
          heap.emplace(-dis[ii][jj], PII{ii, jj});
        }
      }
    }
  }
}

int main() {
#ifdef LOCAL_EXEC
//	freopen("sample.in", "r", stdin);
//	freopen("sample.out", "w", stdout);
#else
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#endif

  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      dis[i][j] = min({i - 1, j - 1, n - i, n - j});
    }
  }

  int ans = 0;
  for (int it = 0; it < n * n; it++) {
    int x;
    cin >> x;
    int i = ((x - 1) / n) + 1;
    int j = ((x - 1) % n) + 1;

    ans += dis[i][j];
    emp[i][j] = true;
    relax(i, j);
  }
  cout << ans << endl;

  return 0;
}
Problem p02670 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U> using Pa = pair<T, U>;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  int M = N * N;
  vec<int> P(M);
  for (int i = 0; i < M; i++) {
    cin >> P[i];
    P[i]--;
  }
  vec<int> dx = {-1, 0, 1, 0}, dy = {0, -1, 0, 1};

  int inf = 1e9;
  vvec<int> cost(N, vec<int>(N, inf));
  vvec<int> vis(N, vec<int>(N, 1));

  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) {
      cost[i][j] = min({i, j, N - i - 1, N - j - 1});
    }

  auto in = [&](int x, int y) { return 0 <= x && x < N && 0 <= y && y < N; };

  int ans = 0;
  for (auto &p : P) {
    int sx = p / N, sy = p % N;
    ans += cost[sx][sy];
    vis[sx][sy] = 0;
    deque<Pa<int, int>> deq;
    deq.push_back({sx, sy});
    while (!deq.empty()) {
      auto [x, y] = deq.front();
      deq.pop_front();
      for (int k = 0; k < 4; k++) {
        int nx = x + dx[k], ny = y + dy[k];
        if (!in(nx, ny))
          continue;
        int nc = cost[x][y] + vis[x][y];
        if (cost[nx][ny] > nc) {
          cost[nx][ny] = nc;
          if (vis[x][y])
            deq.push_back({nx, ny});
          else
            deq.push_front({nx, ny});
        }
      }
    }
  }
  cout << ans << "\\n";
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U> using Pa = pair<T, U>;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  int M = N * N;
  vec<int> P(M);
  vec<Pa<int, int>> A(M);
  for (int i = 0; i < M; i++) {
    cin >> P[i];
    P[i]--;
    A[i] = {P[i] / N, P[i] % N};
  }

  int inf = 1e9;
  vec<int> mi(N * N);
  auto id = [&](int x, int y) { return x * N + y; };

  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) {
      mi[id(i, j)] = min({i, j, N - i - 1, N - j - 1});
    }

  auto in = [&](int x, int y) { return 0 <= x && x < N && 0 <= y && y < N; };

  auto dist = [&](int a, int b) {
    return abs(A[a].first - A[b].first) + abs(A[a].second - A[b].second);
  };

  ll ans = 0;
  for (int i = 0; i < M; i++) {
    vec<int> d(i + 1, inf), used(i + 1, 0);
    d[i] = 0;
    while (true) {
      int v = -1;
      int now = inf;
      for (int j = 0; j <= i; j++) {
        if (!used[j]) {
          if (v == -1 || now > d[j]) {
            v = j;
            now = d[j];
          }
        }
      }
      if (v == -1)
        break;
      used[v] = 1;
      for (int j = 0; j <= i; j++) {
        if (v != j)
          d[j] = min(d[j], d[v] + dist(v, j) - 1);
      }
    }
    int res = inf;
    for (int j = 0; j <= i; j++)
      res = min(res, d[j] + mi[P[j]]);
    ans += res;
  }
  cout << ans << "\\n";
}
Problem p02670 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair<int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock() / CLOCKS_PER_SEC

const int N = 507;
int dist[N][N];

vector<ii> v = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
bool bad[N][N];

signed main() {
#ifdef HOME
  freopen("input.txt", "r", stdin);
#else
#define endl '\\n'
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#endif
  int n;
  cin >> n;

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      dist[i][j] = min(min(i, j), min(n - i - 1, n - j - 1));
      bad[i][j] = 1;
    }
  }

  int ans = 0;
  for (int t = 0; t < n * n; ++t) {
    int who;
    cin >> who;
    --who;

    int i = who / n;
    int j = who % n;

    bad[i][j] = 0;
    ans += dist[i][j];
    queue<ii> q;
    q.push(mp(i, j));
    while (q.size()) {
      auto p = q.front();
      q.pop();
      int i = p.f, j = p.s;
      for (auto e : v) {
        int x = i + e.f, y = j + e.s;
        if (0 <= x && x < n && 0 <= y && y < n &&
            dist[i][j] + bad[i][j] < dist[x][y]) {
          dist[x][y] = dist[i][j] + bad[i][j];
          q.push(mp(x, y));
        }
      }
    }
  }
  cout << ans << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair<int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock() / CLOCKS_PER_SEC

const int N = 507;
int dist[N][N];

vector<ii> v = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
bool bad[N][N];

signed main() {
#ifdef HOME
  freopen("input.txt", "r", stdin);
#else
#define endl '\\n'
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#endif
  int n;
  cin >> n;

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      dist[i][j] = min(min(i, j), min(n - i - 1, n - j - 1));
      bad[i][j] = 1;
    }
  }

  int ans = 0;
  for (int t = 0; t < n * n; ++t) {
    int who;
    cin >> who;
    --who;

    int i = who / n;
    int j = who % n;

    bad[i][j] = 0;
    ans += dist[i][j];
    queue<ii> q;
    q.push(mp(i, j));
    while (q.size()) {
      auto p = q.front();
      q.pop();
      int i = p.f, j = p.s;
      for (auto e : v) {
        int x = i + e.f, y = j + e.s;
        if (dist[i][j] + bad[i][j] < dist[x][y]) {
          dist[x][y] = dist[i][j] + bad[i][j];
          q.push(mp(x, y));
        }
      }
    }
  }
  cout << ans << endl;
}
Problem p02670 (C++) — buggy submission, verdict: Runtime Error

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

#define GO                                                                     \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long int

const int maxr = 505;

int n, health[maxr][maxr];
bool done[maxr][maxr];
vector<int> dx = {0, 0, -1, 1};
vector<int> dy = {1, -1, 0, 0};

void update(int x, int y) {

  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (0 > nx || nx >= n || 0 > ny || ny >= n)
      continue;
    int co = !done[x][y];
    if (health[nx][ny] > health[x][y] + co) {
      health[nx][ny] = health[x][y] + co;
      update(nx, ny);
    }
  }
}

int32_t main() {
  GO;
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      health[i][j] = min(min(i, n - i - 1), min(j, n - j - 1));
      done[i][j] = false;
    }
  }
  int ans = 0;
  for (int i = 0; i < n * n; i++) {
    int p;
    cin >> p;
    int x = (p - 1) / n;
    int y = (p - 1) % n;
    ans += health[x][y];
    done[x][y] = true;
    update(x, y);
  }

  cout << ans << endl;
  return 0;
}

//!

Error: 0

Fixed submission:

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

#define GO                                                                     \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long int

const int maxr = 505;

int n, health[maxr][maxr];
bool done[maxr][maxr];
vector<int> dx = {0, 0, -1, 1};
vector<int> dy = {1, -1, 0, 0};

int update(int x, int y) {

  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];

    if (0 > nx || nx >= n || 0 > ny || ny >= n)
      continue;

    int co = !done[x][y];
    if (health[nx][ny] > health[x][y] + co) {
      health[nx][ny] = health[x][y] + co;
      update(nx, ny);
    }
  }
}

int32_t main() {
  GO;
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      health[i][j] = min(min(i, n - i - 1), min(j, n - j - 1));
      done[i][j] = false;
    }
  }
  int ans = 0;
  for (int i = 0; i < n * n; i++) {
    int p;
    cin >> p;
    int x = p / n;
    int y = p % n;
    y--;
    if (y == -1) {
      x--;
      y = n - 1;
    }
    ans += health[x][y];
    done[x][y] = true;
    update(x, y);
  }

  cout << ans << endl;
  return 0;
}

//!