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

#include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using namespace std;

const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

struct combination {
  vector<mint> fact, ifact;
  combination(int n) : fact(n + 1), ifact(n + 1) {
    fact[0] = 1;
    for (int i = 1; i <= n; ++i)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i)
      ifact[i - 1] = ifact[i] * i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
} comb(2000005);

mint g(int r, int c) { return comb(r + c + 2, r + 1) - 1; }

struct edge {
  int to;
};

int check[2010][2010];
int n, x, y;
queue<pair<int, int>> p;
long ans[2010];

void cc(int i) {
  int dis = 0;
  p.push(make_pair(i, 0));
  while (p.size() > 0) {
    // 1つめ位置, 2つめ距離
    pair<int, int> current = p.front();
    p.pop();

    if (current.first > n) {
      continue;
    }
    if (current.first <= 0) {
      continue;
    }
    if (check[i][current.first] != -1 &&
        check[i][current.first] < current.second) {
      continue;
    }

    check[i][current.first] = current.second;

    if (current.first == x) {
      p.push(make_pair(y, current.second + 1));
    } else if (current.first == y) {
      p.push(make_pair(x, current.second + 1));
    }
    p.push(make_pair(current.first + 1, current.second + 1));
    p.push(make_pair(current.first - 1, current.second + 1));
  }
}

int main() {
  cin >> n >> x >> y;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      check[i][j] = -1;
    }
  }

  for (int i = 1; i <= n; i++) {
    cc(i);
  }

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      long a = check[i][j];
      if (a == -1) {
        continue;
      }
      ans[a]++;
    }
  }
  for (int k = 1; k < n; k++) {
    cout << ans[k] / 2 << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using namespace std;

const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

struct combination {
  vector<mint> fact, ifact;
  combination(int n) : fact(n + 1), ifact(n + 1) {
    fact[0] = 1;
    for (int i = 1; i <= n; ++i)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i)
      ifact[i - 1] = ifact[i] * i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
} comb(2000005);

mint g(int r, int c) { return comb(r + c + 2, r + 1) - 1; }

struct edge {
  int to;
};

int check[2010][2010];
int n, x, y;
queue<pair<int, int>> p;

void cc(int i) {
  int dis = 0;
  p.push(make_pair(i, 0));
  while (p.size() > 0) {
    // 1つめ位置, 2つめ距離
    pair<int, int> current = p.front();
    p.pop();

    if (current.first > n) {
      continue;
    }
    if (current.first <= 0) {
      continue;
    }
    if (check[i][current.first] != -1 &&
        check[i][current.first] < current.second) {
      continue;
    }

    check[i][current.first] = current.second;

    if (current.first == x) {
      p.push(make_pair(y, current.second + 1));
    } else if (current.first == y) {
      p.push(make_pair(x, current.second + 1));
    }
    p.push(make_pair(current.first + 1, current.second + 1));
    p.push(make_pair(current.first - 1, current.second + 1));
  }
}

int main() {
  cin >> n >> x >> y;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      check[i][j] = -1;
    }
  }

  for (int i = 1; i <= n; i++) {
    cc(i);
  }

  for (int k = 1; k < n; k++) {
    long sum = 0;
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        if (check[i][j] == k) {
          sum++;
        }
      }
    }
    cout << (sum / 2) << endl;
    sum = 0;
  }
}