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

#include <bits/stdc++.h>
#define clr(x) memset(x, 0, sizeof(x))
#define For(i, a, b) for (int i = (a); i <= (b); i++)
#define Fod(i, b, a) for (int i = (b); i >= (a); i--)
#define fi first
#define se second
#define kill _z_kill
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define outval(x) cerr << #x " = " << x << endl
#define outv(x) cerr << #x " = " << x << "  "
#define outtag(x) cerr << "--------------" #x "---------------" << endl
#define outarr(a, L, R)                                                        \\
  cerr << #a "[" << L << ".." << R << "] = ";                                  \\
  For(_x, L, R) cerr << a[_x] << " ";                                          \\
  cerr << endl;
#define User_Time ((double)clock() / CLOCKS_PER_SEC)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned uint;
typedef long double LD;
typedef vector<int> vi;
typedef pair<int, int> pii;
LL read() {
  LL x = 0, f = 0;
  char ch = getchar();
  while (!isdigit(ch))
    f = ch == '-', ch = getchar();
  while (isdigit(ch))
    x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  return f ? -x : x;
}
// const int mod=998244353;
// int Pow(int x,int y){
//	int ans=1;
//	for (;y;y>>=1,x=(LL)x*x%mod)
//		if (y&1)
//			ans=(LL)ans*x%mod;
//	return ans;
// }
// void Add(int &x,int y){
//	if ((x+=y)>=mod)
//		x-=mod;
// }
// void Del(int &x,int y){
//	if ((x-=y)<0)
//		x+=mod;
// }
// int Add(int x){
//	return x>=mod?x-mod:x;
// }
// int Del(int x){
//	return x<0?x+mod:x;
// }
const int N = 505;
int n;
int p[N * N];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int dis[N][N];
int check(int x, int y) { return 1 <= x && x <= n && 1 <= y && y <= n; }
int alive[N][N];
int vis[N][N], tag = 0;
void upd(int x, int y) {
  if (vis[x][y] == tag)
    return;
  vis[x][y] = tag;
  For(k, 0, 3) {
    int a = x + dx[k], b = y + dy[k];
    if (check(a, b)) {
      if (dis[x][y] + alive[a][b] < dis[a][b]) {
        dis[a][b] = dis[x][y] + alive[a][b];
        upd(a, b);
      }
    }
  }
}
int main() {
  n = read();
  For(i, 1, n * n) p[i] = read();
  For(i, 1, n) For(j, 1, n) {
    dis[i][j] = min(min(i, j), min(n - i + 1, n - j + 1));
    alive[i][j] = 1;
  }
  int ans = 0;
  For(i, 1, n * n) {
    int x = (p[i] - 1) / n + 1;
    int y = (p[i] - 1) % n + 1;
    dis[x][y]--;
    alive[x][y] = 0;
    ans += dis[x][y];
    tag++;
    upd(x, y);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define clr(x) memset(x, 0, sizeof(x))
#define For(i, a, b) for (int i = (a); i <= (b); i++)
#define Fod(i, b, a) for (int i = (b); i >= (a); i--)
#define fi first
#define se second
#define kill _z_kill
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define outval(x) cerr << #x " = " << x << endl
#define outv(x) cerr << #x " = " << x << "  "
#define outtag(x) cerr << "--------------" #x "---------------" << endl
#define outarr(a, L, R)                                                        \\
  cerr << #a "[" << L << ".." << R << "] = ";                                  \\
  For(_x, L, R) cerr << a[_x] << " ";                                          \\
  cerr << endl;
#define User_Time ((double)clock() / CLOCKS_PER_SEC)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned uint;
typedef long double LD;
typedef vector<int> vi;
typedef pair<int, int> pii;
LL read() {
  LL x = 0, f = 0;
  char ch = getchar();
  while (!isdigit(ch))
    f = ch == '-', ch = getchar();
  while (isdigit(ch))
    x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  return f ? -x : x;
}
// const int mod=998244353;
// int Pow(int x,int y){
//	int ans=1;
//	for (;y;y>>=1,x=(LL)x*x%mod)
//		if (y&1)
//			ans=(LL)ans*x%mod;
//	return ans;
// }
// void Add(int &x,int y){
//	if ((x+=y)>=mod)
//		x-=mod;
// }
// void Del(int &x,int y){
//	if ((x-=y)<0)
//		x+=mod;
// }
// int Add(int x){
//	return x>=mod?x-mod:x;
// }
// int Del(int x){
//	return x<0?x+mod:x;
// }
const int N = 505;
int n;
int p[N];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int dis[N][N];
int check(int x, int y) { return 1 <= x && x <= n && 1 <= y && y <= n; }
int alive[N][N];
int vis[N][N], tag = 0;
void upd(int x, int y) {
  if (vis[x][y] == tag)
    return;
  vis[x][y] = tag;
  For(k, 0, 3) {
    int a = x + dx[k], b = y + dy[k];
    if (check(a, b)) {
      if (dis[x][y] + alive[a][b] < dis[a][b]) {
        dis[a][b] = dis[x][y] + alive[a][b];
        upd(a, b);
      }
    }
  }
}
int main() {
  n = read();
  For(i, 1, n * n) p[i] = read();
  For(i, 1, n) For(j, 1, n) {
    dis[i][j] = min(min(i, j), min(n - i + 1, n - j + 1));
    alive[i][j] = 1;
  }
  int ans = 0;
  For(i, 1, n * n) {
    int x = (p[i] - 1) / n + 1;
    int y = (p[i] - 1) % n + 1;
    dis[x][y]--;
    alive[x][y] = 0;
    ans += dis[x][y];
    tag++;
    upd(x, y);
  }
  cout << ans << endl;
  return 0;
}