alexjercan/bugnet huggingface dataset:
Problem p02726 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int graph[2010][2010];

int main() {

  int N, X, Y;
  cin >> N >> X >> Y;

  vector<int> ans(N - 1, 0);
  rep(i, N) {
    rep(j, N) {
      if (j <= i)
        continue;
      int from = i + 1;
      int to = j + 1;
      int num = min(to - from, abs(from - X) + 1 + abs(Y - to));
      ans[num - 1]++;
    }
  }

  rep(i, N - 1) { cout << ans[i] << endl; }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

// 隣接リストで使う辺を表す型
struct Edge {
  int to, cost; // 辺の接続先頂点, 辺の重み
  Edge(int to, int cost) : to(to), cost(cost) {} // コンストラクタ
};

typedef vector<vector<Edge>> AdjList;
AdjList graph;

const int INF = 100000000;

vector<int> dist; // 最短距離

// 戻り値がtrueなら負の閉路を含む
bool bellman_ford(int n, int s) { // nは頂点数、sは開始頂点
  dist = vector<int>(n, INF);
  dist[s] = 0; // 開始点の距離は0
  for (int i = 0; i < n; i++) {
    for (int v = 0; v < n; v++) {
      for (int k = 0; k < graph[v].size(); k++) {
        Edge e = graph[v][k];
        if (dist[v] != INF && dist[e.to] > dist[v] + e.cost) {
          dist[e.to] = dist[v] + e.cost;
          if (i == n - 1)
            return true; // n回目にも更新があるなら負の閉路が存在
        }
      }
    }
  }
  return false;
}

int main() {
  int N, X, Y;
  cin >> N >> X >> Y;

  int V = N;
  int E = N;
  graph = AdjList(N);
  rep(i, N - 1) {
    graph[i].push_back(Edge(i + 1, 1));
    graph[i + 1].push_back(Edge(i, 1));
  }
  graph[X - 1].push_back(Edge(Y - 1, 1));
  graph[Y - 1].push_back(Edge(X - 1, 1));

  vector<int> ans(N - 1, 0);

  for (int i = 0; i < N; i++) {
    bellman_ford(N, i);
    for (int j = 1; j < N; j++) {
      int from = i + 1;
      int to = j + 1;
      if (to <= from)
        continue;

      if (dist[j] != INF)
        ans[dist[j] - 1]++;
    }
  }
  // for (int i = 1; i < N; i++) {
  //   if (dist[i] != INF)
  //     cout << "0から" << i << "へのコスト: " << dist[i] << endl;
  // }
  rep(i, N - 1) { cout << ans[i] << endl; }

  return 0;
}
Problem p02726 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
typedef long long ll;
long long mod = 1e9 + 7;
// const long long INF = 1LL << 60;
int INF = 10000;
using namespace std;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

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

  int ans[5000] = {};
  repi(i, 1, n) {
    repi(j, i + 1, n + 1) {
      int cost = min(j - i, abs(x - i) + 1 + abs(y - j));
      ans[cost]++;
    }
  }

  repi(i, 1, n) { cout << ans[i] << endl; }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
typedef long long ll;
long long mod = 1e9 + 7;
const long long INF = 1LL << 60;
using namespace std;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

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

  vector<vector<int>> g(n + 1, vector<int>(n + 1, 10000));

  for (int i = 0; i < n; i++) {
    g[i][i] = 0;
  }

  repi(i, 1, n) {
    g[i][i + 1] = 1;
    g[i + 1][i] = 1;
  };

  g[x][y] = 1;
  g[y][x] = 1;

  vector<int> ans(3000, 0);

  for (int k = 1; k < n + 1; k++) {
    for (int i = 1; i < n + 1; i++) {
      for (int j = 1; j < n + 1; j++) {
        g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
      }
    }
  }

  for (int i = 1; i < n + 1; i++) {
    for (int j = 1; j < n + 1; j++) {
      if (i != j && g[i][j] != 10000)
        ans[g[i][j]]++;
    }
  };

  cout << ans[1] / 2 << endl;
  for (int i = 2; i < n; i++) {
    cout << ans[i] / 2 << endl;
  }

  return 0;
}
Problem p02726 (C++) — buggy submission, verdict: Time Limit Exceeded

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
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;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
const ll mod = 1e9 + 7;
const int inf = 1 << 30;

