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

#pragma GCC optimize("-O3")
/*
                                C++
                                encoding: UTF-8
                                Last Modified: 28/Mar/20 06:16:57 PM
*/

#include <bits/stdc++.h>
#ifdef H4wk3ye
#include <prettypr.hpp>
#endif

using namespace std;

// clang-format off

#define		int                    long long
#define		ve                     vector
#define		pb                     push_back
#define		endl                   "\\n"
#define		ff                     first
#define		ss                     second
#define		pii                    pair<int, int>
#define		len(v)                 (int)v.size()
#define		all(v)                 v.begin(), v.end()
#define		reset(a, b)            memset(a, b, sizeof(a));
#define		fr(i, s, n)            for (int i = s ; i < n ; ++i)
#define		dfr(i, s, n)           for (int i = s ; i > n ; --i)

template < typename T > void pr (const T& t) {
    cout << t << "\\n";
}

template < typename T, typename U, typename... ARGS >
void pr (const T& t, const U& u, const ARGS&... args) {
    cout << t << " ";
    pr (u, args...);
}

// clang-format on

signed main() {
  ios_base::sync_with_stdio(false), cin.tie(nullptr);
  int n, x, y;
  cin >> n >> x >> y;
  map<int, int> mp;
  fr(i, 1, x + 1) {
    fr(j, i + 1, x + 1) { mp[j - i]++; }
    fr(j, x + 1, n + 1) {
      int ans = min(j - i, x - i + 1 + abs(y - j));
      mp[ans]++;
    }
  }

  fr(i, x + 1, y + 1) {
    fr(j, i + 1, n + 1) {
      int ans = min(j - i, i - x + 1 + abs(j - y));
      mp[ans]++;
    }
  }

  fr(i, y + 1, n + 1) {
    fr(j, i + 1, n + 1) { mp[j - i]++; }
  }

  fr(i, 1, n) pr(mp[i]);

  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize("-O3")
/*
                                C++
                                encoding: UTF-8
                                Last Modified: 28/Mar/20 06:02:46 PM
*/

#include <bits/stdc++.h>
#ifdef H4wk3ye
#include <prettypr.hpp>
#endif

using namespace std;

// clang-format off

#define		int                    long long
#define		ve                     vector
#define		pb                     push_back
#define		endl                   "\\n"
#define		ff                     first
#define		ss                     second
#define		pii                    pair<int, int>
#define		len(v)                 (int)v.size()
#define		all(v)                 v.begin(), v.end()
#define		reset(a, b)            memset(a, b, sizeof(a));
#define		fr(i, s, n)            for (int i = s ; i < n ; ++i)
#define		dfr(i, s, n)           for (int i = s ; i > n ; --i)

template < typename T > void pr (const T& t) {
    cout << t << "\\n";
}

template < typename T, typename U, typename... ARGS >
void pr (const T& t, const U& u, const ARGS&... args) {
    cout << t << " ";
    pr (u, args...);
}

// clang-format on

const int N = 2e3 + 5;
int n, x, y;
ve<int> graph[N];
int vis[N], dis[N];
unordered_map<int, int> mp;
set<pii> s;

void bfs(int node) {
  fr(i, 1, n + 1) {
    vis[i] = 0;
    dis[i] = 1e9;
  }
  dis[node] = 0;
  queue<int> q;
  q.push(node);
  vis[node] = 1;
  while (!q.empty()) {
    int node = q.front();
    q.pop();
    for (auto &to : graph[node]) {
      if (vis[to])
        continue;
      dis[to] = 1 + dis[node];
      vis[to] = 1;
      q.push(to);
    }
  }
  fr(i, 1, n + 1) {
    int u = min(i, node);
    int v = max(i, node);
    if (s.find({u, v}) != s.end())
      continue;
    s.insert({u, v});
    mp[dis[i]]++;
  }
}

signed main() {
  ios_base::sync_with_stdio(false), cin.tie(nullptr);
  cin >> n >> x >> y;
  fr(i, 1, n) {
    graph[i].pb(i + 1);
    graph[i + 1].pb(i);
  }
  graph[x].pb(y);
  graph[y].pb(x);

  fr(i, 1, n + 1) { bfs(i); }

  fr(i, 1, n) { pr(mp[i]); }
  return 0;
}