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

#include <algorithm>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

#define LOCAL 1
#define UPLOAD 2

#define ll long long
const long long INF = 1LL << 60;

template <class T = long long> T input() {
  T x;
  cin >> x;
  return x;
}
template <class T> void output(T x) { cout << x << endl; }
#define debug(x) cerr << #x << ": " << x << '\\n';
#define debugBit(x, n) cerr << #x << ": " << std::bitset<n>(x) << '\\n';

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

int main() {
#if ENVIRONMENT == LOCAL
  std::ifstream in("input.txt");
  std::cin.rdbuf(in.rdbuf());
#endif

  ll n = input();
  ll x = input();
  ll y = input();

  vector<vector<ll>> d(n + 100, vector<ll>(n + 100));
  for (ll i = 0; i < n; i++) {
    for (ll j = i + 1; j < n; j++) {
      ll num1 = i + 1;
      ll num2 = j + 1;

      bool b1 = num1 <= x && x <= num2;
      bool b2 = num1 <= y && y <= num2;
      bool b3 = x <= num1 && num2 <= y;

      ll dist1 = abs(x - num1) + abs(num2 - y) + 1;
      ll dist2 = j - i;

      if (b1 && b2) {
        d[i][j] = dist1;
      } else if (b1 || b2 || b3) {
        d[i][j] = min(dist1, dist2);
      } else {
        d[i][j] = dist2;
      }
    }
  }

  map<ll, ll> ans;
  for (ll i = 0; i < n; i++) {
    for (ll j = i; j < n; j++) {
      // cout << i+1 << "," << j+1 << " : " << d[i][j] << endl;
      ans[d[i][j]]++;
    }
  }

  for (ll i = 1; i < n; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

#define LOCAL 1
#define UPLOAD 2

#define ll long long
const long long INF = 1LL << 60;

template <class T = long long> T input() {
  T x;
  cin >> x;
  return x;
}
template <class T> void output(T x) { cout << x << endl; }
#define debug(x) cerr << #x << ": " << x << '\\n';
#define debugBit(x, n) cerr << #x << ": " << std::bitset<n>(x) << '\\n';

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

int main() {
#if ENVIRONMENT == LOCAL
  std::ifstream in("input.txt");
  std::cin.rdbuf(in.rdbuf());
#endif

  ll n = input();
  ll x = input();
  ll y = input();

  vector<vector<ll>> d(n, vector<ll>(n));

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j) {
        d[i][j] = 0;
      } else if (abs(i - j) == 1) {
        d[i][j] = 1;
      } else {
        d[i][j] = INF;
      }
    }
  }

  d[x - 1][y - 1] = 1;
  d[y - 1][x - 1] = 1;

  for (int k = 0; k < n; k++) {     // 経由する頂点
    for (int i = 0; i < n; i++) {   // 始点
      for (int j = 0; j < n; j++) { // 終点
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }

  map<ll, ll> ans;
  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
      ans[d[i][j]]++;
    }
  }

  for (ll i = 1; i < n; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}