alexjercan/bugnet huggingface dataset:
Problem p02669 (C++) — buggy submission, verdict: Runtime Error

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

#define ll long long
#define all(aaa) aaa.begin(), aaa.end()

const ll INF = 1e18;
const int a[3] = {2, 3, 5};
ll w[3];
map<ll, ll> mp;
ll d;

ll f(ll x) {
  if (x == 0)
    return 0ll;
  if (mp.count(x))
    return mp[x];
  ll ans = min(x * (double)d, (double)INF);
  for (int i = 0; i < 3; i++) {
    ll r = x % a[i], anime = x / a[i];
    if (anime != x)
      ans = min(ans, f(anime) + r * d + w[i]);
    anime = (x + a[i] - 1) / a[i];
    if (anime != x)
      ans = min(ans, f(anime) + (a[i] - r) % a[i] * d + w[i]);
  }
  mp[x] = ans;
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t;
  cin >> t;

  while (t--) {
    ll n;
    cin >> n >> w[0] >> w[1] >> w[2] >> d;
    cout << f(n) << "\\n";
    mp.clear();
  }

  return 0;
}

Error: -11

Fixed submission:

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

#define ll long long
#define all(aaa) aaa.begin(), aaa.end()

const int N = 505;
int a[N][N], mn[N][N], t[N * N];

int que(int x) {
  int s = 0;
  for (; x >= 0; x = (x & (x + 1)) - 1)
    s += t[x];
  return s;
}

void upd(int x, int y) {
  for (; x < N * N; x |= x + 1)
    t[x] += y;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  cin >> n;

  for (int i = 0, k = n * n - 1; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int x;
      cin >> x;
      x--;
      a[x / n][x % n] = k--;
      mn[i][j] = N;
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      mn[i][j] = min(mn[i][j], que(a[i][j]));
      upd(a[i][j], 1);
    }
    for (int j = 0; j < n; j++) {
      upd(a[i][j], -1);
    }
  }

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

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      mn[j][i] = min(mn[j][i], que(a[j][i]));
      upd(a[j][i], 1);
    }
    for (int j = 0; j < n; j++) {
      upd(a[j][i], -1);
    }
  }

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

  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      ans += mn[i][j];
    }
  }

  cout << ans << "\\n";

  return 0;
}
Problem p02669 (C++) — buggy submission, verdict: Runtime Error

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

Int A, B, C, D;
map<Int, Int> m;

Int solve(Int N) {
  if (N == 0)
    return 0;
  if (N == 1)
    return D;
  if (m.count(N))
    return m[N];

  Int l2 = (N / 2) * 2;
  Int r2 = ((N + 1) / 2) * 2;

  Int l3 = (N / 3) * 3;
  Int r3 = ((N + 2) / 3) * 3;

  Int l5 = (N / 5) * 5;
  Int r5 = ((N + 4) / 5) * 5;

  Int res = 1e18;
  if (N < res / D)
    res = N * D;

  res = min(res, llabs(l2 - N) * D + A + solve(l2 / 2));
  res = min(res, llabs(r2 - N) * D + A + solve(r2 / 2));

  res = min(res, llabs(l3 - N) * D + B + solve(l3 / 3));
  res = min(res, llabs(r3 - N) * D + B + solve(r3 / 3));

  res = min(res, llabs(l5 - N) * D + C + solve(l5 / 5));
  res = min(res, llabs(r5 - N) * D + C + solve(r5 / 5));

  m[N] = res;

  return res;
}

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

  for (int t = 0; t < T; t++) {
    m.clear();
    Int N;
    cin >> N;
    cin >> A >> B >> C >> D;

    cout << solve(N) << endl;
  }
}

Error: -11

Fixed submission:

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

Int A, B, C, D;
map<Int, Int> m;

Int solve(Int N) {
  if (N == 0)
    return 0;
  if (N == 1)
    return D;
  if (m.count(N))
    return m[N];

  Int l2 = (N / 2) * 2;
  Int r2 = ((N + 1) / 2) * 2;

  Int l3 = (N / 3) * 3;
  Int r3 = ((N + 2) / 3) * 3;

  Int l5 = (N / 5) * 5;
  Int r5 = ((N + 4) / 5) * 5;

  Int res = 1e18;
  if (N < res / D)
    res = N * D;

  res = min(res, llabs(l2 - N) * D + A + solve(l2 / 2));
  res = min(res, llabs(r2 - N) * D + A + solve(r2 / 2));

  res = min(res, llabs(l3 - N) * D + B + solve(l3 / 2));
  res = min(res, llabs(r3 - N) * D + B + solve(r3 / 2));

  res = min(res, llabs(l5 - N) * D + C + solve(l5 / 2));
  res = min(res, llabs(r5 - N) * D + C + solve(r5 / 2));

  m[N] = res;

  return res;
}

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

  for (int t = 0; t < T; t++) {
    m.clear();
    Int N;
    cin >> N;
    cin >> A >> B >> C >> D;

    cout << solve(N) << endl;
  }
}
Problem p02669 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll LINF = 6e18;

