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

#include <algorithm>
#include <iostream>
#include <list>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

#define rep(i, a, b) for (ll i = a; i < b; i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef unordered_map<string, ll> u_map;

// input
void input() {}
template <class Head, class... Tail> void input(Head &&a, Tail &&...b) {
  cin >> a;
  input(forward<Tail>(b)...);
}

template <class V> void input(vector<V> &v) {
  for_each(v.begin(), v.end(), [](auto &i) { input(i); });
}

// output
template <class V> void output(vector<V> &v) {
  for_each(v.begin(), v.end(), [](auto &x) { cout << x << " "; });
  cout << endl;
}

// math functions
ll mpow(ll a, ll n, ll p) {
  ll x = 1;
  while (n > 0) {
    if (n & 1) {
      x = a * x % p;
    }
    a = a * a % p;
    n >>= 1;
  }

  return x;
}

ll mfact(ll n, ll p) {
  ll res = 1;
  for (ll i = 2; i <= n; i++) {
    res = res * i % p;
  }
  return res;
}

ll mcomb(ll n, ll r, ll p) {
  if (n == 0 && r == 0) {
    return 1;
  }
  if (n < r || n < 0 || r < 0) {
    return 0;
  }

  ll a = mfact(n, p);
  ll b = mfact(n - r, p);
  ll c = mfact(r, p);
  return (((a * mpow(b, p - 2, p)) % p) * mpow(c, p - 2, p)) % p;
}

ll gcd(ll a, ll b) {
  if (a < b) {
    a ^= b;
    b ^= a;
    a ^= b;
  }
  return b ? gcd(b, a % b) : a;
}

ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

int main() {
  int N, X, Y;
  input(N, X, Y);

  int res[N + 1];
  rep(i, 0, N + 1) { res[i] = 0; }

  int dist[N + 1][N + 1];
  rep(i, 0, N + 1) {
    rep(j, 0, N + 1) { dist[i][j] = -1; }
  }

  rep(i, 1, N) {
    rep(j, i + 1, N + 1) {
      dist[i][j] = min(abs(j - i), abs(X - i) + abs(Y - j) + 1);
      res[dist[i][j]]++;
    }
  }

  rep(k, 1, N) { cout << res[k] << endl; }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <list>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

#define rep(i, a, b) for (ll i = a; i < b; i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef unordered_map<string, ll> u_map;

// input
void input() {}
template <class Head, class... Tail> void input(Head &&a, Tail &&...b) {
  cin >> a;
  input(forward<Tail>(b)...);
}

template <class V> void input(vector<V> &v) {
  for_each(v.begin(), v.end(), [](auto &i) { input(i); });
}

// output
template <class V> void output(vector<V> &v) {
  for_each(v.begin(), v.end(), [](auto &x) { cout << x << " "; });
  cout << endl;
}

// math functions
ll mpow(ll a, ll n, ll p) {
  ll x = 1;
  while (n > 0) {
    if (n & 1) {
      x = a * x % p;
    }
    a = a * a % p;
    n >>= 1;
  }

  return x;
}

ll mfact(ll n, ll p) {
  ll res = 1;
  for (ll i = 2; i <= n; i++) {
    res = res * i % p;
  }
  return res;
}

ll mcomb(ll n, ll r, ll p) {
  if (n == 0 && r == 0) {
    return 1;
  }
  if (n < r || n < 0 || r < 0) {
    return 0;
  }

  ll a = mfact(n, p);
  ll b = mfact(n - r, p);
  ll c = mfact(r, p);
  return (((a * mpow(b, p - 2, p)) % p) * mpow(c, p - 2, p)) % p;
}

ll gcd(ll a, ll b) {
  if (a < b) {
    a ^= b;
    b ^= a;
    a ^= b;
  }
  return b ? gcd(b, a % b) : a;
}

ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

int main() {
  const ll INF = 1e9;
  int N, X, Y;
  input(N, X, Y);

  int dist[N + 1][N + 1];
  rep(i, 0, N + 1) {
    rep(j, 0, N + 1) { dist[i][j] = -1; }
  }

  rep(i, 1, N) {
    rep(j, i + 1, N + 1) {
      dist[i][j] = min(abs(j - i), abs(X - i) + abs(Y - j) + 1);
    }
  }

  rep(k, 1, N) {
    int res = 0;
    rep(i, 1, N) {
      rep(j, i + 1, N + 1) {
        if (k == dist[i][j]) {
          res++;
        }
      }
    }

    cout << res << endl;
  }

  return 0;
}