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

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

void solve(long long N, long long X, long long Y) {
  std::vector<std::vector<long long>> dist(N, std::vector<long long>(N));
  std::vector<long long> cnt(N, 0);

  int in_range = 0;
  for (long long i = 0; i < N; i++) {
    for (long long j = 0; j < N; j++) {
      dist[i][j] = abs(j - i);
      // cout << "[" << i << "][" << j << "], " << in_range << " " << dist[i][j]
      // << endl;
      if ((i <= X - 1) && (j >= Y - 1)) {
        dist[i][j] -= (Y - X - 1);
      }
      // cout << "[" << i+1 << "][" << j+1 << "], " << in_range << " " <<
      // dist[i][j] << endl;
    }
  }

  for (long long i = 0; i < N; i++) {
    for (long long j = 0; j < N; j++) {
      if (i < j) {
        if (dist[i][j] > dist[i][X - 1] + dist[X - 1][j]) {
          dist[i][j] = dist[i][X - 1] + dist[X - 1][j];
        }
        if (dist[i][j] > dist[i][Y - 1] + dist[Y - 1][j]) {
          dist[i][j] = dist[i][Y - 1] + dist[Y - 1][j];
        }

        cnt[dist[i][j] - 1]++;
      }

      // if(i < j){
      //     cout << "[" << i+1 << "][" << j+1 << "], " << in_range << " " <<
      //     dist[i][j] << endl;
      // }
    }
  }

  for (long long k = 0; k < N - 1; k++) {
    cout << cnt[k] << endl;
  }
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  long long N;
  scanf("%lld", &N);
  long long X;
  scanf("%lld", &X);
  long long Y;
  scanf("%lld", &Y);
  solve(N, X, Y);
  return 0;
}


Error: TLE

Fixed submission:

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

void solve(long long N, long long X, long long Y) {
  std::vector<std::vector<long long>> dist(N, std::vector<long long>(N));
  int in_range = 0;
  for (long long i = 0; i < N; i++) {
    for (long long j = 0; j < N; j++) {
      in_range = 0;
      dist[i][j] = abs(j - i);
      // cout << "[" << i << "][" << j << "], " << in_range << " " << dist[i][j]
      // << endl;
      if ((i <= X - 1) && (j >= Y - 1)) {
        dist[i][j] -= (Y - X - 1);
        in_range = 1;
      }
      // cout << "[" << i+1 << "][" << j+1 << "], " << in_range << " " <<
      // dist[i][j] << endl;
    }
  }

  for (long long i = 0; i < N; i++) {
    for (long long j = 0; j < N; j++) {
      in_range = 0;
      if (dist[i][j] > dist[i][X - 1] + dist[X - 1][j]) {
        dist[i][j] = dist[i][X - 1] + dist[X - 1][j];
      }
      if (dist[i][j] > dist[i][Y - 1] + dist[Y - 1][j]) {
        dist[i][j] = dist[i][Y - 1] + dist[Y - 1][j];
      }
      // if(i < j){
      //     cout << "[" << i+1 << "][" << j+1 << "], " << in_range << " " <<
      //     dist[i][j] << endl;
      // }
    }
  }

  for (long long k = 1; k < N; k++) {
    long long cnt = 0;
    for (long long i = 0; i < N; i++) {
      for (long long j = 0; j < N; j++) {
        if (i < j) {
          if (dist[i][j] == k) {
            cnt++;
          }
        }
      }
    }
    cout << cnt << endl;
  }
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  long long N;
  scanf("%lld", &N);
  long long X;
  scanf("%lld", &X);
  long long Y;
  scanf("%lld", &Y);
  solve(N, X, Y);
  return 0;
}