ll c2, c3, c5, c1;
map<ll, ll> mp;

ll dfs(ll n) {
  if (!mp.count(n)) {
    if (n == 0) {
      mp[n] = 0;
    } else if (n == 1) {
      mp[n] = c1;
    } else {
      ll res = LINF;
      if (n <= res / c1)
        res = min(res, c1 * n);
      res = min(res, dfs(n / 2) + c2 + c1 * (n % 2));
      res = min(res, dfs(n / 3) + c3 + c1 * (n % 3));
      res = min(res, dfs(n / 5) + c5 + c1 * (n % 5));
      if (n > 2)
        res = min(res, dfs(1 + n / 2) + c2 + c1 * (2 - (n % 2)));
      res = min(res, dfs(1 + n / 3) + c3 + c1 * (3 - (n % 3)));
      res = min(res, dfs(1 + n / 5) + c5 + c1 * (5 - (n % 5)));
      mp[n] = res;
    }
  }
  return mp[n];
}

int main() {
  int T;
  cin >> T;
  while (T--) {
    mp = {};
    ll n;
    cin >> n;
    cin >> c2 >> c3 >> c5 >> c1;
    ll ans = dfs(n);
    cout << ans << endl;
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll LINF = 6e18;

ll c2, c3, c5, c1;
map<ll, ll> mp;

ll dfs(ll n) {
  if (!mp.count(n)) {
    if (n == 0) {
      mp[n] = 0;
    } else if (n == 1) {
      mp[n] = c1;
      //} else if (n == 2) {
      // mp[n] = c1 + min(c1, c2);
    } else {
      ll res = LINF;
      if (n <= res / c1)
        res = min(res, c1 * n);
      res = min(res, dfs(n / 2) + c2 + c1 * (n % 2));
      res = min(res, dfs(n / 3) + c3 + c1 * (n % 3));
      res = min(res, dfs(n / 5) + c5 + c1 * (n % 5));
      res = min(res, dfs(1 + n / 2) + c2 + c1 * (2 - (n % 2)));
      res = min(res, dfs(1 + n / 3) + c3 + c1 * (3 - (n % 3)));
      res = min(res, dfs(1 + n / 5) + c5 + c1 * (5 - (n % 5)));
      mp[n] = res;
    }
  }
  return mp[n];
}

int main() {
  int T;
  cin >> T;
  while (T--) {
    mp = {};
    ll n;
    cin >> n;
    cin >> c2 >> c3 >> c5 >> c1;
    ll ans = dfs(n);
    cout << ans << endl;
  }
}
Problem p02669 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define IOS std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define IN freopen("in.txt", "r", stdin);
#define endl '\\n'
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
const ll inf = 0x3f3f3f3f3f3f3f3f;

ll ans = inf;
ll n, a, b, c, d;
map<ll, ll> mp;

ll solve(ll x) {
  if (!x)
    return 0;
  if (x == 1)
    return d;
  if (mp.count(x))
    return mp[x];
  ll l2 = (x / 2) * 2;
  ll r2 = ((x + 1) / 2) * 2;
  ll l3 = (x / 3) * 3;
  ll r3 = ((x + 2) / 3) * 3;
  ll l5 = (x / 5) * 5;
  ll r5 = ((x + 4) / 5) * 5;
  ll res = 1e18;
  if (x < res / d)
    res = x * d;
  res = min(res, llabs(l2 - x) * d + a + solve(l2 / 2));
  res = min(res, llabs(r2 - x) * d + a + solve(r2 / 2));
  res = min(res, llabs(l3 - x) * d + b + solve(l3 / 3));
  res = min(res, llabs(r3 - x) * d + b + solve(r3 / 3));
  res = min(res, llabs(l5 - x) * d + c + solve(l5 / 5));
  res = min(res, llabs(r5 - x) * d + c + solve(r5 / 5));
  mp[x] = res;
  return res;
}
int main() {
  IOS;
  int _;
  cin >> _;
  while (_--) {
    mp.clear();
    cin >> n >> a >> b >> c >> d;
    cout << solve(n) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define IOS std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define IN freopen("in.txt", "r", stdin);
#define endl '\\n'
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
const ll inf = 0x3f3f3f3f3f3f3f3f;

ll ans = inf;
ll n, a, b, c, d;

void dfs(ll x, ll now) {
  if (x >= n) {
    ans = min(ans, now + (x - n) * d);
    return;
  }
  ans = min(ans, d * (n - x) + now);
  if (now + a < ans)
    dfs(2 * x, now + a);
  if (now + b < ans)
    dfs(3 * x, now + b);
  if (now + c < ans)
    dfs(5 * x, now + c);
}
int main() {
  IOS;
  int _;
  cin >> _;
  while (_--) {
    ans = inf;
    cin >> n >> a >> b >> c >> d;
    dfs(1, d);
    cout << ans << endl;
  }
  return 0;
}
Problem p02669 (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<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};
const ll INF = 1E+18;

ll a;
ll b;
ll c;
ll d;
ll n;
map<ll, ll> mp;

ll f(ll x) {
  if (x == 0)
    return 0;
  if (x == 1)
    return d;
  if (mp.count(x))
    return mp[x];
  ll l2 = x - x % 2;
  // ll r2 = ((x-1)/2 + 1)*2;
  ll r2 = l2 + 2LL;
  if (l2 == 2LL)
    r2 = 2LL;
  ll l3 = x - x % 3;
  ll r3 = ((x - 1) / 3 + 1) * 3;
  // ll r3 = l3 + 3LL;
  ll l5 = x - x % 5;
  ll r5 = ((x - 1) / 5 + 1) * 5;
  // ll r5 = l5 + 5LL;
  ll res = INF;
  if (d <= INF / x)
    res = d * x;
  // debug(x);
  res = min(res, abs(l2 - x) * d + a + f(l2 / 2));
  // debug(x);
  res = min(res, abs(r2 - x) * d + a + f(r2 / 2));
  // debug(x);
  res = min(res, abs(l3 - x) * d + b + f(l3 / 3));
  // debug(x);
  res = min(res, abs(r3 - x) * d + b + f(r3 / 3));
  // debug(x);
  res = min(res, abs(l5 - x) * d + c + f(l5 / 5));
  // debug(x);
  res = min(res, abs(r5 - x) * d + c + f(r5 / 5));
  // debug(x);
  mp[x] = res;
  return res;
}

int main() {
  int t;
  cin >> t;
  rep(i, t) {
    cin >> n >> a >> b >> c >> d;
    mp.clear();
    ll ans = f(n);
    cout << ans << endl;
  }
  return 0;
}


Error: -11

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};
const ll INF = 1E+18;

ll a;
ll b;
ll c;
ll d;
ll n;
map<ll, ll> mp;

ll f(ll x) {
  if (x == 0)
    return 0;
  if (x == 1)
    return d;
  if (mp.count(x))
    return mp[x];
  ll l2 = x - x % 2;
  // ll r2 = ((x-1)/2 + 1)*2;
  ll r2 = l2 + 2;
  ll l3 = x - x % 3;
  // ll r3 = ((x-1)/3+1)*3;
  ll r3 = l3 + 3;
  ll l5 = x - x % 5;
  // ll r5 = ((x-1)/5+1)*5;
  ll r5 = l5 + 5;
  ll res = INF;
  if (d <= INF / x)
    res = d * x;
  res = min(res, abs(l2 - x) * d + a + f(l2 / 2));
  res = min(res, abs(r2 - x) * d + a + f(r2 / 2));
  res = min(res, abs(l3 - x) * d + b + f(l3 / 3));
  res = min(res, abs(r3 - x) * d + b + f(r3 / 3));
  res = min(res, abs(l5 - x) * d + c + f(l5 / 5));
  res = min(res, abs(r5 - x) * d + c + f(r5 / 5));
  mp[x] = res;
  return res;
}

int main() {
  int t;
  cin >> t;
  rep(i, t) {
    cin >> n >> a >> b >> c >> d;
    mp.clear();
    ll ans = f(n);
    cout << ans << endl;
  }

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

#include <queue>
#include <stdio.h>

struct str {
  int x0;
  int y0;
  int dist;
};
bool operator<(str a, str b) { return a.dist > b.dist; }

std::priority_queue<str> Q;
int x[2500010], check[510][510], check2[510][510];
int direX[5] = {1, 0, -1, 0}, direY[5] = {0, 1, 0, -1};
int main() {
  int a;
  scanf("%d", &a);
  for (int i = 1; i <= a * a; i++)
    scanf("%d", &x[i]);
  for (int i = 1; i <= a; i++) {
    for (int j = 1; j <= a; j++) {
      int s1 = (i - 1) < (a - i) ? (i - 1) : (a - i);
      int s2 = (j - 1) < (a - j) ? (j - 1) : (a - j);
      check[i][j] = s1 < s2 ? s1 : s2;
      check2[i][j] = 1;
    }
  }

  long long int sum = 0;
  for (int i = 1; i <= a * a; i++) {
    int s = (x[i] - 1) / a + 1;
    int t = (x[i] - 1) % a + 1;
    check2[s][t] = 0;
    sum += check[s][t];

    for (int j = 0; j < 4; j++) {
      if (check[s][t] < check[s + direX[j]][t + direY[j]]) {
        check[s + direX[j]][t + direY[j]] = check[s][t];
        Q.push({s + direX[j], t + direY[j], check[s][t]});
      }
    }
    while (!Q.empty()) {
      int x0 = Q.top().x0;
      int y0 = Q.top().y0;
      int dist = Q.top().dist;
      Q.pop();

      for (int j = 0; j < 4; j++) {
        if (check[x0][y0] + check2[x0][y0] <
            check[x0 + direX[j]][y0 + direY[j]]) {
          check[x0 + direX[j]][y0 + direY[j]] = check[x0][y0] + check2[x0][y0];
          Q.push(
              {x0 + direX[j], y0 + direY[j], check[x0][y0] + check2[x0][y0]});
        }
      }
    }
  }
  printf("%lld", sum);
}

Error: TLE

Fixed submission:

#include <queue>
#include <stdio.h>

struct str {
  int x0;
  int y0;
  int dist;
};
bool operator<(str a, str b) { return a.dist > b.dist; }

std::priority_queue<str> Q;
int x[2500010], check[510][510], check2[510][510];
int direX[5] = {1, 0, -1, 0}, direY[5] = {0, 1, 0, -1};
int main() {

  int a;
  scanf("%d", &a);
  for (int i = 1; i <= a * a; i++)
    scanf("%d", &x[i]);
  for (int i = 1; i <= a; i++) {
    for (int j = 1; j <= a; j++) {
      int s1 = (i - 1) < (a - i) ? (i - 1) : (a - i);
      int s2 = (j - 1) < (a - j) ? (j - 1) : (a - j);
      check[i][j] = s1 < s2 ? s1 : s2;
      check2[i][j] = 1;
    }
  }

  long long int sum = 0;
  for (int i = 1; i <= a * a; i++) {
    int s = (x[i] - 1) / a + 1;
    int t = (x[i] - 1) % a + 1;
    check2[s][t] = 0;
    sum += check[s][t];

    for (int j = 0; j < 4; j++)
      Q.push({s + direX[j], t + direY[j], check[s][t]});
    while (!Q.empty()) {
      int x0 = Q.top().x0;
      int y0 = Q.top().y0;
      int dist = Q.top().dist;
      Q.pop();
      if (dist >= check[x0][y0])
        continue;
      check[x0][y0] = dist;

      for (int j = 0; j < 4; j++) {
        if (check[x0][y0] + check2[x0][y0] <
            check[x0 + direX[j]][y0 + direY[j]]) {
          Q.push(
              {x0 + direX[j], y0 + direY[j], check[x0][y0] + check2[x0][y0]});
        }
      }
    }
  }
  printf("%lld", sum);
}
Problem p02670 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < (int)(n); i++)
#define ALL(a) a.begin(), a.end()
#define MOD 1000000007
using namespace std;
using ll = long long;

int N;
vector<vector<int>> D;
vector<vector<bool>> occupied;

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

void dfs(int x, int y) {
  int d = D[x][y] + occupied[x][y];
  REP(i, 0, 4) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx < 0 || nx >= N || ny < 0 || ny >= N)
      continue;
    if (D[nx][ny] > d) {
      D[nx][ny] = d;
      dfs(nx, ny);
    }
  }
}

// void print(vector<vector<int>> &D) {
//     cout << "----------" << endl;
//     REP(i, 0, N) {
//         REP(j, 0, N) {
//             cout << D[i][j] << " ";
//         }
//         cout << endl;
//     }
//     cout << "----------" << endl;
// }

int main() {
  cin >> N;

  D.assign(N, vector<int>(N));
  REP(i, 0, N) REP(j, 0, N) {
    D[i][j] = min({
        i,
        j,
        N - 1 - i,
        N - 1 - j,
    });
  }

  int ans = 0;
  occupied.assign(N, vector<bool>(N, true));
  REP(i, 0, N * N) {
    int p;
    cin >> p;
    p--;
    ans += D[p / N][p % N];
    occupied[p / N][p % N] = false;
    dfs(p / N, p % N);
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < (int)(n); i++)
#define ALL(a) a.begin(), a.end()
#define MOD 1000000007
using namespace std;
using ll = long long;

int N;
vector<int> order;
vector<vector<int>> P, D;

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

int dijkstra(int n) {
  using pi = pair<int, int>;
  priority_queue<pi, vector<pi>, greater<pi>> Q;
  Q.push({0, order[n]});
  // cout << __func__ << " " << n << endl;
  int ret = 1 << 28;
  while (!Q.empty()) {
    auto q = Q.top();
    Q.pop();
    int x = q.second / N;
    int y = q.second % N;
    // cout << "--> " << x << " " << y << endl;
    if (x == 0 || x == N - 1 || y == 0 || y == N - 1)
      ret = min(ret, D[x][y]);
    if (D[x][y] < q.first)
      continue;
    REP(i, 0, 4) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx < 0 || nx >= N || ny < 0 || ny >= N)
        continue;
      if (D[nx][ny] > q.first + (P[nx][ny] > (n + 1))) {
        D[nx][ny] = q.first + (P[nx][ny] > (n + 1));
        Q.push({D[nx][ny], nx * N + ny});
      }
    }
  }
  return ret;
}

