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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
// #include <tuple>

#define INF INT_MAX >> 1
#define SIZE 100100
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, from, to) for (int i = (from); i < (int)(to); i++)
#define all(obj) (obj).begin(), (obj).end()
#define lpair pair<ll, ll>
#define vint vector<ll>
#define vinput(name)                                                           \\
  for (int i = 0; i < (int)(name.size()); i++)                                 \\
    cin >> name[i];
#define voutput(name)                                                          \\
  for (int i = 0; i < (int)(name.size()); i++)                                 \\
    cout << name[i] << endl;
typedef long long ll;

using namespace std;

int n, x, y;
vector<int> ans;
void bfs(int s) {
  queue<pair<int, int>> que; //(ノード, コスト)
  que.push(make_pair(s, 0));
  vector<int> dp(n, INF);
  while (!que.empty()) {
    pair<int, int> p = que.front();
    que.pop();
    int pos = p.first;
    int cost = p.second;
    if (pos < 0 || n <= pos)
      continue;
    if (dp[pos] <= cost)
      continue;
    dp[pos] = cost;
    que.push(make_pair(pos - 1, cost + 1));
    que.push(make_pair(pos + 1, cost + 1));
    if (pos == x)
      que.push(make_pair(y, cost + 1));
    if (pos == y)
      que.push(make_pair(x, cost + 1));
  }
  rep(i, n) {
    if (s < i) {
      ans[dp[i] - 1]++;
    }
  }
}

int main(void) {
  cin >> n >> x >> y;
  x--;
  y--;
  ans.resize(n);
  rep(i, n - 1) { bfs(i); }
  rep(i, n - 1) { cout << ans[i] << endl; }

  return 0;
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
// #include <tuple>

#define INF INT_MAX >> 1
#define SIZE 100100
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, from, to) for (int i = (from); i < (int)(to); i++)
#define all(obj) (obj).begin(), (obj).end()
#define lpair pair<ll, ll>
#define vint vector<ll>
#define vinput(name)                                                           \\
  for (int i = 0; i < (int)(name.size()); i++)                                 \\
    cin >> name[i];
#define voutput(name)                                                          \\
  for (int i = 0; i < (int)(name.size()); i++)                                 \\
    cout << name[i] << endl;
typedef long long ll;

using namespace std;

int n, x, y;
int calc(int s, int g) {
  queue<pair<int, int>> que;
  que.push(make_pair(s, -1));
  while (!que.empty()) {
    int pos = que.front().first;
    int dist = que.front().second;
    que.pop();
    if (pos == g)
      return dist;
    if (pos == x) {
      que.push(make_pair(y, dist + 1));
    } else if (pos == y) {
      que.push(make_pair(x, dist + 1));
    }
    if (pos != 0 && pos > min(g, min(x, y)))
      que.push(make_pair(pos - 1, dist + 1));
    if (pos != n - 1 && pos < max(g, max(x, y)))
      que.push(make_pair(pos + 1, dist + 1));
  }
}
/*
int calc(int s, int g){
        int dist = -1;
        int pos = s;
        while(1){
                if(pos == g)break;
                if(pos == x && y <= g){
                        pos = y;
                } else{
                        pos++;
                }
                dist++;
        }
        return dist;
}
*/

int main(void) {
  ll ans[SIZE] = {0};
  cin >> n >> x >> y;
  x--;
  y--;
  rep(i, n - 1) {
    rep2(j, i + 1, n) { ans[calc(i, j)]++; }
  }
  rep(i, n - 1) { cout << ans[i] << endl; }

  return 0;
}