Problem p02964 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n; i >= 0; i--)
#define REP(i, s, t) for (int i = s; i <= t; i++)
#define RREP(i, s, t) for (int i = s; i >= t; i--)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000
#define int long long

int nextd[64][200010];
int A[400010];
vector<int> v[200010];
signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, K;
  cin >> N >> K;

  rep(i, N) cin >> A[i];
  rep(i, N) A[N + i] = A[i];
  rep(i, N * 2) { v[A[i]].push_back(i); }
  rep(i, 60) rep(j, 200010) nextd[i][j] = INF2;
  // rep(i, N) {
  //     cout << i + 1 << ": ";
  //     rep(j, v[i + 1].size()) {
  //         cout << v[i + 1][j] << " ";
  //     }
  //     cout << endl;
  // }
  rep(i, N) {
    auto head = lower_bound(v[A[i]].begin(), v[A[i]].end(), i);
    auto nxt = head;
    ++nxt;
    // cout << *head << " " << *nxt << endl;
    nextd[0][i] = *nxt - *head + 1;
  }
  rep(i, 59) {
    rep(j, N) {
      if (nextd[i][j] != INF2) {
        int nxt = j;
        nxt += nextd[i][nxt];
        nxt += nextd[i][nxt % N];
        if (nxt - j <= N * K)
          nextd[i + 1][j] = nxt - j;
      }
    }
  }

  int now = 0;
  while (true) {
    int nxt = now;
    rep(i, 60) {
      if (now + nextd[i][now % N] <= N * K) {
        nxt = now + nextd[i][now % N];
      } else {
        break;
      }
    }
    if (nxt == now)
      break;
    now = nxt;
    // cout << now << " " << now % N << " "<< N * K - now << endl;
  }
  int s = now % N;
  int c = N * K - now;
  int cnt[200010];
  vector<int> v2;
  rep(i, c) {
    if (cnt[A[(s + i) % N]] == 0) {
      cnt[A[(s + i) % N]]++;
      v2.push_back(A[(s + i) % N]);
    } else {
      while (cnt[A[(s + i) % N]] > 0) {
        cnt[v2[v2.size() - 1]]--;
        v2.pop_back();
      }
    }
  }
  rep(i, v2.size()) { cout << v2[i] << " "; }
  cout << endl;

  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n; i >= 0; i--)
#define REP(i, s, t) for (int i = s; i <= t; i++)
#define RREP(i, s, t) for (int i = s; i >= t; i--)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000
#define int long long

int nextd[64][200010];
int A[400010];
vector<int> v[200010];
signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, K;
  cin >> N >> K;

  rep(i, N) cin >> A[i];
  rep(i, N) A[N + i] = A[i];
  rep(i, N * 2) { v[A[i]].push_back(i); }
  rep(i, 60) rep(j, 200010) nextd[i][j] = INF2;
  // rep(i, N) {
  //     cout << i + 1 << ": ";
  //     rep(j, v[i + 1].size()) {
  //         cout << v[i + 1][j] << " ";
  //     }
  //     cout << endl;
  // }
  rep(i, N) {
    auto head = lower_bound(v[A[i]].begin(), v[A[i]].end(), i);
    auto nxt = head;
    ++nxt;
    // cout << *head << " " << *nxt << endl;
    nextd[0][i] = *nxt - *head + 1;
  }
  rep(i, 59) {
    rep(j, N) {
      if (nextd[i][j] != INF2) {
        int nxt = j;
        nxt += nextd[i][nxt];
        nxt += nextd[i][nxt % N];
        if (nxt - j <= N * K)
          nextd[i + 1][j] = nxt - j;
      }
    }
  }

  int now = 0;
  while (true) {
    int nxt = now;
    rep(i, 60) {
      if (now + nextd[i][now % N] <= N * K) {
        nxt = now + nextd[i][now % N];
      } else {
        break;
      }
    }
    if (nxt == now)
      break;
    now = nxt;
    // cout << now << " " << now % N << " "<< N * K - now << endl;
  }
  int s = now % N;
  int c = N * K - now;
  int cnt[200010] = {};
  vector<int> v2;
  rep(i, c) {
    if (cnt[A[(s + i) % N]] == 0) {
      cnt[A[(s + i) % N]]++;
      v2.push_back(A[(s + i) % N]);
    } else {
      while (cnt[A[(s + i) % N]] > 0) {
        cnt[v2[v2.size() - 1]]--;
        v2.pop_back();
      }
    }
  }
  rep(i, v2.size()) { cout << v2[i] << " "; }
  cout << endl;

  return 0;
}