int main() {
  cin >> N;
  order.assign(N * N, 0);
  P.assign(N, vector<int>(N));
  REP(i, 0, N * N) {
    int p;
    cin >> p;
    p--;
    P[p / N][p % N] = i + 1;
    order[i] = p;
  }

  // REP(i, 0, N * N) {
  //     cout << i << " " << order[i] << endl;
  // }

  // REP(i, 0, N) {
  //     REP(j, 0, N) {
  //         cout << P[i][j] << " ";
  //     }
  //     cout << endl;
  // }

  int ans = 0;
  REP(i, 0, N * N) {
    D.assign(N, vector<int>(N, 1 << 28));
    D[order[i] / N][order[i] % N] = 0;
    int d = dijkstra(i);
    // cout << "i : " << i << " " << d << endl;
    ans += d;
  }
  cout << ans << endl;
  return 0;
}
Problem p02670 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int C[510][510];
bool U[510][510];
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  int M = N * N;
  vector<int> P(M);
  for (auto &x : P)
    cin >> x;
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      C[i][j] = min({i - 1, j - 1, N - i, N - j});
    }
  }
  auto X = [&](auto x) { return (x - 1) / N + 1; };
  auto Y = [&](auto x) { return (x - 1) % N + 1; };

  long long ans = 0;

  auto bfs = [&](int x, int y) {
    ans += C[x][y];
    U[x][y] = 1;
    queue<tuple<int, int, int>> Q;
    Q.emplace(C[x][y]--, x, y);
    while (!Q.empty()) {
      auto [z, x, y] = Q.front();
      Q.pop();
      for (int i = 0; i < 4; i++) {
        int xt = x + dx[i], yt = y + dy[i];
        if (z + 1 == C[xt][yt]) {
          Q.emplace(C[xt][yt]--, xt, yt);
        }
        if (U[xt][yt] && z == C[xt][yt]) {
          Q.emplace(C[xt][yt]--, xt, yt);
        }
      }
    }
  };

  for (int i = 0; i < M; i++) {
    bfs(X(P[i]), Y(P[i]));
  }

  cout << ans << "\\n";

  return 0;
}