int n;
struct edge {
  int to, cost;
};
typedef pair<int, int> P;
const int MAX_V = 2010;
vector<edge> G[MAX_V];
int d[MAX_V];

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + n, inf);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int x, y;
  cin >> n >> x >> y;
  x--;
  y--;
  rep(i, n - 1) {
    G[i].push_back({i + 1, 1});
    G[i + 1].push_back({i, 1});
  }
  G[x].push_back({y, 1});
  G[y].push_back({x, 1});
  vector<int> ans(n);
  rep(i, n) {
    dijkstra(i);
    rep(j, n) { ans[abs(d[i] - d[j])]++; }
  }
  for (int i = 1; i < n; i++) {
    cout << ans[i] / 2 << endl;
  }
}

Error: TLE

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
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;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
const ll mod = 1e9 + 7;
const int inf = 1 << 30;

int n;
struct edge {
  int to, cost;
};
typedef pair<int, int> P;
const int MAX_V = 2010;
vector<edge> G[MAX_V];
int d[MAX_V];

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + n, inf);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int x, y;
  cin >> n >> x >> y;
  x--;
  y--;
  rep(i, n - 1) {
    G[i].push_back({i + 1, 1});
    G[i + 1].push_back({i, 1});
  }
  G[x].push_back({y, 1});
  G[y].push_back({x, 1});
  vector<int> ans(n);
  rep(i, n) {
    dijkstra(i);
    rep(j, n) { ans[abs(d[i] - d[j])]++; }
  }
  for (int i = 1; i < n; i++) {
    cout << ans[i] / 2 << endl;
  }
}
Problem p02726 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for (ll i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

int main() {
  ll n, x, y;
  cin >> n >> x >> y;
  x--, y--;
  vl ans(n, 0);
  rep(i, n - 1) rep2(j, i + 1, n) {
    ll d = j - i;
    d = min(d, abs(i - x) + abs(j - y) + 1);
    ans[d]++;
  }
  rep2(i, 1, n) cout << ans[i] << '\\n';
}

Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for (ll i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

int main() {
  ll n, x, y;
  cin >> n >> x >> y;
  x--, y--;
  vl ans(n, 0);
  rep(i, n - 1) rep2(j, 1, n) {
    ll d = j - i;
    d = min(d, abs(i - x) + abs(j - y) + 1);
    d = min(d, abs(i - y) + abs(j - x) + 1);
    ans[d]++;
  }
  rep2(i, 1, n) cout << ans[i] << '\\n';
}
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 + 1; i++)
#define sort(A) sort(A.begin(), A.end())
#define reverse(A) reverse(A.begin(), A.end());

typedef long long ll;

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

  vector<vector<int>> to(n + 1, vector<int>(0));

  rep1(i, n - 1) {
    to[i].push_back(i + 1);
    to[i + 1].push_back(i);
  }
  to[x].push_back(y);
  to[y].push_back(x);

  vector<int> ans(n, 0);

  rep1(i, n) {

    // こっからBFS
    int INF = 1000000000;
    vector<int> d(n + 1, INF);
    vector<bool> seen(n + 1, false);
    queue<int> q;
    q.push(i);
    d[i] = 0;
    while (!q.empty()) {
      int now = q.front();
      q.pop();
      if (seen[now])
        continue;
      seen[now] = true;
      for (int next : to[now]) {
        d[next] = min(d[now] + 1, d[next]);
        q.push(next);
      }
    } // while

    for (int j = i + 1; j <= n; j++)
      ans[d[j]]++;
  } // i

  rep1(i, n - 1) cout << ans[i] << 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 + 1; i++)
#define sort(A) sort(A.begin(), A.end())
#define reverse(A) reverse(A.begin(), A.end());

