Problem p02964 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

int N;
int64_t K;
std::vector<int> A;

void calcAns(int, int64_t);

int main() {
  scanf("%d%lld", &N, &K);
  K *= N;
  A.resize(N);
  for (int &e : A) {
    scanf("%d", &e);
    e--;
  }
  std::vector<int> to(N), dp_table(200'000);
  for (int i{N - 1}; i >= 0; i--)
    dp_table[A[i]] = N + i;
  for (int i{N - 1}; i >= 0; i--) {
    to[i] = dp_table[A[i]] + 1;
    dp_table[A[i]] = i;
  }
  std::vector<int> order(N, -1);
  int begin, loop_size, now_i{};
  for (int i{}; i < N; i++) {
    order[now_i] = i;
    int next{to[now_i] % N};
    if (order[next] >= 0) {
      begin = next;
      loop_size = i + 1 - order[next];
      break;
    }
    now_i = next;
  }
  // ループに入る前の挙動
  now_i = 0;
  int64_t now_dist{};
  while (now_i != begin) {
    int64_t delta{to[now_i] - now_i};
    if (now_dist + delta > K) {
      calcAns(now_i, K - now_dist);
      return 0;
    }
    now_dist += delta;
    now_i = to[now_i] % N;
  }
  // 今now_dist <= K
  // ループの長さ
  int64_t loop_length{to[begin] - begin};
  now_i = to[begin] % N;
  while (now_i != begin) {
    int64_t delta{to[now_i] - now_i};
    loop_length += delta;
    now_i = (now_i + delta) % N;
  }
  // K == (beginからの距離)
  K = (K - now_dist) % loop_length;
  now_i = begin;
  now_dist = 0;
  while (true) {
    int64_t delta{to[now_i] - now_i};
    if (now_dist + delta > K) {
      calcAns(now_i, K - now_dist);
      break;
    }
    now_dist += delta;
    now_i = to[now_i] % N;
  }

  return 0;
}

void calcAns(int begin, int64_t dist) {
  std::vector<int> count(200'000), st;
  for (int64_t i{}; i < dist; i++) {
    int num{A[(begin + i) % N]};
    if (count[num] == 0) {
      st.push_back(num);
      count[num]++;
    } else
      while (count[num] > 0) {
        if (st.back() == num)
          count[num]--;
        st.pop_back();
      }
  }
  for (int i{}; i < (int)st.size(); i++) {
    printf("%d", st[i] + 1);
    if (i + 1 != (int)st.size())
      putchar(' ');
  }
  putchar('\n');
  return;
}

Fixed submission:
#include <bits/stdc++.h>

int N;
int64_t K;
std::vector<int> A;

void calcAns(int, int64_t);

int main() {
  scanf("%d%lld", &N, &K);
  K *= N;
  A.resize(N);
  for (int &e : A) {
    scanf("%d", &e);
    e--;
  }
  std::vector<int> to(N), dp_table(200'000);
  for (int i{N - 1}; i >= 0; i--)
    dp_table[A[i]] = N + i;
  for (int i{N - 1}; i >= 0; i--) {
    to[i] = dp_table[A[i]] + 1;
    dp_table[A[i]] = i;
  }
  std::vector<int> order(N, -1);
  int begin, loop_size, now_i{};
  for (int i{}; i < N; i++) {
    order[now_i] = i;
    int next{to[now_i] % N};
    if (order[next] >= 0) {
      begin = next;
      loop_size = i + 1 - order[next];
      break;
    }
    now_i = next;
  }
  // ループに入る前の挙動
  now_i = 0;
  int64_t now_dist{};
  while (now_i != begin) {
    int64_t delta{to[now_i] - now_i};
    if (now_dist + delta > K) {
      calcAns(now_i, K - now_dist);
      return 0;
    }
    now_dist += delta;
    now_i = to[now_i] % N;
  }
  // 今now_dist <= K
  // ループの長さ
  int64_t loop_length{to[begin] - begin};
  now_i = to[begin] % N;
  while (now_i != begin) {
    int64_t delta{to[now_i] - now_i};
    loop_length += delta;
    now_i = (now_i + delta) % N;
  }
  // K == (beginからの距離)
  K = (K - now_dist) % loop_length;
  now_i = begin;
  now_dist = 0;
  while (true) {
    int64_t delta{to[now_i] - now_i};
    if (now_dist + delta > K) {
      calcAns(now_i, K - now_dist);
      break;
    }
    now_dist += delta;
    now_i = to[now_i] % N;
  }

  return 0;
}

void calcAns(int begin, int64_t dist) {
  std::vector<int> count(200'000), st;
  for (int64_t i{}; i < dist; i++) {
    int num{A[(begin + i) % N]};
    if (count[num] == 0) {
      st.push_back(num);
      count[num]++;
    } else
      while (count[num] > 0) {
        count[st.back()]--;
        st.pop_back();
      }
  }
  for (int i{}; i < (int)st.size(); i++) {
    printf("%d", st[i] + 1);
    if (i + 1 != (int)st.size())
      putchar(' ');
  }
  putchar('\n');
  return;
}