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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
const int INF = 1e9;

int main() {
  int k, n;
  cin >> k >> n;
  vector<ll> a(n, 0);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<ll> dist(n, 0);
  for (int i = 1; i < n; i++) {
    dist[i - 1] = a[i] - a[i - 1];
  }
  dist[n - 1] = k - a[n - 1] + a[0];

  sort(dist.begin(), dist.end());
  ll sum = 0;
  for (int i = 0; i < n - 1; i++) {
    sum += dist[i];
  }
  cout << sum << endl;
}

Error: -6
malloc(): corrupted top size


Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
const int INF = 1e9;

int main() {
  int k, n;
  cin >> k >> n;
  vector<ll> a(n + 1, 0);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  a[n] = k;
  vector<ll> dist(n, 0);
  for (int i = 0; i <= n; i++) {
    dist[i] = a[i + 1] - a[i];
  }

  sort(dist.begin(), dist.end());
  ll sum = 0;
  for (int i = 0; i < n - 1; i++) {
    sum += dist[i];
  }
  cout << sum << endl;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
int n[200005];
int main() {

  int a, b, c, i, o, ans, l;
  while (~scanf("%d%d", &a, &b)) {
    ans = 0;
    for (i = 0; i < b; i++) {
      scanf("%d", &n[i]);
    }
    o = n[b - 1] - n[0];
    for (i = 0; i < b - 1; i++) {
      l = n[i + 1] - n[i];
      ans = max(ans, l);
    }
    ans = a - ans;
    ans = min(ans, o);
    printf("%d\\n", ans);
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;

int main() {
  int n[20005];
  int a, b, c, i, o, ans, l;
  while (~scanf("%d%d", &a, &b)) {
    ans = 0;
    for (i = 0; i < b; i++) {
      scanf("%d", &n[i]);
    }
    o = n[b - 1] - n[0];
    for (i = 0; i < b - 1; i++) {
      l = n[i + 1] - n[i];
      ans = max(ans, l);
    }
    ans = a - ans;
    ans = min(ans, o);
    printf("%d\\n", ans);
  }
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define MOD 1000000007
#define md % 998244353
#define INF 2e9
#define EPS 1e-9
#define MAX LLONG_MAX
#define MIN LLONG_MIN
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define endl "\\n"
#define FOR(i, a, b) for (long long i = a; i < b; i++)
#define REV(i, a, b) for (long long i = a; i > b; i--)
#define mset(a, v) memset(a, v, sizeof(a))
#define fd(n) fixed << setprecision(n)
typedef std::pair<int, int> pii;
typedef std::vector<long long> vi;
typedef std::vector<vi> vvi;
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

ll solve(ll x, ull y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

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

  ll i, j, n, k;
  cin >> n >> k;
  ll a[k];
  for (i = 0; i < k; i++)
    cin >> a[i];

  sort(a, a + k);
  ll ans = 0;

  ll s[k] = {0};

  for (i = 0; i < k - 1; i++)
    s[i] = a[i + 1] - a[i];

  s[k - 1] = n - a[k - 1] + a[0];

  sort(s, s + k);
  ll sum = 0;
  for (i = 0; i < k - 1; i++)
    sum = sum + s[i];

  cout << sum;
}


Error: -6
*** stack smashing detected ***: terminated


Fixed submission:

#include <bits/stdc++.h>
#define MOD 1000000007
#define md % 998244353
#define INF 2e9
#define EPS 1e-9
#define MAX LLONG_MAX
#define MIN LLONG_MIN
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define endl "\\n"
#define FOR(i, a, b) for (long long i = a; i < b; i++)
#define REV(i, a, b) for (long long i = a; i > b; i--)
#define mset(a, v) memset(a, v, sizeof(a))
#define fd(n) fixed << setprecision(n)
typedef std::pair<int, int> pii;
typedef std::vector<long long> vi;
typedef std::vector<vi> vvi;
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

ll solve(ll x, ull y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

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

  ll i, j, n, k;
  cin >> n >> k;
  ll a[k];
  for (i = 0; i < k; i++)
    cin >> a[i];

  sort(a, a + k);
  ll ans = 0;
  ll x, y, z, w;

  y = a[1] - a[0];
  z = a[2] - a[1];
  w = n - a[2] + a[0];

  ll b[3];

  b[1] = y;
  b[2] = z;
  b[3] = w;

  sort(b, b + 3);
  ll sum = 0;
  for (i = 0; i < 2; i++)
    sum = sum + b[i];

  cout << sum;
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <float.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int main() {
  int k, n;
  cin >> k >> n;
  int a[n], l[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n - 1; i++) {
    l[i] = a[i + 1] - a[i];
  }
  l[n - 1] = k + a[0] - a[n - 1];

  for (int i = 0; i < n - 1; i++) {
    if (l[i] > l[i + 1]) {
      int pre = l[i];
      l[i] = l[i + 1];
      l[i + 1] = pre;
    }
  }

  cout << k - l[n - 1];
}

Error: WA

Fixed submission:

#include <algorithm>
#include <float.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int main() {
  int k, n;
  cin >> k >> n;
  int a[n], l[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n - 1; i++) {
    l[i] = a[i + 1] - a[i];
  }
  l[n - 1] = k + a[0] - a[n - 1];

  for (int i = 0; i < n; i++) {
    if (l[i] > l[i + 1]) {
      int pre = l[i];
      l[i] = l[k];
      l[k] = pre;
    }
  }

  cout << k - l[n - 1];
}
Problem p02725 (C++) — buggy submission, verdict: Runtime Error

#include <cctype>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
int a[1000000];
int answer[1000000];
int main() {
  int n;
  int k;
  int x;
  cin >> k >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    if (i == 1) {
      if (k - a[n] + a[i] >= a[i + 1] - a[i]) {
        answer[i] = a[n] - a[i];
      } else {
        answer[i] = k - (a[i + 1] - a[i]);
      }
    } else if (i == n) {
      if (k - a[n] + a[1] >= a[i] - a[i - 1]) {
        answer[i] = a[n] - a[1];
      } else {
        answer[i] = k - (a[i] - a[i - 1]);
      }
    } else {
      if (a[i] - a[i - 1] >= a[i + 1] - a[i]) {
        answer[i] = k - (a[i] - a[i - 1]);
      } else {
        answer[i] = k - (a[i + 1] - a[i]);
      }
    }
  }
  x = k + 1;
  for (int i = 1; i <= n; i++) {
    if (answer[i] < x) {
      x = answer[i];
    }
  }
  cout << x << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <cctype>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
long long a[100000];
long long answer[100000];
int main() {
  long long n;
  long long k;
  long long x;
  cin >> k >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (long long i = 1; i <= n; i++) {
    if (i == 1) {
      if (k - a[n] + a[i] >= a[i + 1] - a[i]) {
        answer[i] = a[n] - a[i];
      } else {
        answer[i] = k - (a[i + 1] - a[i]);
      }
    } else if (i == n) {
      if (k - a[n] + a[1] >= a[i] - a[i - 1]) {
        answer[i] = a[n] - a[1];
      } else {
        answer[i] = k - (a[i] - a[i - 1]);
      }
    } else {
      if (a[i] - a[i - 1] >= a[i + 1] - a[i]) {
        answer[i] = k - (a[i] - a[i - 1]);
      } else {
        answer[i] = k - (a[i + 1] - a[i]);
      }
    }
  }
  x = k + 1;
  for (long long i = 1; i <= n; i++) {
    if (answer[i] < x) {
      x = answer[i];
    }
  }
  cout << x << endl;

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

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

#define int long long
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

signed main() {
  int k, n;
  cin >> k >> n;
  int a[200010];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  a[n] = k + a[0];

  int ans = k;
  for (int i = 0; i < n; i++) {
    chmin(ans, k - (a[i + 1] - a[i]));
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

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

#define int long long
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

signed main() {
  int k, n;
  cin >> k >> n;
  int a[100010];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  a[n] = k + a[0];

  int ans = k;
  for (int i = 0; i < n; i++) {
    chmin(ans, k - (a[i + 1] - a[i]));
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int k, n;
int a[N];
int main() {
  int i, ans;
  scanf("%d%d", &k, &n);
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  sort(a + 1, a + n + 1);
  ans = a[n] - a[1];
  for (i = 2; i <= n; i++)
    ans = min(ans, k - a[i] + a[i - 1]);
  printf("%d\\n", ans);
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int i, j = 0, k = 0, arr[100005], n = 0, large = 0, small = 0, a = 0, b = 0;
  cin >> k;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> arr[i];
  }
  small = 2147483646;
  large = arr[1];
  for (i = 1; i <= n; i++) {
    if (arr[i] > large) {
      large = arr[i];
    }
    if (arr[i] == 0) {

    } else {
      if (arr[i] < small) {
        small = arr[i];
      }
    }
  }

  a = large - small;
  b = (k + small) - large;
  if (a >= b) {
    cout << b;
  } else {
    cout << a;
  }

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

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

int distance(int i, int j, int X, int Y) {
  int result;
  result = min(abs(i - j), abs(X - i) + 1 + abs(Y - j));
  result = min(result, abs(Y - i) + 1 + abs(X - j));
  return result;
}
int main() {
  // cin.tie(0);
  // ios::sync_with_stdio(false);

  int N, X, Y;
  scanf("%d %d %d", &N, &X, &Y);

  vector<long long> count(N, 0);
  // cout << "k=" << k << "\\n";
  for (int i = 1; i < N; i++) {
    for (int j = i + 1; j <= N; j++) {
      // cout << i << " " << j << " " << distance(i, j, X, Y) << "\\n";
      count[distance(i, j, X, Y)]++;
    }
  }
  for (int k = 1; k <= N - 1; k++) {

    cout << count[k] << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int distance(int i, int j, int X, int Y) {
  int result;
  result = min(abs(i - j), abs(X - i) + 1 + abs(Y - j));
  result = min(result, abs(Y - i) + 1 + abs(X - j));
  return result;
}
int main() {
  // cin.tie(0);
  // ios::sync_with_stdio(false);

  int N, X, Y;
  scanf("%d %d %d", &N, &X, &Y);

  long long count;
  for (int k = 1; k <= N - 1; k++) {
    count = 0;
    // cout << "k=" << k << "\\n";
    for (int i = 1; i < N; i++) {
      for (int j = i + 1; j <= N; j++) {
        // cout << i << " " << j << " " << distance(i, j, X, Y) << "\\n";
        if (distance(i, j, X, Y) == k) {
          count++;
        }
      }
    }
    cout << count << "\\n";
  }
  return 0;
}
Problem p02726 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rng(a) a.begin(), a.end()
using namespace std;
using ll = long long;
using P = pair<int, int>;

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rng(a) a.begin(), a.end()
using namespace std;
using ll = long long;
using P = pair<int, int>;

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

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

int main() {
  int N, X, Y;
  cin >> N >> X >> Y;

  vector<vector<int>> dist(N, vector<int>(N, 0));
  vector<int> count(N - 1, 0);
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      int d1 = max(Y - 1 - j, j - Y + 1);
      int d2 = max(i - X + 1, X - 1 - i);
      count.at(min(j - i, d1 + d2 + 1) - 1) += 1;
    }
  }

  for (int i = 0; i < N - 1; i++) {
    cout << count.at(i) << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int N, X, Y;
  cin >> N >> X >> Y;

  vector<vector<int>> dist(N, vector<int>(N, 0));
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      int d1 = max(Y - 1 - j, j - Y + 1);
      int d2 = max(i - X + 1, X - 1 - i);
      dist.at(i).at(j) = min(j - i, d1 + d2 + 1);
    }
  }

  for (int k = 1; k < N; k++) {
    int count = 0;
    for (int i = 0; i < N; i++) {
      for (int j = i + 1; j < N; j++) {
        if (dist.at(i).at(j) == k) {
          count += 1;
        }
      }
    }
    cout << count << endl;
  }
}
Problem p02726 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

int main() {
  int n, x, y;
  cin >> n >> x >> y;
  int d[n][n];
  vector<int> ans(n, 0);
  rep(i, n) rep(j, n) d[i][j] = abs(i - j);
  x--;
  y--;
  rep(i, n) rep(j, n) {
    d[i][j] = min(d[i][j], d[i][x] + d[y][j] + 1);
    d[i][j] = min(d[i][j], d[i][y] + d[x][j] + 1);
  }
  rep(i, n) rep(j, n) ans[d[i][j]]++;
  rep(i, n - 1) cout << ans[i + 1] / 2 << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  int n, inf = 2000 + 1;
  cin >> n;
  int d[n][n];
  vector<int> ans(n, 0);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i != j) {
        d[i][j] = inf;
      } else {
        d[i][j] = 0;
      }
    }
  }
  for (int i = 0; i < n - 1; i++) {
    d[i][i + 1] = 1;
    d[i + 1][i] = 1;
  }
  int x, y;
  cin >> x >> y;
  d[x - 1][y - 1] = 1;
  d[y - 1][x - 1] = 1;
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (d[i][j] > d[i][k] + d[k][j])
          d[i][j] = d[i][k] + d[k][j];
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      ans[d[i][j]]++;
    }
  }
  for (int i = 1; i < n; i++) {
    cout << ans[i] / 2 << endl;
  }
}
Problem p02726 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int a[2010];
int main() {
  int n, x, y;
  cin >> n >> x >> y;
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if (x <= i && y >= j) {
        a[min(j - i, i - x + y - j + 1)]++;
      } else {
        if (x >= i && y <= j) {
          a[j - i - (y - x) + 1]++;
        } else {
          if (j >= x && j <= y && i <= x) {
            a[min(j - i, y - j + x - i + 1)]++;
          } else {
            if (j >= y && i <= y && i >= x) {
              a[min(j - i, i - x + j - y + 1)]++;
            } else {
              a[j - i]++;
            }
          }
        }
      }
    }
  }
  for (int i = 1; i <= n - 1; i++) {
    cout << a[i] << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int a[1010];
int main() {
  int n, x, y;
  cin >> n >> x >> y;
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if (x <= i && y >= j) {
        a[min(j - i, i - x + y - j + 1)]++;
      } else {
        if (x >= i && y <= j) {
          a[j - i - (y - x) + 1]++;
        } else {
          if (j >= x && j <= y && i <= x) {
            a[min(j - i, y - j + x - i + 1)]++;
          } else {
            if (j >= y && i <= y && i >= x) {
              a[min(j - i, i - x + j - y + 1)]++;
            } else {
              a[j - i]++;
            }
          }
        }
      }
    }
  }
  for (int i = 1; i <= n - 1; i++) {
    cout << a[i] << endl;
  }
}
Problem p02726 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

const int N = 2000;
const long long INF = 1 << 21;

long long n, k, M[N][N];
long long dist[N], ans[N];

void bfs(int s) {
  rep(i, n) dist[i] = INF;

  queue<int> q;

  q.push(s);
  dist[s] = 0;

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

    for (int v = 0; v < n; v++) {
      if (M[u][v] == 1 && dist[v] == INF) {
        dist[v] = dist[u] + 1;
        q.push(v);
      }
    }
  }
}

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

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

  for (int i = 0; i < n - 1; i++) {
    M[i][i + 1] = 1;
    M[i + 1][i] = 1;
  }
  M[x][y] = M[y][x] = 1;

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

  bfs(0);

  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (i <= x) {
        ans[dist[j] - i]++;
      } else if (x < i && j < y && i < y) {
        ans[min(i - x + 1 + y - j, j - i)]++;
      } else if (x < i && y <= j && i < y) {
        ans[min(i - x + 1, y - i) + j - y]++;
      } else {
        ans[dist[j] - dist[i]]++;
      }
    }
  }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

