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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
// #define pi acos(-1)
// #include<bits/stdc++.h>
typedef long long ll;
#define INF 0x7f7f7f7f  // 2139062143
#define INF1 0x3f3f3f3f // 1061109567
#define INF2 2147483647
#define llINF 9223372036854775807
#define pi 3.14159265358979323846264338327950254
#define pb push_back
#define ll long long
#define debug cout << "debug\\n";
#define STDIN                                                                  \\
  freopen("in.txt", "r", stdin);                                               \\
  freopen("out.txt", "w", stdout);
#define Quick                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);
#define scai(x) scanf("%d", &x)
#define sca2i(x, y) scanf("%d %d", &x, &y)
#define scaf(x) scanf("%lf", &x)
#define sca2f(x, y) scanf("%lf %lf", &x, &y)
#define For(m, n) for (int i = m; i < n; i++)
#define PII pair<int, int>
#define PLL pair<long, long>
#define lowbit(x) (x & -x)

const int maxn = 50;
#define ERR 1e-6
#define fabs(a) (a < 0 ? -(a) : a)
#define square(a) ((a) * (a))
const int N = 2e3 + 10;
int n;
int ans[N];
int d[N][N];
void floyd() {
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}

int main() {
  // STDIN
  int x, y;
  cin >> n >> x >> y;
  for (int i = 1; i < n; i++) {
    for (int j = i + 1; j <= n; j++) {
      int dist = min(min(abs(j - i), abs(x - i) + 1 + abs(y - j)),
                     abs(y - i) + 1 + abs(x - j));
      ans[dist]++;
    }
  }
  for (int i = 1; i < n; i++) {
    printf("%d\\n", ans[i]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
// #define pi acos(-1)
// #include<bits/stdc++.h>
typedef long long ll;
#define INF 0x7f7f7f7f  // 2139062143
#define INF1 0x3f3f3f3f // 1061109567
#define INF2 2147483647
#define llINF 9223372036854775807
#define pi 3.14159265358979323846264338327950254
#define pb push_back
#define ll long long
#define debug cout << "debug\\n";
#define STDIN                                                                  \\
  freopen("in.txt", "r", stdin);                                               \\
  freopen("out.txt", "w", stdout);
#define Quick                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);
#define scai(x) scanf("%d", &x)
#define sca2i(x, y) scanf("%d %d", &x, &y)
#define scaf(x) scanf("%lf", &x)
#define sca2f(x, y) scanf("%lf %lf", &x, &y)
#define For(m, n) for (int i = m; i < n; i++)
#define PII pair<int, int>
#define PLL pair<long, long>
#define lowbit(x) (x & -x)

const int maxn = 50;
#define ERR 1e-6
#define fabs(a) (a < 0 ? -(a) : a)
#define square(a) ((a) * (a))
const int N = 2e3 + 10;
int n;
int ans[N];
int d[N][N];
void floyd() {
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}

int main() {
  // STDIN
  int x, y;
  cin >> n >> x >> y;

  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (i == j)
        d[i][j] = 0;
      else
        d[i][j] = INF1;
  for (int i = 1; i < n; i++) {
    d[i][i + 1] = d[i + 1][i] = 1;
  }
  d[x][y] = d[y][x] = 1;
  floyd();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (d[i][j] != INF1) {
        ans[d[i][j]]++;
      }
    }
  }
  for (int i = 1; i < n; i++) {
    printf("%d\\n", ans[i] / 2);
  }
  return 0;
}