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

#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, a, b) for (int i = a; i <= b; ++i)
#define filei freopen("input.txt", "r", stdin);
#define fileo freopen("output.txt", "w", stdout);
#define gooi cout << "Case #" << i + 1 << " :";
#define s(n) scanf("%d", &n)
#define rev(i, n) for (int i = n; i >= 0; --i)
#define REV(i, a, b) for (int i = a; i >= b; --i)
#define miN(a, b) (((a) < (b)) ? (a) : (b))
#define sc(n) scanf("%c", &n)
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define INF 1000000000
#define pii pair<long long int, long long int>
#define pb(a) push_back(a)
#define F first
#define S second
typedef long long ll;
using namespace std;
void pre() {}
int arr[500][500];
bool isp[500][500];
ll q;
int n;
bool isvalid(int i, int j) {
  if (i >= n || i < 0 || j < 0 || j >= n) {
    return false;
    ;
  }
  return true;
}
void update(int i, int j) {
  if (!isp[i][j]) {
    if (isvalid(i + 1, j))
      if (arr[i + 1][j] == arr[i][j] + 1) {
        arr[i + 1][j]--;
        update(i + 1, j);
      }
    if (isvalid(i - 1, j))
      if (arr[i - 1][j] == arr[i][j] + 1) {
        arr[i - 1][j]--;
        update(i - 1, j);
      }
    if (isvalid(i, j + 1))
      if (arr[i][j + 1] == arr[i][j] + 1) {
        arr[i][j + 1]--;
        update(i, j + 1);
      }
    if (isvalid(i, j - 1))
      if (arr[i][j - 1] == arr[i][j] + 1) {
        arr[i][j - 1]--;
        update(i, j - 1);
      }
  } else {
    if (isvalid(i + 1, j))
      if (arr[i + 1][j] == arr[i][j] + 2) {
        arr[i + 1][j]--;
        update(i + 1, j);
      }
    if (isvalid(i - 1, j))
      if (arr[i - 1][j] == arr[i][j] + 2) {
        arr[i - 1][j]--;
        update(i - 1, j);
      }
    if (isvalid(i, j + 1))
      if (arr[i][j + 1] == arr[i][j] + 2) {
        arr[i][j + 1]--;
        update(i, j + 1);
      }
    if (isvalid(i, j - 1))
      if (arr[i][j - 1] == arr[i][j] + 2) {
        arr[i][j - 1]--;
        update(i, j - 1);
      }
  }
}
void solve() {
  cin >> n;
  ll m = n * n;
  for (int i = 0; i < n; i++) {
    for (int j = i; j < n - i; j++) {
      arr[i][j] = i;
      arr[n - i - 1][j] = i;
      arr[j][i] = i;
      arr[j][n - i - 1] = i;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      isp[i][j] = 1;
    }
  }
  ll ans = 0;
  ll a, b;
  for (int i = 0; i < m; i++) {
    cin >> q;
    q--;
    a = q / n;
    b = q % n;
    ans += arr[a][b];
    isp[a][b] = 0;
    update(a, b);
  }
  cout << ans << "\\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  pre();
  solve();
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, a, b) for (int i = a; i <= b; ++i)
#define filei freopen("input.txt", "r", stdin);
#define fileo freopen("output.txt", "w", stdout);
#define gooi cout << "Case #" << i + 1 << " :";
#define s(n) scanf("%d", &n)
#define rev(i, n) for (int i = n; i >= 0; --i)
#define REV(i, a, b) for (int i = a; i >= b; --i)
#define miN(a, b) (((a) < (b)) ? (a) : (b))
#define sc(n) scanf("%c", &n)
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define INF 1000000000
#define pii pair<long long int, long long int>
#define pb(a) push_back(a)
#define F first
#define S second
typedef long long ll;
using namespace std;
void pre() {}
int arr[500][500];
bool isp[500][500];
ll q[1000];
int n;
bool isvalid(int i, int j) {
  if (i >= n || i < 0 || j < 0 || j >= n) {
    return false;
    ;
  }
  return true;
}
void update(int i, int j) {
  if (!isp[i][j]) {
    if (isvalid(i + 1, j))
      if (arr[i + 1][j] == arr[i][j] + 1) {
        arr[i + 1][j]--;
        update(i + 1, j);
      }
    if (isvalid(i - 1, j))
      if (arr[i - 1][j] == arr[i][j] + 1) {
        arr[i - 1][j]--;
        update(i - 1, j);
      }
    if (isvalid(i, j + 1))
      if (arr[i][j + 1] == arr[i][j] + 1) {
        arr[i][j + 1]--;
        update(i, j + 1);
      }
    if (isvalid(i, j - 1))
      if (arr[i][j - 1] == arr[i][j] + 1) {
        arr[i][j - 1]--;
        update(i, j - 1);
      }
  } else {
    if (isvalid(i + 1, j))
      if (arr[i + 1][j] == arr[i][j] + 2) {
        arr[i + 1][j]--;
        update(i + 1, j);
      }
    if (isvalid(i - 1, j))
      if (arr[i - 1][j] == arr[i][j] + 2) {
        arr[i - 1][j]--;
        update(i - 1, j);
      }
    if (isvalid(i, j + 1))
      if (arr[i][j + 1] == arr[i][j] + 2) {
        arr[i][j + 1]--;
        update(i, j + 1);
      }
    if (isvalid(i, j - 1))
      if (arr[i][j - 1] == arr[i][j] + 2) {
        arr[i][j - 1]--;
        update(i, j - 1);
      }
  }
}
void solve() {
  cin >> n;
  ll m = n * n;
  for (int i = 0; i < n; i++) {
    for (int j = i; j < n - i; j++) {
      arr[i][j] = i;
      arr[n - i - 1][j] = i;
      arr[j][i] = i;
      arr[j][n - i - 1] = i;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      isp[i][j] = 1;
    }
  }
  ll ans = 0;
  ll a, b;
  for (int i = 0; i < m; i++) {
    cin >> q[i];
    q[i]--;
    a = q[i] / n;
    b = q[i] - n * (q[i] / n);
    ans += arr[a][b];
    isp[a][b] = 0;
    // cout<<a<<" "<<b<<"\\n";
    update(a, b);
  }
  cout << ans << "\\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  pre();
  long long int num = 1;
  // cin>>num;
  for (long long int i = 0; i < num; i++) {
    solve();
  }
}