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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using P = pair<ll, ll>;
#define debug(var) cerr << "[" << #var << "] " << var << endl
#define chmin(x, y) x = min(x, y)
const ll mod = 1000000007;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

int main() {
  ll n;
  cin >> n;

  vector<ll> p(n * n);

  rep(i, n * n) {
    cin >> p[i];
    p[i]--;
  }
  ll ans = 0;

  vector<vector<bool>> empty(n, vector<bool>(n));
  vector<vector<ll>> cost(n, vector<ll>(n));

  rep(i, n) rep(j, n) {
    cost[i][j] = min(i, j);
    chmin(cost[i][j], n - 1 - i);
    chmin(cost[i][j], n - 1 - j);
  }

  rep(i, n * n) {
    ll x = p[i] / n;
    ll y = p[i] % n;
    empty[x][y] = true;
    ans += cost[x][y];
    queue<P> q;
    q.emplace(x, y);
    while (!q.empty()) {
      P now = q.front();
      q.pop();
      ll nx = now.first;
      ll ny = now.second;
      ll v = cost[nx][ny] + 1;
      if (empty[nx][ny])
        v--;
      rep(j, 4) {
        ll ix = nx + dx[j];
        ll iy = ny + dy[j];
        if (ix < 0 || ix >= n || iy < 0 || iy >= n)
          continue;
        if (cost[ix][iy] > v) {
          cost[ix][iy] = v;
          q.emplace(ix, iy);
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using P = pair<int, int>;
#define debug(var) cerr << "[" << #var << "] " << var << endl
#define chmin(x, y) x = min(x, y)
const ll mod = 1000000007;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

template <typename T> struct BIT {
  int n;
  vector<T> d;
  BIT(int n = 0) : n(n), d(n + 1) {}
  void add(int i, T x = 1) {
    for (i++; i <= n; i += i & -i) {
      d[i] += x;
    }
  }
  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i & -i) {
      x += d[i];
    }
    return x;
  }
  T sum(int l, int r) { return sum(r - 1) - sum(l - 1); }
};

int main() {
  ll n;
  cin >> n;
  if (n > 400)
    return -1;
  vector<ll> p(n * n);

  rep(i, n * n) {
    cin >> p[i];
    p[i]--;
  }
  ll ans = 0;

  vector<BIT<ll>> bitx(n, BIT<ll>(n));
  vector<BIT<ll>> bity(n, BIT<ll>(n));
  rep(i, n) {
    rep(j, n) {
      bitx[i].add(j, 1LL);
      bity[i].add(j, 1LL);
    }
  }

  rep(i, n * n) {
    ll x = p[i] / n;
    ll y = p[i] % n;

    ll v1 = bity[x].sum(y - 1);
    if (y == 0)
      v1 = 0;
    ll v2 = bity[x].sum(n - 1) - bity[x].sum(y);
    if (y == n - 1)
      v2 = 0;
    ll v3 = bitx[y].sum(x - 1);
    if (x == 0)
      v3 = 0;
    ll v4 = bitx[y].sum(n - 1) - bitx[y].sum(x);
    if (x == n - 1)
      v4 = 0;
    /*
    debug(p[i]);
    debug(v1);
    debug(v2);
    debug(v3);
    debug(v4);
    */

    ll res = min(min(v1, v2), min(v3, v4));
    res = max(res, 0LL);
    ans += res;
    bitx[y].add(x, -1LL);
    bity[x].add(y, -1LL);
  }

  cout << ans << endl;

  return 0;
}