typedef long long ll;

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

  vector<vector<int>> to(n + 1, vector<int>(0));

  rep1(i, n - 1) {
    to[i].push_back(i + 1);
    to[i + 1].push_back(i);
  }
  to[x].push_back(y);
  to[y].push_back(x);

  vector<int> ans(n, 0);

  rep1(i, n) {
    for (int j = i + 1; j <= n; j++) {

      // こっからBFS
      int INF = 1000000000;
      vector<int> d(n + 1, INF);
      vector<bool> seen(n + 1, false);
      queue<int> q;
      q.push(i);
      d[i] = 0;
      while (!q.empty()) {
        int now = q.front();
        q.pop();
        if (seen[now])
          continue;
        seen[now] = true;
        for (int next : to[now]) {
          d[next] = min(d[now] + 1, d[next]);
          q.push(next);
        }
      } // while
      ans[d[j]]++;
    } // j
  }   // i

  rep1(i, n - 1) cout << ans[i] << endl;
}
Problem p02726 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
int main() {
  int n, x, y;
  cin >> n >> x >> y;

  vector<int> ans(n + 1, 0);

  for (int i = 1; i <= n; i++) {
    queue<pair<int, int>> q;
    int yet = n;
    q.push(make_pair(i, 0));
    vector<int> dis(n + 1, n);
    while (!q.empty()) {
      pair<int, int> node = q.front();
      q.pop();
      int num = node.first;
      int d = node.second;
      if (dis[num] != yet)
        continue;
      dis[num] = d;

      if (num == x)
        q.push(make_pair(y, d + 1));
      if (num == y)
        q.push(make_pair(x, d + 1));
      if (num + 1 <= n)
        q.push(make_pair(num + 1, d + 1));
      if (num - 1 > 0)
        q.push(make_pair(num - 1, d + 1));
    }
    rep(j, n + 1) ans[dis[j]]++;
  }

  for (int i = 1; i < n; i++)
    cout << ans[i] / 2 << endl;

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

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

  vector<vector<int>> d(n + 1, vector<int>(n + 1, 0));

  vector<int> ans(n + 1);

  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; i++) {
      int a;
      if (i <= x && y <= j)
        a = j - 1 - (x - y);
      else
        a = j - i;
      ans[a]++;
    }
  }

  for (int i = 1; i < n; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}
Problem p02726 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define INF 1e18
#define MAX 100010
using namespace std;

struct edge {
  int to;
  ll cost;
  edge(int t, int c) : to(t), cost(c) {}
};

typedef pair<ll, int> p;
ll dist[MAX];
vector<edge> graph[MAX];

void dijkstra(int r) {
  priority_queue<p, vector<p>, greater<p>> que;
  bool color[MAX];
  for (int i = 0; i < MAX; i++) {
    dist[i] = INF;
    color[i] = false;
  }
  dist[r] = 0;
  que.push(p(0, r));
  while (!que.empty()) {
    p f = que.top();
    que.pop();
    int v = f.second;
    color[v] = true;
    if (dist[v] < f.first)
      continue;
    for (int i = 0; i < graph[v].size(); i++) {
      edge e = graph[v][i];
      if (color[e.to] == true)
        continue;
      if (dist[e.to] > dist[v] + e.cost) {
        dist[e.to] = dist[v] + e.cost;
        que.push(p(dist[e.to], e.to));
      }
    }
  }
}

int main() {
  int n, x, y;
  cin >> n >> x >> y;
  for (int i = 0; i < n; i++) {
    graph[i].push_back(edge(i + 1, 1));
    graph[i + 1].push_back(edge(i, 1));
  }
  graph[x - 1].push_back(edge(y - 1, 1));
  graph[y - 1].push_back(edge(x - 1, 1));
  vector<int> ans(n);
  for (int i = 0; i < n; i++) {
    dijkstra(i);
    for (int j = 0; j < n; j++) {
      if (dist[j] == 0 || dist[j] >= n)
        continue;
      ans[dist[j] - 1]++;
    }
  }
  for (int i = 0; i < n - 1; i++)
    cout << ans[i] / 2 << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define p pair<int, int>
using namespace std;

int main() {
  int n, x, y;
  cin >> n >> x >> y;
  int dist[n + 1][n + 1];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j)
        dist[i][j] = 0;
      else
        dist[i][j] = 1e9;
    }
  }
  for (int i = 0; i < n; i++) {
    dist[i + 1][i] = 1;
    dist[i][i + 1] = 1;
  }
  dist[x - 1][y - 1] = 1;
  dist[y - 1][x - 1] = 1;
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
      }
    }
  }
  vector<int> ans(n);
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      ans[dist[i][j] - 1]++;
    }
  }
  for (int i = 0; i < n - 1; i++)
    cout << ans[i] << endl;
  return 0;
}
Problem p02726 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

const int MAX_N = 2000;
int n, x, y;

void solve() {
  int d[n][n];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i <= j) {
        d[i][j] = min(abs(i - j), abs(i - (x - 1)) + abs(j - (y - 1)) + 1);
      } else {
        d[i][j] = d[j][i];
      }
    }
  }

  int ans[n - 1];
  for (int i = 1; i <= n - 1; i++) {
    ans[i - 1] = 0;
  }
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      ans[d[i][j] - 1]++;
    }
  }

  for (int i = 0; i < n - 1; i++) {
    cout << ans[i] << endl;
  }
}

