Problem p02964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

void pre() {}

int a[400009], ans[400009];
int n, lst[400009], nxt[400009];
bool vis[400009];
void solve(int i) {
  if (vis[i])
    return;
  if (nxt[i] + 1 >= n) {
    ans[i] = (nxt[i] + 1) - n;
    vis[i] = 1;
    return;
  }
  solve(nxt[i] + 1);
  ans[i] = ans[nxt[i] + 1];
}
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  pre();
  cin >> n;
  ll k;
  cin >> k;
  rep(i, n) {
    cin >> a[i];
    a[n + i] = a[i];
  }
  repD(i, 2 * n - 1, n) lst[a[i]] = i;
  repD(i, n - 1, 0) {
    nxt[i] = lst[a[i]];
    lst[a[i]] = i;
  }
  rep(i, n) {
    if (!vis[i])
      solve(i);
  }
  ans[n] = 0;
  vi v;
  v.pb(0);
  memset(lst, -1, sizeof(lst));
  lst[0] = 0;
  int cur = 0;
  while (lst[ans[cur]] == -1) {
    cur = ans[cur];
    lst[cur] = sz(v);
    v.pb(cur);
  }
  cur = ans[cur];
  k--;
  if (k >= sz(v)) {
    k -= lst[cur];
    k %= (sz(v) - lst[cur]);
    k += lst[cur];
  }
  fill(vis);
  vi fns;
  repA(i, v[k], n - 1) {
    if (!vis[a[i]])
      fns.pb(a[i]), vis[a[i]] = 1;
    else {
      while (fns.back() != a[i])
        vis[fns.back()] = 0, fns.pop_back();
      vis[a[i]] = 0;
      fns.pop_back();
    }
  }
  trav(i, fns) cout << i << ' ';
  return 0;
}


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

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

void pre() {}

int a[400009], ans[400009];
int n, lst[400009], nxt[400009];
bool vis[400009];
void solve(int i) {
  if (vis[i])
    return;
  if (nxt[i] + 1 >= n) {
    ans[i] = (nxt[i] + 1) - n;
    vis[i] = 1;
    return;
  }
  solve(nxt[i] + 1);
  ans[i] = ans[nxt[i] + 1];
  vis[i] = 1;
}
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  pre();
  cin >> n;
  ll k;
  cin >> k;
  rep(i, n) {
    cin >> a[i];
    a[n + i] = a[i];
  }
  repD(i, 2 * n - 1, n) lst[a[i]] = i;
  repD(i, n - 1, 0) {
    nxt[i] = lst[a[i]];
    lst[a[i]] = i;
  }
  rep(i, n) {
    if (!vis[i])
      solve(i);
  }
  ans[n] = 0;
  vi v;
  v.pb(0);
  memset(lst, -1, sizeof(lst));
  lst[0] = 0;
  int cur = 0;
  while (lst[ans[cur]] == -1) {
    cur = ans[cur];
    lst[cur] = sz(v);
    v.pb(cur);
  }
  cur = ans[cur];
  k--;
  if (k >= sz(v)) {
    k -= lst[cur];
    k %= (sz(v) - lst[cur]);
    k += lst[cur];
  }
  fill(vis);
  vi fns;
  repA(i, v[k], n - 1) {
    if (!vis[a[i]])
      fns.pb(a[i]), vis[a[i]] = 1;
    else {
      while (fns.back() != a[i])
        vis[fns.back()] = 0, fns.pop_back();
      vis[a[i]] = 0;
      fns.pop_back();
    }
  }
  trav(i, fns) cout << i << ' ';
  return 0;
}