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

/**
 * Includes
 */
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

/**
 * Macros
 */
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define repx(i, s, e, d) for (int i = s, i##_end = (e); i < i##_end; i += d)
#define repxr(i, s, e, d) for (int i = s, i##_end = (e); i > i##_end; i += d)
#define rept(n)                                                                \\
  for (int ___i___ = 0, i##_len = (n); ___i___ < i##_len; ++___i___)
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define ZERO(a) memset(a, 0, sizeof(a))
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define CEIL(x, y) (((x) + ((x) > 0 ? (y)-1 : -((y)-1))) / (y))

/**
 * Standard input helpers
 */
// vector:input
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}
// pair:output
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &pair_var) {
  os << "(" << pair_var.first << ", " << pair_var.second << ")";
  return os;
}
// vector:output
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "{";
  for (int i = 0; i < vec.size(); i++) {
    os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
  }
  os << "}";
  return os;
}
// map:output
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &map_var) {
  os << "{";
  repi(itr, map_var) {
    os << *itr;
    itr++;
    if (itr != map_var.end())
      os << ", ";
    itr--;
  }
  os << "}";
  return os;
}
// set:output
template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) {
  os << "{";
  repi(itr, set_var) {
    os << *itr;
    itr++;
    if (itr != set_var.end())
      os << ", ";
    itr--;
  }
  os << "}";
  return os;
}
/**
 * Debug tools
 */
#define DUMPOUT cout
void dump_func() { DUMPOUT << endl; }
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&...tail) {
  DUMPOUT << head;
  if (sizeof...(Tail) > 0) {
    DUMPOUT << ", ";
  }
  dump_func(std::move(tail)...);
}
#ifdef LOCAL_
#define dump(...)                                                              \\
  DUMPOUT << "  " << string(#__VA_ARGS__) << ": "                              \\
          << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl  \\
          << "    ",                                                           \\
      dump_func(__VA_ARGS__)
#else
#define dump(...)
#endif

/**
 * Helpers
 */
typedef long long int lli;
typedef long double ld;
typedef pair<int, int> ii;
template <typename T> using heapq = priority_queue<T, vector<T>, greater<T>>;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
struct aaa {
  aaa() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(20);
  };
} aaaaaaa;

/**
 * Libraries
 */
/**
 * If necessary
 */
// #define int long long int
// struct S{
//     int x
//     bool operator<(const S& s) const {
//         return x < s.x;
//     }
// };
/**
 * Solver
 */
int dist(int i, int j, int X, int Y) { // i < j && X < Y
  if (j <= X) {
    return j - i;
  }
  if (Y <= i) {
    return j - i;
  }
  if (X <= i && j <= Y) {
    return min(j - i, i - X + 1 + Y - j);
  }
  if (i <= X && Y <= j) {
    return X - i + 1 + j - Y;
  }
  if (i <= X && j <= Y) {
    return X - i + min(j - X, Y - j + 1);
  }
  return min(i - X + 1 + j - Y, j - i);
}

signed main() {
  int N, X, Y;
  cin >> N >> X >> Y;
  X--;
  Y--;

  vector<int> ans(N, 0);
  rep(i, N) {
    repx(j, i + 1, N, 1) {
      int d = dist(i, j, X, Y);
      if (d == 2)
        dump(i, j, d);
      ans[d]++;
    }
  }

  rep(i, N - 1) { cout << ans[i + 1] << endl; }
  return 0;
}


Error: TLE

Fixed submission:

/**
 * Includes
 */
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

/**
 * Macros
 */
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define repx(i, s, e, d) for (int i = s, i##_end = (e); i < i##_end; i += d)
#define repxr(i, s, e, d) for (int i = s, i##_end = (e); i > i##_end; i += d)
#define rept(n)                                                                \\
  for (int ___i___ = 0, i##_len = (n); ___i___ < i##_len; ++___i___)
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define ZERO(a) memset(a, 0, sizeof(a))
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define CEIL(x, y) (((x) + ((x) > 0 ? (y)-1 : -((y)-1))) / (y))

/**
 * Standard input helpers
 */
// vector:input
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}
// pair:output
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &pair_var) {
  os << "(" << pair_var.first << ", " << pair_var.second << ")";
  return os;
}
// vector:output
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "{";
  for (int i = 0; i < vec.size(); i++) {
    os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
  }
  os << "}";
  return os;
}
// map:output
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &map_var) {
  os << "{";
  repi(itr, map_var) {
    os << *itr;
    itr++;
    if (itr != map_var.end())
      os << ", ";
    itr--;
  }
  os << "}";
  return os;
}
// set:output
template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) {
  os << "{";
  repi(itr, set_var) {
    os << *itr;
    itr++;
    if (itr != set_var.end())
      os << ", ";
    itr--;
  }
  os << "}";
  return os;
}
/**
 * Debug tools
 */
#define DUMPOUT cout
void dump_func() { DUMPOUT << endl; }
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&...tail) {
  DUMPOUT << head;
  if (sizeof...(Tail) > 0) {
    DUMPOUT << ", ";
  }
  dump_func(std::move(tail)...);
}
#ifdef LOCAL_
#define dump(...)                                                              \\
  DUMPOUT << "  " << string(#__VA_ARGS__) << ": "                              \\
          << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl  \\
          << "    ",                                                           \\
      dump_func(__VA_ARGS__)
#else
#define dump(...)
#endif

/**
 * Helpers
 */
typedef long long int lli;
typedef long double ld;
typedef pair<int, int> ii;
template <typename T> using heapq = priority_queue<T, vector<T>, greater<T>>;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
struct aaa {
  aaa() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(20);
  };
} aaaaaaa;

/**
 * Libraries
 */
struct Edge {
  int from, to, cost;
};
const int INF = 1e9;
vector<int> bellman_ford(vector<Edge> edges, int V, int s) {
  vector<int> dist(V);
  int E = edges.size();
  for (int i = 0; i < V; i++)
    dist[i] = INF;
  dist[s] = 0;
  while (true) {
    bool update = false;
    for (int i = 0; i < E; i++) {
      Edge e = edges[i];
      if (dist[e.from] != INF && dist[e.to] > dist[e.from] + e.cost) {
        dist[e.to] = dist[e.from] + e.cost;
        update = true;
      }
    }

    if (!update)
      break;
  }
  return dist;
}
/**
 * If necessary
 */
// #define int long long int
// struct S{
//     int x
//     bool operator<(const S& s) const {
//         return x < s.x;
//     }
// };
/**
 * Solver
 */
signed main() {
  int N, X, Y;
  cin >> N >> X >> Y;
  X--;
  Y--;
  vector<Edge> edges(2 * N);
  rep(i, N - 1) {
    edges[i].from = i;
    edges[i].to = i + 1;
    edges[i].cost = 1;

    edges[N + i].to = i;
    edges[N + i].from = i + 1;
    edges[N + i].cost = 1;
  }
  edges[N - 1].from = X;
  edges[N - 1].to = Y;
  edges[N - 1].cost = 1;
  edges[2 * N - 1].to = X;
  edges[2 * N - 1].from = Y;
  edges[2 * N - 1].cost = 1;

  vector<int> ans(N, 0);
  rep(i, N) {
    vector<int> dists = bellman_ford(edges, N, i);
    dump(dists);
    repx(j, i + 1, N, 1) { ans[dists[j]]++; }
  }

  rep(i, N - 1) { cout << ans[i + 1] << endl; }
  return 0;
}