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

#include <algorithm>
#include <array>
#include <climits>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

#define INF 1000000007 // 1000000000000000003
#define MOD 1000000007
#define MAX 2000005

#define ll long long

// printf("%.10f", );
// cout << fixed << setprecision(10) << heikin << endl;
// vector<vector<int>> data(3, vector<int>(4, -1));
// left + 1 < top

using namespace std;
using P = pair<int, int>;
using Pc = pair<double, int>;
using T = tuple<int, int, int>;
using edge = struct {
  int to;
  long long dist;
};
const double PI = 3.141592653589793;
long long fact[MAX], inv[MAX];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

//////////////////////////////////

vector<vector<int>> bingo(3, vector<int>(3));
vector<vector<int>> tree(100005);
int color[100005];
map<P, int> colorMap;
int m = 0;

struct BridgeInfo {
  int town1;
  int town2;
  int year;
  BridgeInfo(int t1, int t2, int y) {
    town1 = t1;
    town2 = t2;
    year = y;
  }
};

struct People {
  int index;
  int town;
  int year;
  People(int i, int t, int y) {
    index = i;
    town = t;
    year = y;
  }
};

bool compare_by_second(pair<int, int> a, pair<int, int> b) {
  if (a.second != b.second) {
    return a.second < b.second;
  } else {
    return a.first < b.first;
  }
}

struct UnionFind {
  vector<int> uf;

  UnionFind(int size) : uf(size, -1){};

  int root(int target) {
    if (uf[target] < 0)
      return target;
    else
      return uf[target] = root(uf[target]);
  }

  void merge(int a, int b) {
    a = root(a);
    b = root(b);
    if (a == b)
      return;
    if (uf[b] < uf[a])
      swap(a, b);
    uf[a] += uf[b];
    uf[b] = a;
  }
};

ll CalcPow(ll num, ll power) {
  if (power == 0)
    return 1;
  if (power % 2 == 0) {
    ll half = CalcPow(num, power / 2);
    return (half * half) % MOD;
  } else
    return (num * CalcPow(num, power - 1)) % MOD;
}

ll combination(ll all, ll select) {
  if (all < select)
    return 0;
  if (all < 0 || select < 0)
    return 0;
  return fact[all] * (inv[select] * inv[all - select] % MOD) % MOD;
}

void initCombination(int max) {
  fact[0] = fact[1] = 1;
  for (int i = 2; i <= max; ++i) {
    fact[i] = fact[i - 1] * i % MOD;
  }

  inv[max] = CalcPow(fact[max], MOD - 2);
  for (int i = max - 1; 0 <= i; --i) {
    inv[i] = (inv[i + 1] * (i + 1)) % MOD;
  }
}

int main() {

  int N, X, Y;
  cin >> N >> X >> Y;
  --X;
  --Y;

  vector<vector<int>> data(N, vector<int>(N, INF));
  vector<int> ans(N);
  for (int i = 0; i < N; ++i) {
    for (int j = i + 1; j < N; ++j) {
      int m1 = min(abs(X - i) + 1 + abs(j - Y), abs(Y - i) + 1 + abs(j - X));
      data[i][j] = min(abs(j - i), m1);
      ans[data[i][j]]++;
    }
  }
  for (int i = 1; i < N; ++i) {
    cout << ans[i] << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <climits>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

#define INF 1000000007 // 1000000000000000003
#define MOD 1000000007
#define MAX 2000005

#define ll long long

// printf("%.10f", );
// cout << fixed << setprecision(10) << heikin << endl;
// vector<vector<int>> data(3, vector<int>(4, -1));
// left + 1 < top

using namespace std;
using P = pair<int, int>;
using Pc = pair<double, int>;
using T = tuple<int, int, int>;
using edge = struct {
  int to;
  long long dist;
};
const double PI = 3.141592653589793;
long long fact[MAX], inv[MAX];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

//////////////////////////////////

vector<vector<int>> bingo(3, vector<int>(3));
vector<vector<int>> tree(100005);
int color[100005];
map<P, int> colorMap;
int m = 0;

struct BridgeInfo {
  int town1;
  int town2;
  int year;
  BridgeInfo(int t1, int t2, int y) {
    town1 = t1;
    town2 = t2;
    year = y;
  }
};

struct People {
  int index;
  int town;
  int year;
  People(int i, int t, int y) {
    index = i;
    town = t;
    year = y;
  }
};

bool compare_by_second(pair<int, int> a, pair<int, int> b) {
  if (a.second != b.second) {
    return a.second < b.second;
  } else {
    return a.first < b.first;
  }
}

struct UnionFind {
  vector<int> uf;

  UnionFind(int size) : uf(size, -1){};

  int root(int target) {
    if (uf[target] < 0)
      return target;
    else
      return uf[target] = root(uf[target]);
  }

  void merge(int a, int b) {
    a = root(a);
    b = root(b);
    if (a == b)
      return;
    if (uf[b] < uf[a])
      swap(a, b);
    uf[a] += uf[b];
    uf[b] = a;
  }
};

ll CalcPow(ll num, ll power) {
  if (power == 0)
    return 1;
  if (power % 2 == 0) {
    ll half = CalcPow(num, power / 2);
    return (half * half) % MOD;
  } else
    return (num * CalcPow(num, power - 1)) % MOD;
}

ll combination(ll all, ll select) {
  if (all < select)
    return 0;
  if (all < 0 || select < 0)
    return 0;
  return fact[all] * (inv[select] * inv[all - select] % MOD) % MOD;
}

void initCombination(int max) {
  fact[0] = fact[1] = 1;
  for (int i = 2; i <= max; ++i) {
    fact[i] = fact[i - 1] * i % MOD;
  }

  inv[max] = CalcPow(fact[max], MOD - 2);
  for (int i = max - 1; 0 <= i; --i) {
    inv[i] = (inv[i + 1] * (i + 1)) % MOD;
  }
}

struct Connection {
  vector<int> friends;
  vector<int> block;
  vector<int> friendable;
  Connection(){};
};

int main() {

  int N, X, Y;
  cin >> N >> X >> Y;

  vector<vector<int>> d(N, vector<int>(N, INF));
  for (int i = 0; i < N - 1; ++i) {
    d[i][i + 1] = 1;
    d[i + 1][i] = 1;
  }
  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]);
      }
    }
  }

  vector<int> ans(N - 1, 0);
  for (int i = 0; i < N; ++i) {
    for (int j = i + 1; j < N; ++j) {
      int currentD = d[i][j];
      ans[currentD - 1]++;
    }
  }

  for (int i = 0; i < N - 1; ++i) {
    cout << ans[i] << endl;
  }
}