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

#include <bits/stdc++.h>

#include <iterator>
#include <set>
using namespace std;
// using namespace __gnu_pbds;

#define f first
#define s second
#define pb push_back
#define all(x) x.begin(), x.end()

#define ll long long int
#define vi vector<int>
#define vii vector<pair<int, int>>
#define pii pair<int, int>
#define plii pair<pair<ll, int>, int>
#define piii pair<pii, int>
#define viii vector<pair<pii, int>>
#define vl vector<ll>
#define vll vector<pair<ll, ll>>
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define mp make_pair
#define ms(x, v) memset(x, v, sizeof x)
#define pr_vec(v)                                                              \\
  for (int i = 0; i < v.size(); i++)                                           \\
    cout << v[i] << " ";

#define f_in(st) freopen(st, "r", stdin)
#define f_out(st) freopen(st, "w", stdout)

#define INF (ll)1e17
#define matrix vector<vector<ll>>

#define fr(i, a, b) for (i = a; i <= b; i++)
#define fb(i, a, b) for (i = a; i >= b; i--)
#define ASST(x, l, r) assert(x <= r && x >= l)
#define maxbits 9
#define maxmask (1ll << maxbits)
// typedef tree<pll, null_type, less<pll>, rb_tree_tag,
//              tree_order_statistics_node_update>
//     new_data_set;
const int N = 2e5 + 10, M = (ll)1e9 + 1;
const int mod = (ll)998244353, sz = 5, MAX = (ll)1e6 + 10;
string fir = "Monocarp", sec = "Bicarp";

void work() {
  ll n, x, y;
  cin >> n >> x >> y;
  ll dist[n + 1][n + 1];
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= n; j++) {
      dist[i][j] = INF;
    }
  }

  for (ll k = 0; k < n; k++) {
    for (ll i = 1; i <= n; i++) {
      if (i + k <= n) {
        dist[i][i + k] = k;
        dist[i + k][i] = k;
      }
      if (i - k > 0) {
        dist[i][i - k] = k;
        dist[i - k][i] = k;
      }
    }
  }

  dist[x][y] = 1;
  dist[y][x] = 1;

  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= n; j++) {
      if (dist[i][j] > dist[i][x] + dist[x][j]) {
        dist[i][j] = dist[i][x] + dist[x][j];
        dist[j][i] = dist[i][j];
      }

      if (dist[i][j] > dist[i][y] + dist[y][j]) {
        dist[i][j] = dist[i][y] + dist[y][j];
        dist[j][i] = dist[i][j];
      }
    }
  }
  vl ans(n + 1, 0);

  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= n; j++) {
      if (dist[i][j] != INF)
        ans[dist[i][j]]++;
    }
  }

  for (ll i = 1; i < n; i++)
    cout << ans[i] / 2 << "\\n";
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll t;
  // cin >> t;
  t = 1;
  while (t--) {
    work();
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#include <iterator>
#include <set>
using namespace std;
// using namespace __gnu_pbds;

#define f first
#define s second
#define pb push_back
#define all(x) x.begin(), x.end()

#define ll long long int
#define vi vector<int>
#define vii vector<pair<int, int>>
#define pii pair<int, int>
#define plii pair<pair<ll, int>, int>
#define piii pair<pii, int>
#define viii vector<pair<pii, int>>
#define vl vector<ll>
#define vll vector<pair<ll, ll>>
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define mp make_pair
#define ms(x, v) memset(x, v, sizeof x)
#define pr_vec(v)                                                              \\
  for (int i = 0; i < v.size(); i++)                                           \\
    cout << v[i] << " ";

#define f_in(st) freopen(st, "r", stdin)
#define f_out(st) freopen(st, "w", stdout)

#define INF (ll)1e17
#define matrix vector<vector<ll>>

#define fr(i, a, b) for (i = a; i <= b; i++)
#define fb(i, a, b) for (i = a; i >= b; i--)
#define ASST(x, l, r) assert(x <= r && x >= l)
#define maxbits 9
#define maxmask (1ll << maxbits)
// typedef tree<pll, null_type, less<pll>, rb_tree_tag,
//              tree_order_statistics_node_update>
//     new_data_set;
const int N = 2e5 + 10, M = (ll)1e9 + 1;
const int mod = (ll)998244353, sz = 5, MAX = (ll)1e6 + 10;
string fir = "Monocarp", sec = "Bicarp";

void work() {
  ll n, x, y;
  cin >> n >> x >> y;
  ll dist[n + 1][n + 1];
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= n; j++) {
      dist[i][j] = INF;
    }
  }

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

  dist[n][n] = 0;

  for (ll k = 1; k <= n; k++) {
    for (ll i = 1; i <= n; i++) {
      for (ll j = 1; j <= n; j++) {
        if (dist[i][j] > dist[i][k] + dist[k][j])
          dist[i][j] = dist[i][k] + dist[k][j];
      }
    }
  }

  vl ans(n + 1, 0);

  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= n; j++) {
      if (dist[i][j] != INF)
        ans[dist[i][j]]++;
    }
  }

  for (ll i = 1; i < n; i++)
    cout << ans[i] / 2 << "\\n";
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  // precomp();
  ll t;
  // cin >> t;
  t = 1;
  while (t--) {
    work();
  }
  return 0;
}