int main() {
  cin >> n >> x >> y;
  solve();
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const int MAX_N = 2000;
int n, x, y;

void solve() {
  int d[n][n];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i <= j) {
        d[i][j] = min(abs(i - j), abs(i - (x - 1)) + abs(j - (y - 1)) + 1);
      } else {
        d[i][j] = d[j][i];
      }
    }
  }

  int ans[n - 1];
  for (int i = 1; i <= n - 1; i++) {
    ans[i - 1] = 0;
    for (int j = 0; j < n - 1; j++) {
      for (int k = j + 1; k < n; k++) {
        if (d[j][k] == i) {
          ans[i - 1]++;
        }
      }
    }
  }

  for (int i = 0; i < n - 1; i++) {
    cout << ans[i] << endl;
  }
}

int main() {
  cin >> n >> x >> y;
  solve();
}
Problem p02726 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define REP(i, k, n) for (long long i = k; i < (long long)(n); i++)
#define all(a) a.begin(), a.end()
#define pb push_back
typedef long long ll;
typedef pair<ll, ll> P;
using vi = vector<ll>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;

ll n, x, y;

int main() {
  cin >> n >> x >> y;

  vi d(n);
  x--;
  y--;
  rep(i, n) {
    REP(j, i + 1, n) {
      ll dij = abs(j - i);
      ll dxy = abs(x - i) + abs(y - j) + 1;
      ll dis = min(dij, dxy);
      d[dis]++;
    }
  }
  REP(i, 1, n) cout << d[i] << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define REP(i, k, n) for (long long i = k; i < (long long)(n); i++)
#define all(a) a.begin(), a.end()
#define pb push_back
typedef long long ll;
typedef pair<ll, ll> P;
using vi = vector<ll>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;

ll n, x, y;
vi d(600);
void ws() {
  x--;
  y--;
  rep(i, n) {
    REP(j, i + 1, n) {
      ll dij = abs(j - i);
      ll dxy = abs(x - i) + abs(j - y) + 1;
      ll dyx = abs(y - i) + abs(j - x) + 1;

      ll dis = min({dij, dxy, dyx});
      d[dis]++;
    }
  }
}

int main() {
  cin >> n >> x >> y;
  ws();
  REP(i, 1, n) cout << d[i] << endl;
  return 0;
}
Problem p02726 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <iterator>
#include <string>
#define pan(i, n) for (int i = 0; i < n; i++)
#define ll long long int
#define vi vector<int>
#define pb push_back
#define INF 1e9 + 7
#define PI 3.141592653
#define inf 100007
using namespace std;

int main() {
  int n, x, y;
  cin >> n >> x >> y;
  int d[n + 1][n + 1], ans[n + 1];
  memset(ans, 0, sizeof(ans));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      ans[min({abs(j - i), abs(x - i) + 1 + abs(j - y),
               abs(x - j) + 1 + abs(i - y)})]++;
    }
  }
  for (int j = 1; j < n; j++)
    cout << ans[j] / 2 << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iterator>
#include <string>
#define pan(i, n) for (int i = 0; i < n; i++)
#define ll long long int
#define vi vector<int>
#define pb push_back
#define INF 1e9 + 7
#define PI 3.141592653
#define inf 99999
using namespace std;

