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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
typedef long long LL;
int N;
int a[601][601];
vector<int> v;
// map<int,int>m;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int m[300000];

void bfs(int x, int y, int now, int p) {
  REP(i, 4) {
    int xx = x + dx[i];
    int yy = y + dy[i];
    if (xx < 0 || xx >= N || yy < 0 || yy >= N)
      continue;
    int nxt = now;
    if (m[y * N + x] > m[p])
      nxt++;
    if (a[xx][yy] > nxt) {
      a[xx][yy] = nxt;
      bfs(xx, yy, nxt, p);
      // a[xx][yy]=min(a[xx][yy],a[x][y]);
    }
  }
}

/*void output(){
        REP(i,N){
                REP(j,N){
                        //a[i][j]=min(i,min(j,min(N-1-i,N-1-j)));
                        cout<<a[i][j]<<" ";
                }
                cout<<endl;
        }
        cout<<endl;
}*/

int main() {
  cin >> N;
  REP(i, N * N) {
    int aa;
    cin >> aa;
    aa--;
    v.push_back(aa);
    m[aa] = i;
    // mm[x][y]=i;
  }
  REP(i, N) {
    REP(j, N) { a[i][j] = min(i, min(j, min(N - 1 - i, N - 1 - j))); }
  }

  LL ans = 0;
  REP(i, v.size()) {
    int now = v[i];
    int x = now % N;
    int y = now / N;
    bfs(x, y, a[x][y], v[i]);
    ans += a[x][y];
    // cout<<now<<" "<<a[x][y]<<endl;
    // output();
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
typedef long long LL;
int N;
int a[300001];
vector<int> v;
map<int, int> m;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

int par1[300000];
int rank1[300000];
vector<int> G[3601];
// n要素で初期化
void init(int n) {
  for (int i = 0; i < n; i++) {
    par1[i] = i;
    rank1[i] = 0;
  }
}
// 木の根を求める
int find(int x) {
  if (par1[x] == x) {
    return x;
  } else {
    return par1[x] = find(par1[x]);
  }
}
// xとyの属する集合を併合
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (rank1[x] < rank1[y]) {
    par1[x] = y;
    REP(i, G[x].size()) { G[y].push_back(find(G[x][i])); }
    sort(G[y].begin(), G[y].end());
    G[y].erase(unique(G[y].begin(), G[y].end()), G[y].end());
  } else {
    par1[y] = x;
    REP(i, G[y].size()) { G[x].push_back(find(G[y][i])); }
    sort(G[x].begin(), G[x].end());
    G[x].erase(unique(G[x].begin(), G[x].end()), G[x].end());
    rank1[x]++;
  }
}
// xとyが同じ集合に属するか否か
bool same(int x, int y) { return find(x) == find(y); }

void bfs(int pre, int now, int p) {
  int nnn = pre;
  REP(i, G[nnn].size()) {
    int aa = find(G[nnn][i]);
    // int aa=G[nnn][i];
    // cout<<pre<<" "<<aa<<endl;
    int nxt = now;
    if (m[pre] > m[p])
      nxt++;
    // cout<<a[aa]<<" aaa"<<nxt<<m[p]<<" "<<m[pre]<<endl;
    if (a[aa] > nxt) {
      a[aa] = nxt;
      bfs(aa, nxt, p);
      // a[xx][yy]=min(a[xx][yy],a[x][y]);
    }
  }
}

/*void output(){
        REP(i,N*N){
                int x=i%N;
                int y=i/N;
                cout<<a[i]<<" ";
                if(x==N-1){
                        cout<<endl;
                }
        }
        cout<<endl;
}*/

int main() {
  cin >> N;
  init(N + 10);
  REP(i, N * N) {
    int aa;
    cin >> aa;
    aa--;
    v.push_back(aa);
    m[aa] = i;
  }
  REP(i, N) {
    REP(j, N) {
      a[j * N + i] = min(i, min(j, min(N - 1 - i, N - 1 - j)));
      int now = j * N + i;
      if (i != 0) {
        G[now].push_back(now - 1);
      }
      if (i != N - 1) {
        G[now].push_back(now + 1);
      }
      if (j != 0) {
        G[now].push_back(now - N);
      }
      if (j != N - 1) {
        G[now].push_back(now + N);
      }
    }
  }

  LL ans = 0;
  REP(i, v.size()) {
    int now = v[i];
    bfs(now, a[now], v[i]);
    ans += a[now];
    REP(i, G[now].size()) {
      if (m[now] > m[G[now][i]]) {
        unite(G[now][i], now);
      }
    }
    // cout<<now<<" "<<a[now]<<endl;
    // output();
  }

  cout << ans << endl;
}