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

#include <bits/stdc++.h>
using namespace std;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define int long long
#define ll long long
#define ld long double
#define endl '\\n'
#define vi vector<int>
#define vvi vector<vi>
#define vs vector<string>
#define vl vector<long long>
#define vpii vector<pii>
#define vpipii vector<pipii>
#define vb vector<bool>
#define pb push_back
#define pob pop_back
#define gcd __gcd
#define mp make_pair
#define pii pair<int, int>
#define pipii pair<int, pii>
#define pll pair<long long, long long>
#define pld pair<long double, long double>
#define mod 1000000007
#define mod2 998244353
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repp(i, a, b) for (ll i = a; i < b; i++)
#define reppr(i, a, b) for (ll i = a - 1; i >= b; i--)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define ff first
#define ss second
#define pc putchar_unlocked
#define gc getchar_unlocked
#define inf 9223372036854775807
#define infn -9223372036854775807
#define pi 3.14159265358979323846
#define eps 0.0000000001
#define sp << " " <<
#define len(s) s.size()
#define setprecision0 cout << fixed << setprecision(0);
#define setprecision10 cout << fixed << setprecision(10);
#define all(n) n.begin(), n.end()
#define SORT(v) sort(all(v))

void solve();
signed main() {
  fast;

  int t = 1;
  // cin >> t;
  while (t--)
    solve();
}

void solve() {
  // int k , n ;
  // cin >> k >> n ;
  // int mx = 0;
  // int a[n];
  // rep(i,n)
  // {
  //     cin >> a[i];
  // }
  // rep(i,n)
  // {
  //     if(i!=n-1)
  //         mx = max(mx,a[i+1]-a[i]);
  //     else
  //         mx = max(mx,a[0] + k - a[n-1]);
  // }
  // cout << k - mx;
  int n, x, y;
  cin >> n >> x >> y;
  int dist[n] = {};
  repp(i, 1, n + 1) {
    repp(j, i + 1, n + 1) {
      if ((i > x) || (j < y) || (i >= x && j <= y && !(i == x && j == y))) {
        int lol = abs(i - x) + 1 + abs(j - y);
        dist[min(j - i, lol)]++;
      } else
        dist[j - i + x - y + 1]++;
    }
  }
  repp(i, 1, n) cout << dist[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>> graph(n);
  graph[0].push_back(1);
  graph[n - 1].push_back(n - 2);
  for (int i = 1; i < n - 1; i++) {
    graph[i].push_back(i + 1);
    graph[i].push_back(i - 1);
  }
  graph[x - 1].push_back(y - 1);
  graph[y - 1].push_back(x - 1);
  int INF = 1e9 + 7;
  vector<vector<int>> dist(n, vector<int>(n, INF));
  vector<bool> vis(n, false);
  for (int i = 0; i < n; i++) {
    vis = vector<bool>(n, false);
    vis[i] = true;
    dist[i][i] = 0;
    queue<int> q;
    q.push(i);
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      vis[u] = true;
      for (auto it = graph[u].begin(); it != graph[u].end(); it++) {
        if (vis[*it])
          continue;
        dist[i][*it] = min(dist[i][*it], 1 + dist[i][u]);
        dist[*it][i] = min(dist[*it][i], 1 + dist[u][i]);
        q.push(*it);
      }
    }
  }
  // for(int i = 0; i < n; i++)
  // {
  // 	for(int j = 0; j < n; j++)
  // 		cout << dist[i][j] << " ";
  // 	cout << endl;
  // }
  // cout << n << endl;
  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 (k == dist[i][j])
          count++;
      }
    }
    cout << count << endl;
  }

  return 0;
}