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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define REP(i, n) FOR(i, 0, n - 1)
#define NREP(i, n) FOR(i, 1, n)
using ll = long long;
using pii = pair<int, int>;
using piii = pair<pii, pii>;
const ll dx[4] = {0, -1, 1, 0};
const ll dy[4] = {-1, 0, 0, 1};
const int INF = 1e9 + 7;
int gcd(int x, int y) {
  if (x < y)
    swap(x, y);
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
void mul(ll a, ll b) { a = a * b % INF; }
using Graph = vector<vector<int>>;
ll mypow(ll x, ll n, ll m) {
  if (n == 0) {
    return 1;
  }
  if (n % 2 == 0) {
    return mypow(x * x % m, n / 2, m);
  } else {
    return x * mypow(x, n - 1, m) % m;
  }
}
long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}
const int MAX = 510000;
ll fac[MAX], finv[MAX], inv[MAX];
// テーブルをつくる前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1; // mod pにおける1,2,,,nの逆元
  for (int i = 2; i < MAX; ++i) {
    fac[i] = fac[i - 1] * i % INF;
    inv[i] = INF - inv[INF % i] * (INF / i) % INF;
    finv[i] = finv[i - 1] * inv[i] % INF;
  }
}

ll COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % INF) % INF;
}
///////////////////////////////////////
int ans[2010];
int d[2010];
int main() {
  int N, X, Y;
  cin >> N >> X >> Y;
  X--, Y--;
  vector<int> G[2010];
  for (int i = 0; i < N - 1; ++i) {
    G[i].push_back(i + 1);
    G[i + 1].push_back(i);
  }
  G[X].push_back(Y);
  G[Y].push_back(X);
  REP(i, 2010) ans[i] = 0;
  REP(i, N) {
    REP(j, N) { d[j] = INF; }
    d[i] = 0;
    priority_queue<pii, vector<pii>, greater<pii>> que;
    que.push({0, i});
    while (!que.empty()) {
      pii p = que.top();
      que.pop();
      int v = p.second;
      if (d[v] < p.first)
        continue;
      for (int j = 0; j < G[v].size(); ++j) {
        if (d[G[v][j]] > d[v] + 1) {
          d[G[v][j]] = d[v] + 1;
          que.push({d[G[v][j]], G[v][j]});
        }
      }
    }
    for (int j = 0; j < N; ++j) {
      if (i == j)
        continue;
      ans[d[j]]++;
    }
  }
  for (int i = 1; i < N; ++i) {
    cout << ans[i] / 2 << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define REP(i, n) FOR(i, 0, n - 1)
#define NREP(i, n) FOR(i, 1, n)
using ll = long long;
using pii = pair<int, int>;
using piii = pair<pii, pii>;
const ll dx[4] = {0, -1, 1, 0};
const ll dy[4] = {-1, 0, 0, 1};
const int INF = 1e9 + 7;
int gcd(int x, int y) {
  if (x < y)
    swap(x, y);
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
void mul(ll a, ll b) { a = a * b % INF; }
using Graph = vector<vector<int>>;
ll mypow(ll x, ll n, ll m) {
  if (n == 0) {
    return 1;
  }
  if (n % 2 == 0) {
    return mypow(x * x % m, n / 2, m);
  } else {
    return x * mypow(x, n - 1, m) % m;
  }
}
long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}
const int MAX = 510000;
ll fac[MAX], finv[MAX], inv[MAX];
// テーブルをつくる前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1; // mod pにおける1,2,,,nの逆元
  for (int i = 2; i < MAX; ++i) {
    fac[i] = fac[i - 1] * i % INF;
    inv[i] = INF - inv[INF % i] * (INF / i) % INF;
    finv[i] = finv[i - 1] * inv[i] % INF;
  }
}

ll COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % INF) % INF;
}
///////////////////////////////////////

int d[2010][2010];
int main() {
  int N, X, Y;
  cin >> N >> X >> Y;
  X--, Y--;
  REP(i, 2010) {
    REP(j, 2010) { d[i][j] = INF; }
  }
  for (int i = 0; i < N - 1; ++i) {
    d[i][i] = 0;
    d[i][i + 1] = 1;
    d[i + 1][i] = 1;
  }
  d[N - 1][N - 1] = 0;
  d[X][Y] = 1;
  d[Y][X] = 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]);
      }
    }
  }
  int ans[2010];
  REP(i, 2010) ans[i] = 0;
  for (int i = 0; i < N - 1; ++i) {
    for (int j = i + 1; j < N; ++j) {
      ans[d[i][j]]++;
    }
  }
  for (int i = 1; i < N; ++i) {
    cout << ans[i] << endl;
  }
  return 0;
}