Problem p02726 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define fastio                                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);                                                              \\
  ios_base::sync_with_stdio(0)
typedef long long ll;
// #define int ll
#define swap(x, y) ((x) ^= (y) ^= (x) ^= (y))
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vector<int>> matrix;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define all(c) c.begin(), c.end()
#define mp make_pair
#define pb push_back
#define f first
#define tcase                                                                  \\
  int _;                                                                       \\
  cin >> _;                                                                    \\
  for (; _; --_)
#define s second
#define endl '\\n'
const int MAX = 1e6 + 1;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1);
const double DEG = 180 / PI;
const int MOD = 1e9 + 7;
int mod(int x, int m) {
  if (x >= 0)
    return x % m;
  if (-x < m)
    return m - (-x);
  return mod(x % m, m);
}
int gcd(int a, int b) {
  while (b) {
    a = mod(a, b);
    swap(a, b);
  }
  return a;
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
int fexp(int base, int exp, int m) {
  int total = 1;
  while (exp > 0) {
    if (exp % 2 == 1) {
      total = mod(((mod(total, m)) * (mod(base, m))), m);
      exp--;
    }
    base = mod((mod(base, m) * mod(base, m)), m);
    exp /= 2;
  }
  return mod(total, m);
}

int n;

void solve() {
  int x, y;

  cin >> n >> x >> y;

  int dist[n + 1][n + 1];
  memset(dist, 0, sizeof(dist));

  vi adj[n + 1];

  for (int i = 1; i < n; ++i) {
    adj[i].pb(i + 1);
    adj[i + 1].pb(i);
  }
  adj[x].pb(y);
  adj[y].pb(x);

  int ans = 0;

  for (int i = 1; i <= n; ++i) {
    vector<bool> visited(n + 1, false);
    queue<int> q;

    dist[i][i] = 0;
    visited[i] = true;
    q.push(i);

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

      for (int v : adj[pos]) {
        if (!visited[v]) {
          visited[v] = true;
          dist[v][i] = dist[pos][i] + 1;
          q.push(v);
        }
      }
    }
  }

  vi res(n + 1, 0);
  for (int i = 1; i <= n; ++i)
    for (int j = i + 1; j <= n; ++j)
      res[dist[i][j]]++;
  for (int i = 1; i < n; ++i)
    cout << res[i] << endl;
}

int32_t main() {
  fastio;
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define fastio                                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);                                                              \\
  ios_base::sync_with_stdio(0)
typedef long long ll;
// #define int ll
#define swap(x, y) ((x) ^= (y) ^= (x) ^= (y))
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vector<int>> matrix;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define all(c) c.begin(), c.end()
#define mp make_pair
#define pb push_back
#define f first
#define tcase                                                                  \\
  int _;                                                                       \\
  cin >> _;                                                                    \\
  for (; _; --_)
#define s second
#define endl '\\n'
const int MAX = 1e6 + 1;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1);
const double DEG = 180 / PI;
const int MOD = 1e9 + 7;
int mod(int x, int m) {
  if (x >= 0)
    return x % m;
  if (-x < m)
    return m - (-x);
  return mod(x % m, m);
}
int gcd(int a, int b) {
  while (b) {
    a = mod(a, b);
    swap(a, b);
  }
  return a;
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
int fexp(int base, int exp, int m) {
  int total = 1;
  while (exp > 0) {
    if (exp % 2 == 1) {
      total = mod(((mod(total, m)) * (mod(base, m))), m);
      exp--;
    }
    base = mod((mod(base, m) * mod(base, m)), m);
    exp /= 2;
  }
  return mod(total, m);
}

int n;

void solve() {
  int x, y;

  cin >> n >> x >> y;

  matrix dist(n + 1, vi(n + 1));
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      dist[i][j] = (i == j ? 0 : INF);

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

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

  vi res(n + 1, 0);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      res[dist[i][j]]++;
  for (int i = 1; i < n; ++i)
    cout << res[i] / 2 << endl;
}

int32_t main() {
  fastio;
  solve();
  return 0;
}