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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 10000;

int main() {

  int n, x, y;
  cin >> n >> x >> y;
  --x, --y;
  vector<int> d(n - 1);

  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      int t = min(abs(j - i), abs(i - x) + 1 + abs(y - j));
      d[t - 1]++;
    }
  }
  for (int i : d)
    cout << i << "\\n";
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 10000;

int main() {

  int n, x, y;
  cin >> n >> x >> y;

  vector<vector<int>> g(n, vector<int>(n, N));
  for (int i = 0; i < n - 1; i++) {
    g[i][i + 1] = 1;
    g[i + 1][i] = 1;
  }
  for (int i = 0; i < n; i++)
    g[i][i] = 0;
  g[--x][--y] = 1;
  g[y][x] = 1;

  vector<int> res(n - 1);
  res[0] = 2 * n;

  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (g[i][j] > g[i][k] + g[k][j]) {
          res[g[i][j] - 1]--;
          g[i][j] = g[i][k] + g[k][j];
          res[g[i][j] - 1]++;
        }
      }
    }
  }

  for (int i : res)
    cout << i / 2 << " ";
}
Problem p02726 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int N = 2e3 + 5;

struct edge {
  int v, nxt;
} e[N * 2];
int p[N], eid;
int ans[N], d[N];

void bfs(int s) {
  memset(d, 0x3f, sizeof(d));
  queue<int> q;
  q.push(s);
  d[s] = 0;
  while (q.size()) {
    int u = q.front();
    q.pop();
    for (int i = p[u]; ~i; i = e[i].nxt) {
      int v = e[i].v;
      if (d[u] + 1 < d[v]) {
        d[v] = d[u] + 1;
        q.push(v);
      }
    }
  }
}

