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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define repr1(i, n) for (int i = n; i >= 1; i--)
#define all(v) v.begin(), v.end()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int INF = 1e9;
const ll LLINF = 1e18;
const ll MOD = 1e9 + 7;
const double EPS = 1e-10;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int n, x, y;
ll dist[2010][2010];
ll cnt[2010];

int main() {
  cin >> n >> x >> y;
  x--;
  y--;
  vector<vector<int>> g(n);
  rep(i, n) {
    if (i > 0)
      g[i - 1].push_back(i);
    if (i < n - 1)
      g[i + 1].push_back(i);
  }
  g[x].push_back(y);
  g[y].push_back(x);

  rep(i, n) rep(j, n) dist[i][j] = LLINF;
  rep(i, n) dist[i][i] = 0;
  rep(i, n) {
    priority_queue<pli, vector<pli>, greater<pli>> q;
    q.emplace(0, i);
    while (!q.empty()) {
      pli top = q.top();
      q.pop();
      int u = top.second;
      for (auto v : g[u]) {
        if (chmin(dist[i][v], dist[i][u] + 1)) {
          q.emplace(dist[i][v], v);
        }
      }
    }
  }

  rep(i, n) rep(j, n) {
    if (i >= j)
      continue;
    cnt[dist[i][j]]++;
  }
  for (int k = 1; k <= n - 1; k++) {
    cout << cnt[k] << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define repr1(i, n) for (int i = n; i >= 1; i--)
#define all(v) v.begin(), v.end()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int INF = 1e9;
const ll LLINF = 1e18;
const ll MOD = 1e9 + 7;
const double EPS = 1e-10;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int n, x, y;

int main() {
  cin >> n >> x >> y;
  x--;
  y--;
  vector<vector<int>> g(n);
  rep(i, n) {
    if (i > 0)
      g[i - 1].push_back(i);
    if (i < n - 1)
      g[i + 1].push_back(i);
  }
  g[x].push_back(y);
  g[y].push_back(x);

  for (int k = 1; k <= n - 1; k++) {
    int cnt = 0;
    rep(i, n) {
      vector<ll> dist(n);
      rep(j, n) dist[j] = LLINF;
      dist[i] = 0;

      priority_queue<pli, vector<pli>, greater<pli>> q;
      q.emplace(0, i);
      while (!q.empty()) {
        pli top = q.top();
        q.pop();
        int u = top.second;
        for (auto v : g[u]) {
          if (chmin(dist[v], dist[u] + 1)) {
            q.emplace(dist[v], v);
          }
        }
      }
      // rep(j, n) cout << dist[j] << " ";
      // cout << endl;
      rep(j, n) {
        if (dist[j] == k)
          cnt++;
      }
    }
    cout << cnt / 2 << endl;
  }
}