Error: TLE

Fixed submission:

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

bool C[510][510];
bool U[510][510];
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  int M = N * N;
  vector<int> P(M);
  for (auto &x : P)
    cin >> x;
  auto X = [&](auto x) { return (x - 1) / N + 1; };
  auto Y = [&](auto x) { return (x - 1) % N + 1; };

  reverse(P.begin(), P.end());

  auto bfs = [&](int x, int y) {
    U[x][y] = 1;
    memset(C, 0, sizeof(C));
    deque<tuple<int, int, int>> Q;
    Q.emplace_back(0, x, y);
    while (!Q.empty()) {
      auto [z, x, y] = Q.front();
      Q.pop_front();
      if (x == 0 || y == 0 || x == N + 1 || y == N + 1)
        return z;
      for (int i = 0; i < 4; i++) {
        int xt = x + dx[i], yt = y + dy[i];
        if (C[xt][yt])
          continue;
        if (U[xt][yt]) {
          Q.emplace_back(z + 1, xt, yt);
        } else {
          Q.emplace_front(z, xt, yt);
        }
        C[xt][yt] = 1;
      }
    }
  };

  long long ans = 0;
  for (int i = 0; i < M; i++) {
    ans += bfs(X(P[i]), Y(P[i]));
  }

  cout << ans << "\\n";

  return 0;
}
Problem p02670 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ls (o << 1)
#define rs (o << 1 | 1)
#define pb push_back
const double PI = acos(-1.0);
const int M = 500 + 7;

