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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>

using namespace std;
#define repp(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define reppe(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define repm(i, a, b) for (int i = (int)(a); i > (int)(b); --i)
#define repme(i, a, b) for (int i = (int)(a); i >= (int)(b); --i)

typedef long long ll;

long long GCD(long long a, long long b) {
  if (b == 0)
    return a;
  return GCD(b, a % b);
}

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

  vector<vector<ll>> d(N);
  repp(i, 0, N) {
    vector<ll> v(N, 1000000000);
    d[i] = v;
  }

  repp(i, 0, N - 1) {
    G[i].push_back(i + 1);
    G[i + 1].push_back(i);
    // d[i+1][i] = d[i][i+1] = 1;
  }
  G[X - 1].push_back(Y - 1);
  G[Y - 1].push_back(X - 1);
  // d[X-1][Y-1] = d[Y-1][X-1] = 1;
  --X, --Y;
  repp(i, 0, N) {
    repp(j, i + 1, N) {
      if (i <= X) {
        if (j <= X) {
          d[i][j] = d[j][i] = j - i;
        } else if (j <= Y) {
          d[i][j] = d[j][i] = min(j - i, X - i + 1 + Y - j);
        } else {
          d[i][j] = d[j][i] = X - i + 1 + j - Y;
        }
      } else if (i <= Y) {
        if (j <= Y) {
          d[i][j] = d[j][i] = min(j - i, i - X + 1 + Y - j);
        } else {
          d[i][j] = d[j][i] = min(j - i, i - X + 1 + j - Y);
        }
      } else {
        d[i][j] = d[j][i] = j - i;
      }
    }
  }
  map<long long, long long> res;

  repp(i, 0, N) {
    repp(j, i + 1, N) { ++res[d[j][i]]; }
  }
  repp(i, 1, N) cout << res[i] << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>

using namespace std;
#define repp(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define reppe(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define repm(i, a, b) for (int i = (int)(a); i > (int)(b); --i)
#define repme(i, a, b) for (int i = (int)(a); i >= (int)(b); --i)

typedef long long ll;

long long GCD(long long a, long long b) {
  if (b == 0)
    return a;
  return GCD(b, a % b);
}

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

  vector<vector<ll>> d(N);
  repp(i, 0, N) {
    vector<ll> v(N, 1000000000);
    d[i] = v;
  }

  repp(i, 0, N - 1) {
    G[i].push_back(i + 1);
    G[i + 1].push_back(i);
    // d[i+1][i] = d[i][i+1] = 1;
  }
  G[X - 1].push_back(Y - 1);
  G[Y - 1].push_back(X - 1);
  // d[X-1][Y-1] = d[Y-1][X-1] = 1;
  --X, --Y;
  repp(i, 0, N) {
    repp(j, i + 1, N) {
      if (i <= X) {
        if (j <= X) {
          d[i][j] = d[j][i] = j - i;
        } else if (j <= Y) {
          d[i][j] = d[j][i] = min(j - i, X - i + 1 + Y - j);
        } else {
          d[i][j] = d[j][i] = X - i + 1 + j - Y;
        }
      } else if (i <= Y) {
        if (j <= Y) {
          d[i][j] = d[j][i] = min(j - i, i - X + 1 + Y - j);
        } else {
          d[i][j] = d[j][i] = min(j - i, i - X + 1 + j - Y);
        }
      } else {
        d[i][j] = d[j][i] = j - i;
      }
    }
  }

  repp(k, 1, N) {
    ll res = 0;
    repp(i, 0, N) {
      repp(j, i + 1, N) {
        if (d[i][j] == k)
          ++res;
      }
    }
    cout << res << endl;
  }
}