Problem p02964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ul;
typedef signed long long ll;

mt19937 mt(time(nullptr));
// uniform_int_distribution< int > rand_int(0, 10) // rand[0, 10] inclusive

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed;
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (ll i = 0; i < n; ++i)
    cin >> a[i];
  unordered_map<ll, ll> near;
  vector<ll> cstartidx(n + 1, n);
  // cstartidx[n-1] = n-1;
  // near[a[n-1]] = n-1;
  for (ll i = n - 1; i >= 0; --i) {
    if (near.count(a[i]) > 0) {
      cstartidx[i] = cstartidx[near[a[i]] + 1];
    } else
      cstartidx[i] = i;
    // cerr << cstartidx[i] << " ";
    near[a[i]] = i;
  }
  // cerr << endl;
  vector<ll> nstartidx(n);
  for (ll i = 0; i < n; ++i) {
    if (cstartidx[i] == n)
      nstartidx[i] = n;
    else
      nstartidx[i] = (cstartidx[near[a[cstartidx[i]]] + 1]);
    // cerr << nstartidx[i] << " ";
  }
  // cerr << endl;
  ul start = 0;
  vector<ll> v(n + 1, -1);
  bool l_flg = false;
  for (ll i = 0; i < k - 1; ++i) {
    if (start == n) {
      start = 0;
      continue;
    }
    v[start] = i;
    /*
    if (!l_flg)
      cerr << i << " " << start << endl;
      */
    start = nstartidx[start];
    if (!l_flg) {
      if (v[start] >= 0) {
        l_flg = true;
        k -= (k - 2 - i) / (i + 1 - v[start]) * (i + 1 - v[start]);
        // cerr << start << ":" << v[start] << " " << i << endl;
      }
    }
  }
  vector<ll> s;
  unordered_map<ll, ll> cnt;
  for (ll i = start; i < n; ++i) {
    if (cnt.count(a[i]) == 0 || cnt[a[i]] == 0) {
      s.push_back(a[i]);
      cnt[a[i]] = 1;
    } else {
      while (cnt[a[i]] > 0) {
        cnt[s[s.size() - 1]]--;
        s.pop_back();
      }
    }
  }
  if (s.size() == 0)
    return 0;
  for (ll i = 0; i < s.size(); ++i)
    cout << s[i] << " ";
  cout << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ul;
typedef signed long long ll;

mt19937 mt(time(nullptr));
// uniform_int_distribution< int > rand_int(0, 10) // rand[0, 10] inclusive

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed;
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (ll i = 0; i < n; ++i)
    cin >> a[i];
  unordered_map<ll, ll> near;
  vector<ll> cstartidx(n + 1, n);
  // cstartidx[n-1] = n-1;
  // near[a[n-1]] = n-1;
  for (ll i = n - 1; i >= 0; --i) {
    if (near.count(a[i]) > 0) {
      cstartidx[i] = cstartidx[near[a[i]] + 1];
    } else
      cstartidx[i] = i;
    // cerr << cstartidx[i] << " ";
    near[a[i]] = i;
  }
  // cerr << endl;
  vector<ll> nstartidx(n);
  for (ll i = 0; i < n; ++i) {
    if (cstartidx[i] == n)
      nstartidx[i] = n;
    else
      nstartidx[i] = (cstartidx[near[a[cstartidx[i]]] + 1]);
    // cerr << nstartidx[i] << " ";
  }
  // cerr << endl;
  ul start = 0;
  vector<ll> v(n + 1, -1);
  bool l_flg = false;
  for (ll i = 0; i < k - 1; ++i) {
    if (start == n) {
      k -= (k - 2 - i) / (i + 1) * (i + 1);
      start = 0;
      continue;
    }
    v[start] = i;
    /*
    if (!l_flg)
      cerr << i << " " << start << endl;
      */
    start = nstartidx[start];
    if (!l_flg) {
      if (v[start] >= 0) {
        l_flg = true;
        k -= (k - 2 - i) / (i + 1 - v[start]) * (i + 1 - v[start]);
        // cerr << start << ":" << v[start] << " " << i << endl;
      }
    }
  }
  vector<ll> s;
  unordered_map<ll, ll> cnt;
  for (ll i = start; i < n; ++i) {
    if (cnt.count(a[i]) == 0 || cnt[a[i]] == 0) {
      s.push_back(a[i]);
      cnt[a[i]] = 1;
    } else {
      while (cnt[a[i]] > 0) {
        cnt[s[s.size() - 1]]--;
        s.pop_back();
      }
    }
  }
  if (s.size() == 0)
    return 0;
  for (ll i = 0; i < s.size(); ++i)
    cout << s[i] << " ";
  cout << endl;
  return 0;
}