int x[M * M], y[M * M];
int dx[10] = {-1, 1, 0, 0}, dy[10] = {0, 0, -1, 1};
int vs[M][M]; // i,j位置当前时刻 是否还有人
int d[M][M];  // i,j位置的人，最快离开剧场所需要的时间
struct node {
  int x, y;
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n * n; i++) {
    int z;
    cin >> z;
    x[i] = (z - 1) / n + 1;
    y[i] = (z - 1) % n + 1;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      d[i][j] = min(min(i - 1, n - i), min(j - 1, n - j)), vs[i][j] = 1;
  int ans = 0;
  for (int i = 1; i <= n * n; i++) {
    ans += d[x[i]][y[i]];
    vs[x[i]][y[i]] = 0;
    queue<node> q;
    q.push(node{x[i], y[i]});
    while (q.size()) {
      node tp = q.front();
      q.pop();
      int tx = tp.x, ty = tp.y;
      for (int j = 0; j < 4; j++) {
        int px = tx + dx[j], py = ty + dy[j];
        if (px < 1 || px > n || py < 1 || py > n ||
            d[tx][ty] + vs[tx][ty] >= d[px][py])
          continue; // px,py 先到 tx,ty 是否更优，如果不优没必要拓展下去
        d[px][py] = d[tx][ty] + vs[tx][ty];
        q.push(node{px, py});
      }
    }
    //		cout<<i<<"   "<<x[i]<<" "<<y[i]<<"-----"<<endl;
    //		for(int j=1;j<=n;j++)
    //		for(int k=1;k<=n;k++)
    //		{
    //			cout<<d[j][k]<<" ";
    //			if(k==n)cout<<endl;
    //		}
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ls (o << 1)
#define rs (o << 1 | 1)
#define pb push_back
const double PI = acos(-1.0);
const int M = 500 + 7;

int x[M], y[M];
int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
int vs[M][M]; // i,j位置当前时刻 是否还有人
int d[M][M];  // i,j位置的人，最快离开剧场所需要的时间
struct node {
  int x, y;
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n * n; i++) {
    int z;
    cin >> z;
    x[i] = (z - 1) / n + 1;
    y[i] = (z - 1) % n + 1;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      d[i][j] = min(min(i - 1, n - i), min(j - 1, n - j)), vs[i][j] = 1;
  int ans = 0;
  for (int i = 1; i <= n * n; i++) {
    ans += d[x[i]][y[i]];
    vs[x[i]][y[i]] = 0;
    queue<node> q;
    q.push(node{x[i], y[i]});
    while (q.size()) {
      node tp = q.front();
      q.pop();
      int tx = tp.x, ty = tp.y;
      for (int j = 0; j < 4; j++) {
        int px = tx + dx[j], py = ty + dy[j];
        if (px < 1 || px > n || py < 1 || py > n ||
            d[tx][ty] + vs[tx][ty] >= d[px][py])
          continue; // px,py 先到 tx,ty 是否更优，如果不优没必要拓展下去
        d[px][py] = d[tx][ty] + vs[tx][ty];
        q.push(node{px, py});
      }
    }
    //		cout<<i<<"   "<<x[i]<<" "<<y[i]<<"-----"<<endl;
    //		for(int j=1;j<=n;j++)
    //		for(int k=1;k<=n;k++)
    //		{
    //			cout<<d[j][k]<<" ";
    //			if(k==n)cout<<endl;
    //		}
  }
  cout << ans << endl;
  return 0;
}