int main() {
  int n, x, y;
  cin >> n >> x >> y;
  int d[n][n];
  pan(i, n) pan(j, n) {
    if (i == j)
      d[i][j] = 0;
    else if (abs(j - i) == 1)
      d[i][j] = 1;
    else if ((i == x - 1 && j == y - 1) || (i == y - 1 && j == x - 1))
      d[i][j] = 1;
    else
      d[i][j] = inf;
  }
  // pan(i,n){
  //     pan(j,n)
  //         cout<<d[i][j]<<" ";
  //     cout<<endl;
  // }
  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]);
      }
    }
  }
  // pan(i,n){
  //     pan(j,n)
  //         cout<<d[i][j];
  //     cout<<endl;
  // }
  vi ans;
  for (int i = 1; i < n; i++) {
    int c = 0;
    pan(j, n) pan(k, n) if (d[j][k] == i) c++;
    ans.pb(c / 2);
  }
  pan(i, (int)ans.size()) cout << ans[i] << endl;
}
Problem p02726 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define FOR(i, a, n) for (int i = (a); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define FORD(i, a, n) for (int i = (a); i >= (n); i--)
#define REPD(i, n) FOR(i, n, 0)

#define ALL(o) (o).begin(), (o).end()

template <class T> using V = vector<T>;
template <class T, class U> using P = pair<T, U>;
template <class T> using PQ = priority_queue<T>;
template <class T> using PQR = priority_queue<T, vector<T>, greater<T>>;

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

  int a[n][n];
  fill(a[0], a[n], 0);

  // 0
  REP(i, n) {
    if (i < y) {
      a[0][i] = min(i, (x + y - i) + 1);
    } else {
      a[0][i] = x + (i - y) + 1;
    }
  }
  // 1 <= i < x+1
  FOR(i, 1, x + 1) {
    FOR(j, i, n) { a[i][j] = a[i - 1][j] - 1; }
  }
  // x+1 <= i < y
  FOR(i, x + 1, y) {
    FOR(j, i, n) { a[i][j] = min(j - i, (i - x) + abs(y - j) + 1); }
  }
  // y <= i < n
  FOR(i, y, n) {
    FOR(j, i, n) { a[i][j] = j - i; }
  }

  int cnt[n];
  fill(cnt, cnt + n, 0);

  REP(i, n - 1) {
    FOR(j, i + 1, n) { cnt[a[i][j]]++; }
  }
  REP(i, n - 1) { cout << cnt[i + 1] << endl; }
  // REP(i, n) {
  // 	REP(j,n) {
  // 		printf("%2d ", a[i][j]);
  // 	}
  // 	cout << endl;
  // }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define FOR(i, a, n) for (int i = (a); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define FORD(i, a, n) for (int i = (a); i >= (n); i--)
#define REPD(i, n) FOR(i, n, 0)

#define ALL(o) (o).begin(), (o).end()

template <class T> using V = vector<T>;
template <class T, class U> using P = pair<T, U>;
template <class T> using PQ = priority_queue<T>;
template <class T> using PQR = priority_queue<T, vector<T>, greater<T>>;

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

  int a[n][n];
  fill(a[0], a[n], 2000);

  REP(i, n - 1) {
    a[i][i + 1] = 1;
    a[i + 1][i] = 1;
  }
  a[x - 1][y - 1] = 1;
  a[y - 1][x - 1] = 1;

  REP(k, n) {
    REP(i, n) {
      REP(j, n) { a[i][j] = min(a[i][j], a[i][k] + a[k][j]); }
    }
  }
  int cnt[2001];
  REP(i, n - 1) {
    FOR(j, i + 1, n) { cnt[a[i][j]]++; }
  }
  REP(i, n - 1) { cout << cnt[i + 1] << endl; }

  return 0;
}
Problem p02726 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, x, y;
  cin >> n >> x >> y;
  vector<int> cnt(n);
  for (int i = 1; i <= n - 1; i++) {
    for (int j = i + 1; j <= n; j++) {
      cnt[min({abs(j - i), abs(x - i) + 1 + abs(y - j),
               abs(y - i) + 1 + abs(x - j)})] += 1;
    }
  }
  for (int k = 1; k < n; k++) {
    cout << cnt[k] << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

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

  for (int i = 1; i <= n - 1; i++) {
    int cnt = 0;
    for (int j = 1; j <= n - 1; j++) {
      for (int k = j + 1; k <= n; k++) {
        if (min({abs(k - j), abs(x - j) + 1 + abs(y - k),
                 abs(y - j) + 1 + abs(x - k)}) == i) {
          cnt += 1;
        }
      }
    }
    cout << cnt << endl;
  }
}
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 INF 1e9
typedef long long ll;

int main() {
  int N, X, Y;
  cin >> N >> X >> Y;
  vector<int> c(N + 1);
  for (int i = 1; i <= N; i++) {
    for (int j = i + 1; j <= N; j++) {
      int m = min(abs(i - j), abs(X - i) + 1 + abs(Y - j));
      c[m]++;
    }
  }

  for (int i = 1; i < N; i++) {
    cout << c[i] << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define INF 1e9
typedef long long ll;

int main() {
  int N, X, Y;
  cin >> N >> X >> Y;
  vector<vector<int>> G(N, vector<int>(N, INF));
  rep(i, N) { G[i][i] = 0; }
  rep(i, N - 1) {
    G[i][i + 1] = 1;
    G[i + 1][i] = 1;
  }
  G[X - 1][Y - 1] = 1;
  G[Y - 1][X - 1] = 1;

  vector<int> ans(N);

  for (int i = 0; i < N; i++) {
    vector<int> d(N, INF);
    d[i] = 0;
    queue<int> que;
    que.push(i);
    while (!que.empty()) {
      int p = que.front();
      que.pop();
      for (int j = 0; j < N; j++) {
        if (j == p)
          continue;
        if (G[p][j] + d[p] < d[j]) {
          d[j] = d[p] + 1;
          que.push(j);
        }
      }
    }
    rep(i, N) { ans[d[i]]++; }
  }
  for (int i = 1; i < N; i++) {
    cout << ans[i] / 2 << endl;
  }
}