const int N = 2000;
const int INF = 1 << 10;

int n, k, M[N][N], dist[N], ans[N];

void bfs(int s) {
  rep(i, n) dist[i] = INF;

  queue<int> q;

  q.push(s);
  dist[s] = 0;

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

    for (int v = 0; v < n; v++) {
      if (M[u][v] == 1 && dist[v] == INF) {
        dist[v] = dist[u] + 1;
        q.push(v);
      }
    }
  }
}

void solve() {
  rep(i, n + 1) { ans[i] = 0; }

  rep(i, n) {
    bfs(i);
    for (int j = i + 1; j < n; j++) {
      ans[dist[j]]++;
    }
  }

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

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

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

  for (int i = 0; i < n - 1; i++) {
    M[i][i + 1] = 1;
    M[i + 1][i] = 1;
  }
  M[x][y] = M[y][x] = 1;

  solve();

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

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

int main() {
  int n, x, y, a;
  cin >> n >> x >> y;
  vector<int> num(n, 0);
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++)
      num[min(abs(j - i), abs(y - j) + abs(x - i) + 1)]++;

  for (int i = 1; i <= n - 1; i++)
    cout << num[i] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
// const int n+2 = 2 * 1000 + 3;
const ll INF = LLONG_MAX / 2;

int main() {
  ll n, x, y;
  cin >> n >> x >> y;
  ll d[n + 2][n + 2];
  x--, y--;
  rep(i, n + 2) rep(j, n + 2) d[i][j] = INF;
  rep(i, n + 2) d[i][i] = 0;

  for (int i = 0; i < n - 1; ++i)
    d[i][i + 1] = 1, d[i + 1][i] = 1;
  d[x][y] = 1;
  d[y][x] = 1;

  // rep(i, n) {
  //   rep(j, n) { printf("%10d ", d[i][j]); }
  //   cout << endl;
  // }
  // cout << endl;

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

  // rep(i, n + 1) {
  //   rep(j, n + 1) { printf("%10d ", d[i][j]); }
  //   cout << endl;
  // }

  vector<ll> ans(n);
  rep(i, n) ans[i] = 0;
  for (int k = 1; k < n; ++k) {
    for (int i = 0; i < n; ++i) {
      for (int j = i + 1; j <= n; ++j) {
        if (d[i][j] == k)
          ans[k]++;
      }
    }
  }
  for (int i = 1; i < n; ++i)
    cout << ans[i] << endl;
  return 0;
}
Problem p02726 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

struct edge {
  int from, to;
  edge(int from, int to) : from(from), to(to) {}
};
using node = vector<edge>;
using graph = vector<node>;
void addedge(graph &g, int from, int to, bool bidirectional = false) {
  g[from].emplace_back(edge(from, to));
  if (bidirectional)
    g[to].emplace_back(edge(to, from));
}

class Solution {
public:
  void solve() {
    int n, x, y;
    cin >> n >> x >> y;
    x--, y--;
    cout << "\\n";
    graph g(n);
    for (int i = 0; i < n - 1; i++)
      addedge(g, i, i + 1, true);
    addedge(g, x, y, true);
    vector<int> ans(n);
    for (int i = 0; i < n; i++) {
      queue<int> q;
      q.push(i);
      vector<int> d(n, 0x3f3f3f3f);
      d[i] = 0;
      while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (auto e : g[u]) {
          if (d[e.to] == 0x3f3f3f3f) {
            d[e.to] = d[u] + 1;
            q.push(e.to);
          }
        }
      }
      for (int j = i + 1; j < n; j++) {
        ans[d[j]]++;
      }
    }
    for (int i = 1; i < n; i++)
      cout << ans[i] << "\\n";
    cout << "\\n";

    return;
  };
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  Solution solution;
  solution.solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

class Solution {
public:
  void solve() {
    int n, x, y;
    cin >> n >> x >> y;
    x--, y--;
    vector<vector<int>> d(n, vector<int>(n, 0x3f3f3f3f));
    for (int i = 0; i < n; i++)
      d[i][i] = 0;
    for (int i = 0; i < n - 1; i++)
      d[i][i + 1] = d[i + 1][i] = 1;
    d[x][y] = d[y][x] = 1;
    for (int k = 0; k < n; k++) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
        }
      }
    }
    vector<int> ans(n, 0);
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        ans[d[i][j]]++;
      }
    }
    for (int i = 1; i < n; i++)
      cout << ans[i] << "\\n";
    cout << "\\n";

    return;
  };
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  Solution solution;
  solution.solve();
  return 0;
}