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

#pragma GCC optimize("O3")
#pragma GCC target("sse4.2")
#include <bits/stdc++.h>
#include <x86intrin.h>

#define rep2(x, fr, to) for (int(x) = (fr); (x) < (to); (x)++)
#define rep(x, to) for (int(x) = 0; (x) < (to); (x)++)
#define repr(x, fr, to) for (int(x) = (fr); (x) >= (to); (x)--)
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
const int MD = (int)1e9 + 7;
typedef vector<ll> VL;
void dbg() { cerr << "\\n"; }
template <class T, class... T2> void dbg(const T &fst, const T2 &...rst) {
  cerr << fst << ": ";
  dbg(rst...);
}
template <class T, class T2> void amax(T &a, T2 b) {
  if (a < b)
    a = b;
}

const int NMX = 2048;
int N, res = 0;
__attribute__((aligned(16))) int16_t d[NMX][NMX];

void f1(int16_t d[][NMX]) {
  for (int k = 0; k < N; k++)
    for (int i = 0; i < N; i++) {
      int j = 0;
      const __m128i dik = _mm_set1_epi16(d[i][k]);
      for (; j + 7 < N; j += 8) {
        __m128i dij = _mm_load_si128((__m128i *)(d[i] + j));
        __m128i dkj = _mm_load_si128((__m128i *)(d[k] + j));
        dkj = _mm_add_epi16(dkj, dik);
        dij = _mm_min_epi16(dij, dkj);
        _mm_store_si128((__m128i *)(d[i] + j), dij);
      }

      for (; j < N; j++) {
        d[i][j] = min<int>(d[i][j], d[i][k] + d[k][j]);
      }
    }
}
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  ll n, x, y;
  cin >> n >> x >> y;
  // vector<VI> a(n, VI(n, INF));
  // rep(i, n) a[i][i] = 0;
  // rep(i, n-1) a[i][i+1] = a[i+1][i] =1;
  // a[x-1][y-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]);

  rep(i, n + 2) rep(j, n + 2) d[i][j] = n;
  rep(i, n) d[i][i] = 0;
  rep(i, n - 1) d[i][i + 1] = d[i + 1][i] = 1;
  d[x - 1][y - 1] = d[y - 1][x - 1] = 1;
  N = n;
  f1(d);
  // dbg("dd");
  VI ds(n + 2);
  rep(i, n) rep2(j, i + 1, n) ds[d[i][j]]++;

  // for(auto x :ds) cerr<<x<<" "; dbg();
  rep2(i, 1, n) cout << ds[i] << "\\n";
  return 0;

  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("O3")
#pragma GCC target("avx")
#include <bits/stdc++.h>
#include <emmintrin.h>
#include <smmintrin.h>

#define rep2(x, fr, to) for (int(x) = (fr); (x) < (to); (x)++)
#define rep(x, to) for (int(x) = 0; (x) < (to); (x)++)
#define repr(x, fr, to) for (int(x) = (fr); (x) >= (to); (x)--)
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
const int MD = (int)1e9 + 7;
typedef vector<ll> VL;
void dbg() { cerr << "\\n"; }
template <class T, class... T2> void dbg(const T &fst, const T2 &...rst) {
  cerr << fst << ": ";
  dbg(rst...);
}
template <class T, class T2> void amax(T &a, T2 b) {
  if (a < b)
    a = b;
}

const int NMX = 2048;
int N, res = 0;
__attribute__((aligned(32))) int d[NMX][NMX];

void f1(int d[][NMX]) {
  for (int k = 0; k < N; k++)
    for (int i = 0; i < N; i++) {
      int j = 0;
      const __m128i dik = _mm_set1_epi32(d[i][k]);
      for (; j + 3 < N; j += 4) {
        __m128i dij = _mm_load_si128((__m128i *)(d[i] + j));
        __m128i dkj = _mm_load_si128((__m128i *)(d[k] + j));
        dkj = _mm_add_epi32(dkj, dik);
        dij = _mm_min_epi32(dij, dkj);
        _mm_store_si128((__m128i *)(d[i] + j), dij);
      }

      for (; j < N; j++) {
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
    }
}
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  ll n, x, y;
  cin >> n >> x >> y;
  // vector<VI> a(n, VI(n, INF));
  // rep(i, n) a[i][i] = 0;
  // rep(i, n-1) a[i][i+1] = a[i+1][i] =1;
  // a[x-1][y-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]);

  rep(i, n + 2) rep(j, n + 2) d[i][j] = n;
  rep(i, n) d[i][i] = 0;
  rep(i, n - 1) d[i][i + 1] = d[i + 1][i] = 1;
  d[x - 1][y - 1] = d[y - 1][x - 1] = 1;
  N = n;
  f1(d);
  dbg("dd");
  VI ds(n + 2);
  rep(i, n) rep2(j, i + 1, n) ds[d[i][j]]++;

  // for(auto x :ds) cerr<<x<<" "; dbg();
  rep2(i, 1, n) cout << ds[i] << "\\n";
  return 0;

  return 0;
}