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

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <queue>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1001001001;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
inline int read() {
  int x = 0, t = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-')
    t = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - 48, ch = getchar();
  return x * t;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int main() {
  int N, X, Y;
  cin >> N >> X >> Y;
  --X;
  --Y;
  vector<int> ans(N);
  rep(i, N) {
    vector<int> dist(N, INF);
    queue<int> q;
    q.push(i);
    dist[i] = 0;
    while (!q.empty()) {
      int a = q.front();
      q.pop();
      int d = dist[a];
      if (a - 1 >= 0 && dist[a - 1] == INF) {
        q.push(a - 1);
        dist[a - 1] = d + 1;
      }
      if (a + 1 < N && dist[a + 1] == INF) {
        q.push(a + 1);
        dist[a + 1] = d + 1;
      }
      if (a == X && dist[Y] == INF) {
        q.push(Y);
        dist[Y] = d + 1;
      }
      if (a == Y && dist[X] == INF) {
        q.push(X);
        dist[X] = d + 1;
      }
    }
    rep(i, N) ans[dist[i]]++;
  }
  rep(i, N) ans[i] /= 2;
  for (register int i = 1; i <= N - 1; i++)
    cout << ans[i] << endl;
  return 0;
}

Error: 0

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <random>
#define all(x) (x).begin(), (x).end()

using namespace std;
void dout() { cerr << endl; }
typedef long long ll;
template <typename Head, typename... Tail> void dout(Head H, Tail... T) {
  cerr << H << ' ';
  dout(T...);
}

#define debug(x) cout << #x " = " << (x) << endl

using ll = long long;
using pii = pair<ll, ll>;
using ld = long double;
using pll = pair<long long, long long>;

template <typename T> void do_uniq(vector<T> vec) {
  sort(all(vec));
  vec.resize(unique(all(vec)) - vec.begin());
}

// #define solvsh
// #define multi
#ifdef solvsh

void solve() {}

#else

const int MAXN = 1e3 + 9;

vector<int> g[MAXN];

int answr[MAXN];

int d[MAXN];
int usd[MAXN];
int n;

void f(int v) {
  for (int i = 1; i <= n; ++i) {
    d[i] = n + 2;
    usd[i] = false;
  }

  queue<int> q;
  q.push(v);
  d[v] = 0;
  usd[v] = true;

  while (!q.empty()) {
    auto h = q.front();
    q.pop();
    for (auto e : g[h]) {
      d[e] = min(d[e], d[h] + 1);

      if (!usd[e]) {
        q.push(e);
        usd[e] = true;
      }
    }
  }

  for (int i = 1; i <= n; ++i) {
    answr[d[i]]++;
  }
}

void solve() {
  cin >> n;

  int x, y;
  cin >> x >> y;
  g[x].push_back(y);
  g[y].push_back(x);

  for (int i = 1; i < n; ++i) {
    g[i].push_back(i + 1);
    g[i + 1].push_back(i);
  }

  for (int i = 1; i <= n; ++i) {
    f(i);
  }

  for (int i = 1; i < n; ++i) {
    cout << answr[i] / 2 << "\\n";
  }
}

#endif

void multisolve() {
  int t;
  cin >> t;

  int i = 1;
  while (t--) {
    //        cout << "Case #" << (i++)  << ": ";
    solve();
    //        cout << "\\n";
  }
}

#define int int
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
  cout << fixed << setprecision(20);

#ifdef multi
  multisolve();
#else
  solve();
//    gen();
#endif

#ifdef LOCAL
  // cout << '\\n' << (double)clock() / CLOCKS_PER_SEC << '\\n';
#endif
}