Problem p02964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
#ifdef __LOCAL
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << (x) << '\n'
#define debugArray(x, n)                                                       \
  cerr << __LINE__ << ": " << #x << " = {";                                    \
  for (long long hoge = 0; (hoge) < (long long)(n); ++(hoge))                  \
    cerr << ((hoge) ? "," : "") << x[hoge];                                    \
  cerr << "}" << '\n'
#define debugMatrix(x, h, w)                                                   \
  cerr << __LINE__ << ": " << #x << " =\n";                                    \
  for (long long hoge = 0; (hoge) < (long long)(h); ++(hoge)) {                \
    cerr << ((hoge ? " {" : "{{"));                                            \
    for (long long fuga = 0; (fuga) < (long long)(w); ++(fuga))                \
      cerr << ((fuga ? ", " : "")) << x[hoge][fuga];                           \
    cerr << "}" << (hoge + 1 == (long long)(h) ? "}" : ",") << '\n';           \
  }
#else
#define debug(x) (void(0))
#define debugArray(x, n) (void(0))
#define debugMatrix(x, h, w) (void(0))
#endif

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  long long N, K;
  cin >> N >> K;
  long long Y = N * K;
  int A[N];
  for (int i = 0; i < N; i++)
    cin >> A[i];
  int next[N], cost[N];
  const int M = 212345;
  int pos[M];
  fill(pos, pos + M, -1);
  for (int i = N * 2 - 1; i >= 0; i--) {
    if (i < N) {
      next[i] = (pos[A[i]] + 1) % N;
      cost[i] = pos[A[i]] - i + 1;
    }
    pos[A[i % N]] = i;
  }
  debugArray(next, N);
  debugArray(cost, N);
  long long dist[N];
  fill(dist, dist + N, -1);
  long long sum = 0;
  int cur = 0, prev = 0;
  while (true) {
    if (dist[cur] != -1) {
      break;
    }
    dist[cur] = sum;
    if (dist[cur] > Y) {
      break;
    }
    sum += cost[cur];
    prev = cur;
    cur = next[cur];
  }
  if (Y < dist[cur]) {
    cur = prev;
    Y -= dist[prev];
  } else {
    Y -= dist[cur];
    Y %= sum - dist[cur];
  }
  vector<int> s;
  bool exist[M];
  fill(exist, exist + M, false);
  while (Y--) {
    if (exist[A[cur]]) {
      while (s.back() != A[cur])
        exist[s.back()] = false, s.pop_back();
      s.pop_back();
      exist[A[cur]] = false;
    } else {
      s.push_back(A[cur]);
      exist[A[cur]] = true;
    }
    cur = (cur + 1) % N;
  }
  for (size_t i = 0; i < s.size(); i++)
    cout << s[i] << (i == s.size() - 1 ? '\n' : ' ');
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
#ifdef __LOCAL
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << (x) << '\n'
#define debugArray(x, n)                                                       \
  cerr << __LINE__ << ": " << #x << " = {";                                    \
  for (long long hoge = 0; (hoge) < (long long)(n); ++(hoge))                  \
    cerr << ((hoge) ? "," : "") << x[hoge];                                    \
  cerr << "}" << '\n'
#define debugMatrix(x, h, w)                                                   \
  cerr << __LINE__ << ": " << #x << " =\n";                                    \
  for (long long hoge = 0; (hoge) < (long long)(h); ++(hoge)) {                \
    cerr << ((hoge ? " {" : "{{"));                                            \
    for (long long fuga = 0; (fuga) < (long long)(w); ++(fuga))                \
      cerr << ((fuga ? ", " : "")) << x[hoge][fuga];                           \
    cerr << "}" << (hoge + 1 == (long long)(h) ? "}" : ",") << '\n';           \
  }
#else
#define debug(x) (void(0))
#define debugArray(x, n) (void(0))
#define debugMatrix(x, h, w) (void(0))
#endif

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  long long N, K;
  cin >> N >> K;
  long long Y = N * K;
  int A[N];
  for (int i = 0; i < N; i++)
    cin >> A[i];
  int next[N], cost[N];
  const int M = 212345;
  int pos[M];
  fill(pos, pos + M, -1);
  for (int i = N * 2 - 1; i >= 0; i--) {
    if (i < N) {
      next[i] = (pos[A[i]] + 1) % N;
      cost[i] = pos[A[i]] - i + 1;
    }
    pos[A[i % N]] = i;
  }
  debugArray(next, N);
  debugArray(cost, N);
  long long dist[N];
  fill(dist, dist + N, -1);
  long long sum = 0;
  int cur = 0, prev = 0;
  while (true) {
    if (dist[cur] != -1) {
      break;
    }
    dist[cur] = sum;
    if (dist[cur] > Y) {
      break;
    }
    sum += cost[cur];
    prev = cur;
    cur = next[cur];
  }
  if (Y < dist[cur]) {
    cur = prev;
    Y -= dist[prev];
  } else {
    Y -= dist[cur];
    Y %= sum - dist[cur];
  }
  fill(dist, dist + N, -1);
  sum = 0;
  while (true) {
    if (dist[cur] != -1) {
      break;
    }
    dist[cur] = sum;
    if (dist[cur] > Y) {
      break;
    }
    sum += cost[cur];
    prev = cur;
    cur = next[cur];
  }
  if (Y < dist[cur]) {
    cur = prev;
    Y -= dist[prev];
  }
  vector<int> s;
  bool exist[M];
  fill(exist, exist + M, false);
  while (Y--) {
    if (exist[A[cur]]) {
      while (s.back() != A[cur])
        exist[s.back()] = false, s.pop_back();
      s.pop_back();
      exist[A[cur]] = false;
    } else {
      s.push_back(A[cur]);
      exist[A[cur]] = true;
    }
    cur = (cur + 1) % N;
  }
  for (size_t i = 0; i < s.size(); i++)
    cout << s[i] << (i == s.size() - 1 ? '\n' : ' ');
  return 0;
}