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

#include <bits/stdc++.h>
#include <regex>
#define FOR(i, s, e) for (int i = int(s); i < int(e); ++i)
// #define FOR(i, s, e) for (ll i = ll(s); i < ll(e); ++i)
#define REP(i, e) FOR(i, 0, e)
#define INF (INT_MAX / 2)
#define LINF (LLONG_MAX / 2)
#define EPS (1.0e-8)
#define mkpr make_pair
#define AST(exp, msg)                                                          \\
  if (!(exp)) {                                                                \\
    EPR((msg))                                                                 \\
  }                                                                            \\
  assert((exp));
#define TAST(exp, msg)                                                         \\
  if (!(exp)) {                                                                \\
    EPR((msg))                                                                 \\
  }                                                                            \\
  while (!(exp)) {                                                             \\
  }
#define EPR(msg) cerr << (msg) << endl;
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vd = vector<double>;
using vvd = vector<vd>;
using vc = vector<char>;
using vvc = vector<vc>;
using vs = vector<string>;
using pli = pair<long, int>;
using pll = pair<ll, ll>;
using psl = pair<string, ll>;

void get_dist(vi &ds, vvi &es, int s) {
  const int N = es.size();
  queue<int> que;
  que.push(s);
  vb vis(N, false);
  ds = vi(N, INF);
  ds[s] = 0;
  while (!que.empty()) {
    int x = que.front();
    que.pop();
    vis[x] = true;

    for (int nx : es[x]) {
      if (vis[nx])
        continue;
      ds[nx] = min(ds[nx], ds[x] + 1);
      que.push(nx);
    }
  }
}

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

  int N, X, Y;
  cin >> N >> X >> Y;
  X--;
  Y--;

  vvi es(N);
  REP(i, N - 1) {
    es[i].push_back(i + 1);
    es[i + 1].push_back(i);
  }
  es[X].push_back(Y);
  es[Y].push_back(X);

  vi toX, fromY;
  get_dist(toX, es, X);
  get_dist(fromY, es, X);

  vvi dist(N, vi(N, INF));
  map<int, int> ans;
  REP(i, N) {
    FOR(j, i + 1, N) {
      dist[i][j] = min(j - i, toX[i] + fromY[j]);
      dist[j][i] = dist[i][j];
      ans[dist[i][j]] += 1;
    }
  }

  FOR(k, 1, N) { cout << ans[k] << endl; }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <regex>
#define FOR(i, s, e) for (int i = int(s); i < int(e); ++i)
// #define FOR(i, s, e) for (ll i = ll(s); i < ll(e); ++i)
#define REP(i, e) FOR(i, 0, e)
#define INF (INT_MAX / 2)
#define LINF (LLONG_MAX / 2)
#define EPS (1.0e-8)
#define mkpr make_pair
#define AST(exp, msg)                                                          \\
  if (!(exp)) {                                                                \\
    EPR((msg))                                                                 \\
  }                                                                            \\
  assert((exp));
#define TAST(exp, msg)                                                         \\
  if (!(exp)) {                                                                \\
    EPR((msg))                                                                 \\
  }                                                                            \\
  while (!(exp)) {                                                             \\
  }
#define EPR(msg) cerr << (msg) << endl;
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vd = vector<double>;
using vvd = vector<vd>;
using vc = vector<char>;
using vvc = vector<vc>;
using vs = vector<string>;
using pli = pair<long, int>;
using pll = pair<ll, ll>;
using psl = pair<string, ll>;

void get_dist(vi &ds, vvi &es, int s) {
  const int N = es.size();
  queue<int> que;
  que.push(s);
  vb vis(N, false);
  ds = vi(N, INF);
  ds[s] = 0;
  while (!que.empty()) {
    int x = que.front();
    que.pop();
    vis[x] = true;

    for (int nx : es[x]) {
      if (vis[nx])
        continue;
      ds[nx] = min(ds[nx], ds[x] + 1);
      que.push(nx);
    }
  }
}

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

  int N, X, Y;
  cin >> N >> X >> Y;
  X--;
  Y--;

  vvi es(N);
  REP(i, N - 1) {
    es[i].push_back(i + 1);
    es[i + 1].push_back(i);
  }
  es[X].push_back(Y);
  es[Y].push_back(X);

  vi toX, fromY;
  get_dist(toX, es, X);
  get_dist(fromY, es, X);

  vvi dist(N, vi(N, INF));
  REP(i, N) {
    FOR(j, i + 1, N) {
      dist[i][j] = min(j - i, toX[i] + fromY[j]);
      dist[j][i] = dist[i][j];
    }
  }

  FOR(k, 1, N) {
    int ans = 0;
    REP(i, N) {
      FOR(j, i + 1, N) {
        if (dist[i][j] == k)
          ans++;
      }
    }
    cout << ans << endl;
  }

  return 0;
}