int main() {
  int n, x, y;
  cin >> n >> x >> y;
  memset(p, -1, sizeof(p));
  for (int i = 2; i <= n; ++i) {
    e[eid] = edge{i, p[i - 1]};
    p[i - 1] = eid++;
    e[eid] = edge{i - 1, p[i]};
    p[i] = eid++;
  }
  e[eid] = edge{y, p[x]};
  p[x] = eid++;
  e[eid] = edge{x, p[y]};
  p[y] = eid++;

  for (int i = 1; i <= n; ++i) {
    bfs(i);
    for (int i = 1; i <= n; ++i)
      ans[d[i]]++;
  }
  for (int i = 1; i <= n - 1; ++i)
    cout << ans[i] / 2 << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 2e3 + 5;

struct edge {
  int v, nxt;
} e[N];
int p[N], eid;
int ans[N], d[N];

void bfs(int s) {
  memset(d, 0x3f, sizeof(d));
  queue<int> q;
  q.push(s);
  d[s] = 0;
  while (q.size()) {
    int u = q.front();
    q.pop();
    for (int i = p[u]; ~i; i = e[i].nxt) {
      int v = e[i].v;
      if (d[u] + 1 < d[v]) {
        d[v] = d[u] + 1;
        q.push(v);
      }
    }
  }
}

int main() {
  int n, x, y;
  cin >> n >> x >> y;
  memset(p, -1, sizeof(p));
  for (int i = 2; i <= n; ++i) {
    e[eid] = edge{i, p[i - 1]};
    p[i - 1] = eid++;
    e[eid] = edge{i - 1, p[i]};
    p[i] = eid++;
  }
  e[eid] = edge{y, p[x]};
  p[x] = eid++;
  e[eid] = edge{x, p[y]};
  p[y] = eid++;

  for (int i = 1; i <= n; ++i) {
    bfs(i);
    for (int i = 1; i <= n; ++i)
      ans[d[i]]++;
  }
  for (int i = 1; i <= n - 1; ++i)
    cout << ans[i] / 2 << endl;
  return 0;
}
Problem p02726 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long LL;

int main() {
  int N, X, Y;
  cin >> N >> X >> Y;
  --X;
  --Y;
  vector<vector<int>> dist(N, vector<int>(N, 99999));
  REP(i, N) {
    queue<pair<int, int>> q;
    q.push(make_pair(i, 0));
    while (!q.empty()) {
      auto t = q.front();
      q.pop();
      int pos = t.first, d = t.second;
      if (dist[i][pos] <= d)
        continue;
      dist[i][pos] = d;
      if (pos - 1 >= 0)
        q.push(make_pair(pos - 1, d + 1));
      if (pos + 1 < N)
        q.push(make_pair(pos + 1, d + 1));
      if (pos == X)
        q.push(make_pair(Y, d + 1));
      if (pos == Y)
        q.push(make_pair(X, d + 1));
    }
  }
  vector<int> ret(N, 0);
  REP(i, N) {
    for (int j = i + 1; j < N; ++j) {
      ret[dist[i][j]]++;
    }
  }
  for (int i = 1; i < N; ++i) {
    cout << ret[i] << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long LL;

int main() {
  int N, X, Y;
  cin >> N >> X >> Y;
  --X;
  --Y;
  vector<vector<int>> dist(N, vector<int>(N, 99999));
  REP(i, N) {
    queue<pair<int, int>> q;
    q.push(make_pair(i, 0));
    while (!q.empty()) {
      auto t = q.front();
      q.pop();
      int pos = t.first, d = t.second;
      if (dist[i][pos] <= d)
        continue;
      dist[i][pos] = d;
      if (pos - 1 >= 0)
        q.push(make_pair(pos - 1, d + 1));
      if (pos + 1 < N)
        q.push(make_pair(pos + 1, d + 1));
      if (pos == X)
        q.push(make_pair(Y, d + 1));
      if (pos == Y)
        q.push(make_pair(X, d + 1));
    }
  }
  vector<int> ret(N, 0);
  REP(i, N) {
    for (int j = i + 1; j < N; ++j) {
      int td = dist[i][j];
      ret[td] += 1;
      cerr << "(" << i << "," << j << ") " << td << endl;
    }
  }
  for (int i = 1; i < N; ++i) {
    cout << ret[i] << endl;
  }
}
Problem p02726 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define gcd(m, n) __gcd(m, n)
#define lcm(m, n) m *(n / gcd(m, n))
#define fast                                                                   \\
  std::ios_base::sync_with_stdio(false);                                       \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pi acos(-1.0)
#define endl '\\n'
#define MOD 1000000007
#define ull unsigned long long
#define ll long long
#define ld long double
#define pb push_back
#define dbg(x) cout << #x << "  " << x << endl;

ll power(ll base, ll exp) {
  ll res = 1;
  while (exp > 0) {
    if (exp % 2 == 1)
      res = (res * base);
    base = (base * base);
    exp /= 2;
  }
  return res;
}
ll mod(ll a, ll b) { return (a % b + b) % b; }
using namespace std;

int main() {
  fast;
  ll n, x, y;
  cin >> n >> x >> y;
  vector<ll> ans(n + 1, 0);
  for (ll i = 1; i <= n - 1; i++) {
    for (ll j = i + 1; j <= n; j++) {
      ll dist = min(
          j - i, min(1 + abs(x - i) + abs(y - j), 1 + abs(x - j) + abs(y - i)));
      ans[dist]++;
    }
  }
  for (ll i = 1; i <= n - 1; i++)
    cout << ans[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define gcd(m, n) __gcd(m, n)
#define lcm(m, n) m *(n / gcd(m, n))
#define fast                                                                   \\
  std::ios_base::sync_with_stdio(false);                                       \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pi acos(-1.0)
#define endl '\\n'
#define MOD 1000000007
#define ull unsigned long long
#define ll long long
#define ld long double
#define pb push_back
#define dbg(x) cout << #x << "  " << x << endl;

ll power(ll base, ll exp) {
  ll res = 1;
  while (exp > 0) {
    if (exp % 2 == 1)
      res = (res * base);
    base = (base * base);
    exp /= 2;
  }
  return res;
}
ll mod(ll a, ll b) { return (a % b + b) % b; }
using namespace std;

int main() {
  fast;
  ll n, x, y;
  cin >> n >> x >> y;
  for (ll k = 1; k <= n - 1; k++) {
    ll ans = 0;
    for (ll i = 1; i <= n; i++) {
      for (ll j = i + 1; j <= n; j++) {
        ll dist = min(j - i, min(1 + abs(x - i) + abs(y - j),
                                 1 + abs(x - j) + abs(y - i)));
        if (dist == k)
          ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p02726 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<string> vs;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define all(x) (x).begin(), (x).end()
vi dy = {0, 0, 1, -1};
vi dx = {1, -1, 0, 0};
constexpr auto INF = INT_MAX / 2;
constexpr auto INFL = 5000000000000000;
constexpr auto MOD = 1000000007;

int main() {
  int n, x, y;
  cin >> n >> x >> y;
  x--;
  y--;

  vvi path(n, vi(0));
  rep(i, n - 1) {
    path[i].push_back(i + 1);
    path[i + 1].push_back(i);
  }
  path[x].push_back(y);
  path[y].push_back(x);

  vi dist(n, 0);

  rep(i, n) {
    vector<int> di(n, 0);
    queue<int> que;
    que.push(i);

    while (!que.empty()) {
      int sv = que.front();
      que.pop();
      rep(j, path[sv].size()) {
        int nv = path[sv][j];
        if (di[nv] != 0 || nv == i)
          continue;
        di[nv] = di[sv] + 1;
        que.push(nv);
      }
    }
    rep(j, n) dist[di[j]]++;
  }

  rep1(i, n - 1) {
    dist[i] /= 2;
    cout << dist[i] << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
typedef vector<int> vi;
typedef vector<vi> vvi;

int main() {

  int n, x, y;
  cin >> n >> x >> y;

  x--;
  y--;

  int d[2020][2020];

  rep(i, n) rep(j, n) d[i][j] = abs(j - i);
  d[x][y] = 1;
  d[y][x] = 1;

  rep(k, n) rep(i, n) rep(j, n) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);

  rep(k, n - 1) {
    int sum = 0;
    rep(i, n) rep(j, n) {
      if (d[i][j] == k + 1)
        sum++;
    }
    sum /= 2;
    cout << sum << endl;
  }
}
Problem p02726 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
#include <queue>
using namespace std;

int main(void) {
  int n, x, y, *mat[2000], *count = new int[2000], now;
  bool *v = new bool[2000];
  cin >> n >> x >> y;
  --x;
  --y;
  for (int i = 0; i < n; ++i)
    count[i] = 0;
  queue<int> que;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j)
      v[j] = false;
    que.push(i);
    v[i] = true;
    int num[2] = {1, 0};
    int k = 1;
    while (!que.empty()) {
      now = que.front();
      que.pop();
      num[0]--;
      if (now - 1 >= 0 && !v[now - 1]) {
        que.push(now - 1);
        count[k]++;
        v[now - 1] = true;
        num[1]++;
      }
      if (now + 1 < n && !v[now + 1]) {
        que.push(now + 1);
        count[k]++;
        v[now + 1] = true;
        num[1]++;
      }
      if (now == x && !v[y]) {
        que.push(y);
        count[k]++;
        v[y] = true;
        num[1]++;
      }
      if (now == y && !v[x]) {
        que.push(x);
        count[k]++;
        v[x] = true;
        num[1]++;
      }
      if (!num[0]) {
        k++;
        num[0] = num[1];
        num[1] = 0;
      }
    }
  }
  for (int i = 1; i < n; ++i)
    cout << (count[i] / 2) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <queue>
using namespace std;

int main(void) {
  int n, x, y, *mat[2000], *count = new int[2000], now;
  bool *v = new bool[2000];

  cin >> n >> x >> y;
  for (int i = 0; i < n; ++i) {
    count[i] = 0;
    mat[i] = new int[2000];
    for (int j = 0; j < n; ++j) {
      if (abs(i - j) == 1)
        mat[i][j] = 1;
      else if (i == x - 1 && j == y - 1)
        mat[i][j] = 1;
      else if (i == y - 1 && j == x - 1)
        mat[i][j] = 1;
      else
        mat[i][j] = 0;
    }
  }

  queue<int> que;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j)
      v[j] = false;
    que.push(i);
    v[i] = true;
    int num[2] = {1, 0};
    int k = 1;
    while (!que.empty()) {
      now = que.front();
      que.pop();
      num[0]--;
      for (int j = 0; j < n; ++j) {
        if (mat[now][j] == 1 && !v[j]) {
          que.push(j);
          count[k]++;
          v[j] = true;
          num[1]++;
        }
      }
      if (!num[0]) {
        k++;
        num[0] = num[1];
        num[1] = 0;
      }
    }
  }
  for (int i = 1; i < n; ++i)
    cout << (count[i] / 2) << endl;
  return 0;
}
Problem p02726 (C++) — buggy submission, verdict: Time Limit Exceeded

// Winners don't quit and Quitters don't win
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vii vector<int>
#define vll vector<ll>
#define mod 998244353
#define MAX 100005
int ans[2004][2004];
int xx[2004];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int k, n, x, y, i, j, v, c = 0;
  cin >> n >> x >> y;
  for (i = 1; i <= n; ++i) {
    for (j = 1; j <= n; ++j) {
      if (i != j && ans[i][j] == 0) {
        ans[i][j] = j - i;
        ans[j][i] = j - i;
      }
    }
  }
  // vii point;
  ans[x][y] = 1;
  ans[y][x] = 1;
  for (i = 1; i <= n; ++i) {
    for (j = 1; j <= n; ++j) {
      if ((ans[i][x] + ans[x][y] + ans[y][j]) < ans[i][j]) {
        ans[i][j] = (ans[i][x] + ans[x][y] + ans[y][j]);
      }
    }
  }
  for (i = 1; i <= n; ++i) {
    for (j = i + 1; j <= n; ++j) {
      xx[ans[i][j]]++;
    }
  }
  for (i = 1; i < n; ++i) {
    cout << xx[i] << '\\n';
  }
}

Error: TLE

Fixed submission:

// Winners don't quit and Quitters don't win
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vii vector<int>
#define vll vector<ll>
#define mod 998244353
#define MAX 100005
int ans[2004][2004];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int k, n, x, y, i, j, v, c = 0;
  cin >> n >> x >> y;
  for (i = 1; i <= n; ++i) {
    for (j = 1; j <= n; ++j) {
      if (i != j && ans[i][j] == 0) {
        ans[i][j] = j - i;
        ans[j][i] = j - i;
      }
    }
  }
  vii point;
  ans[x][y] = 1;
  ans[y][x] = 1;
  for (i = 1; i <= n; ++i) {
    for (j = 1; j <= n; ++j) {
      if ((ans[i][x] + ans[x][y] + ans[y][j]) < ans[i][j]) {
        ans[i][j] = (ans[i][x] + ans[x][y] + ans[y][j]);
      }
    }
  }
  for (k = 1; k < n; ++k) {
    c = 0;
    for (i = 1; i <= n; ++i) {
      for (j = i + 1; j <= n; ++j) {
        if (ans[i][j] == k) {
          c++;
        }
      }
    }
    point.pb(c);
  }
  for (i = 0; i < point.size(); ++i) {
    cout << point[i] << '\\n';
  }
}
Problem p02726 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define f1(n) for (int i = 0; i < n; ++i)
#define f2(n) for (int i = 1; i <= n; ++i)
#define lson(x) x << 1
#define rson(x) x << 1 | 1
#define mp make_pair
using namespace std;
typedef long long ll;
typedef long double llf;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
const int maxn = 2e3 + 5;
const int mod = 0x3f3f3f3f;
const ll Mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll Inf = (ll)1e15;
int dis[maxn][maxn];
map<int, int> p;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int n, x, y;
  cin >> n >> x >> y;

  for (int i = 1; i <= n; ++i) {
    for (int j = i; j <= n; ++j) {
      dis[i][j] = dis[j][i] = j - i;
    }
  }

  dis[x][y] = dis[y][x] = 1;
  int k = x;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      if (dis[i][j] > dis[i][k] + dis[k][j]) {
        dis[i][j] = dis[i][k] + dis[k][j];
      }
    }
  }
  k = y;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      if (dis[i][j] > dis[i][k] + dis[k][j]) {
        dis[i][j] = dis[i][k] + dis[k][j];
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = i + 1; j <= n; ++j) {
      p[dis[i][j]]++;
    }
  }
  for (int i = 1; i < n; ++i) {
    cout << p[i] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define f1(n) for (int i = 0; i < n; ++i)
#define f2(n) for (int i = 1; i <= n; ++i)
#define lson(x) x << 1
#define rson(x) x << 1 | 1
#define mp make_pair
using namespace std;
typedef long long ll;
typedef long double llf;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
const int maxn = 1e3 + 5;
const int mod = 0x3f3f3f3f;
const ll Mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll Inf = (ll)1e15;
int dis[maxn][maxn];
map<int, int> p;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int n, x, y;
  cin >> n >> x >> y;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      dis[i][j] = inf;
    }
    dis[i][i] = 0;
  }
  for (int i = 1; i <= n - 1; ++i) {
    dis[i][i + 1] = 1;
    dis[i + 1][i] = 1;
  }
  dis[x][y] = dis[y][x] = 1;
  for (int k = 1; k <= n; ++k) {
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= n; ++j) {
        if (dis[i][j] > dis[i][k] + dis[k][j]) {
          dis[i][j] = dis[i][k] + dis[k][j];
        }
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = i + 1; j <= n; ++j) {
      p[dis[i][j]]++;
    }
  }
  for (int i = 1; i < n; ++i) {
    cout << p[i] << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, string>;
using Map = map<string, ll>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;

int main() {
  ll N;
  cin >> N;
  ll X;
  cin >> X;
  ll Y;
  cin >> Y;
  vector<ll> counter(N, 0);
  for (ll i = 1; i <= N - 1; i++) {
    for (ll j = i + 1; j <= N; j++) {
      counter[min(min(j - i, 1 + llabs(i - X) + llabs(j - Y)),
                  1 + llabs(j - X) + llabs(i - Y))]++;
    };
  }
  for (ll i = 1; i < N; i++) {
    cout << counter[i] << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, char>;

ll F(ll a, ll b, ll x, ll y) {
  if (a == b) {
    return 0;
  }
  ll aa = min(a, b);
  ll bb = max(a, b);
  if (bb <= x) {
    return bb - aa;
  } else if (aa >= y) {
    return bb - aa;
  } else if (aa <= x && x < bb && bb < y) {
    return x - aa + min(bb - x, y - bb + 1);
  } else if (aa <= x && bb >= y) {
    return bb - y + x - aa + 1;
  } else if (x < aa && aa < y && bb >= y) {
    return bb - y + min(y - aa, aa - x + 1);
  } else if (x < aa && aa < y && x < bb && bb < y) {
    return min(bb - aa, y - x + 1 - bb + aa);
  }
}

int main() {
  ll N, X, Y;
  cin >> N >> X >> Y;
  for (int k = 1; k < N; k++) {
    int counter = 0;
    for (int i = 1; i < N + 1; i++) {
      for (int j = 1; j < N + 1; j++) {
        if (F(i, j, X, Y) == k) {
          counter++;
        };
      };
    }
    cout << counter / 2 << endl;
    ;
  }
  // cout<<F(2,3,X,Y);

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

#include <bits/stdc++.h>
using namespace std;
int N, X, Y;
vector<vector<int>> G;
vector<vector<int>> dist;
vector<int> res;

void bfs(int s) {
  dist[s][s] = 0;
  queue<int> Q;
  Q.push(s);
  while (!Q.empty()) {
    int v = Q.front();
    Q.pop();
    for (int to : G[v]) {
      if (dist[s][to] == -1) {
        dist[s][to] = dist[s][v] + 1;
        Q.push(to);
      }
    }
  }

  for (int i = 0; i < N; ++i) {
    if (i == s)
      continue;
    res[dist[s][i]] += 1;
  }
}

int main() {
  cin >> N >> X >> Y;
  G.resize(N);
  dist.resize(N, vector<int>(N, -1));
  res.resize(N, 0);
  for (int i = 0; i + 1 < N; ++i) {
    G[i].push_back(i + 1);
    G[i + 1].push_back(i);
  }
  G[X - 1].push_back(Y - 1);
  G[Y - 1].push_back(X - 1);

  for (int i = 0; i < N; ++i)
    bfs(i);

  for (int k = 1; k < N; ++k)
    cout << res[k] / 2 << '\\n';
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int N, X, Y;
vector<vector<int>> G;
vector<vector<int>> dist;
map<int, map<int, int>> D;

void bfs(int s) {
  dist[s][s] = 0;
  queue<int> Q;
  Q.push(s);
  while (!Q.empty()) {
    int v = Q.front();
    Q.pop();
    for (int to : G[v]) {
      if (dist[s][to] == -1) {
        dist[s][to] = dist[s][v] + 1;
        Q.push(to);
      }
    }
  }

  for (int i = 0; i < N; ++i) {
    if (i == s)
      continue;
    D[s][dist[s][i]] += 1;
  }
}

int main() {
  cin >> N >> X >> Y;
  G.resize(N);
  dist.resize(N, vector<int>(N, -1));
  for (int i = 0; i + 1 < N; ++i) {
    G[i].push_back(i + 1);
    G[i + 1].push_back(i);
  }
  G[X - 1].push_back(Y - 1);
  G[Y - 1].push_back(X - 1);

  for (int i = 0; i < N; ++i)
    bfs(i);

  for (int k = 1; k < N; ++k) {
    int res = 0;
    for (int i = 0; i < N; ++i)
      res += D[i][k];
    cout << res / 2 << '\\n';
  }
}
Problem p02726 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int NOT_VISITED = -1;

void bfs(int v, vector<vector<int>> &g, queue<int> &q, vector<int> &dist) {
  dist[v] = 0;
  q.push(v);

  while (!q.empty()) {
    int v = q.front();
    q.pop();

    for (auto nv : g[v]) {
      if (dist[nv] != NOT_VISITED)
        continue;
      q.push(nv);
      dist[nv] = dist[v] + 1;
    }
  }
}

int main() {
  int n, x, y;
  cin >> n >> x >> y;
  --x, --y;

  vector<vector<int>> g(n);
  for (int i = 0; i < n - 1; ++i) {
    g[i].push_back(i + 1);
    g[i + 1].push_back(i);
  }
  g[x].push_back(y);
  g[y].push_back(x);

  vector<int> ans(n + 1);
  for (int i = 0; i < n; ++i) {
    vector<int> dist(n, NOT_VISITED);
    queue<int> q;

    bfs(i, g, q, dist);

    for (int j = 0; j < n; ++j) {
      int x = dist[j];
      if (x > 0)
        ans[x]++;
    }
  }

  for (int i = 1; i < n; ++i)
    cout << ans[i] / 2 << endl;
}

Error: TLE

Fixed submission:

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

const int INF = 1'000'000'000;

int main() {
  int n, x, y;
  cin >> n >> x >> y;
  --x, --y;

  vector<vector<int>> dist(n, vector<int>(n, INF));
  for (int i = 0; i < n - 1; ++i)
    dist[i][i + 1] = dist[i + 1][i] = 1;
  dist[x][y] = dist[y][x] = 1;

  for (int k = 0; k < n; ++k)
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j)
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);

  vector<int> ans(n);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      int x = dist[i][j];
      if (i == j)
        continue;
      ans[x]++;
    }
  }

  for (int i = 1; i < n; ++i)
    cout << ans[i] / 2 << endl;
}
Problem p02726 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long int ll;
typedef pair<ll, ll> P;
int cc[2002];

int main() {
  int n, x, y;
  cin >> n >> x >> y;
  for (int i = 0; i <= n; i++) {
    cc[i] = 0;
  }
  for (int a = 1; a <= n; a++) {
    for (int b = a + 1; b <= n; b++) {
      cc[min(b - a, abs(a - x) + abs(b - y) + 1)]++;
    }
  }
  for (int i = 1; i < n; i++) {
    cout << cc[i] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

#define INF 1000000

typedef long long int ll;
typedef pair<ll, ll> P;
int cost[20000];
int d[2001][2001]; // 各辺間のコスト
int V;
void warshall_floyd() {
  for (int k = 1; k <= V; k++) {
    for (int i = 1; i <= V; i++) {
      for (int j = 1; j <= V; j++) {
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }
}

int main() {
  int n, x, y;
  cin >> n >> x >> y;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      d[i][j] = INF;
    }
  }
  for (int i = 1; i < n; i++) {
    d[i][i + 1] = 1;
    d[i + 1][i] = 1;
  }
  d[x][y] = 1;
  d[y][x] = 1;
  V = n;
  warshall_floyd();
  for (int i = 0; i < n; i++) {
    cost[i] = 0;
  }
  for (int x = 1; x <= n; x++) {
    for (int y = x + 1; y <= n; y++) {
      cost[d[x][y]]++;
    }
  }
  for (int i = 1; i < n; i++) {
    cout << cost[i] << endl;
  }

  return 0;
}