Problem p02964 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

// #define DEBUG_ //!!提出時にコメントアウト!!
#ifdef DEBUG_
#define dump(x) cerr << #x << " = " << (x) << endl;
#else
#define dump(x) ;
#endif

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back

// #define int long long

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

template <typename T> std::string printVector(const std::vector<T> &data) {
  std::stringstream ss;
  std::ostream_iterator<T> out_it(ss, ", ");
  ss << "[";
  std::copy(data.begin(), data.end() - 1, out_it);
  ss << data.back() << "]";
  return ss.str();
}

const int MOD = 1e9 + 7;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;

LL nex[60][212345] = {};

signed main(int argc, char const *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  LL K;
  cin >> K;
  VL A(N);
  map<int, VI> idx;
  REP(i, N) {
    cin >> A[i];
    idx[A[i]].eb(i);
  }
  for (auto kv : idx) {
    int s = kv.second.size();
    kv.second.eb(kv.second[0] + N);
    REP(i, s) { nex[0][kv.second[i]] = kv.second[i + 1] - kv.second[i] + 1; }
  }

  for (int k = 1; k < 60; k++) {
    for (int i = 0; i < N; i++) {
      nex[k][i] = nex[k - 1][i] + nex[k - 1][(i + nex[k - 1][i]) % N];
    }
  }
  // REP(k,3){
  //     dump(k)
  //     REP(i,N){
  //         cout << nex[k][i] <<" ";
  //     }
  //     cout << endl;
  // }
  LL dest = N * K;
  LL cur = 0;

  for (int i = 59; i >= 0; i--) {
    if (cur + nex[i][cur % N] <= dest) {
      cur += nex[i][cur % N];
    }
  }

  if (cur == dest) {
    cout << endl;
    return 0;
  } else {
    VI ans;
    set<int> has;
    for (int i = cur % N; i < N; i++) {
      if (has.count(A[i])) {
        while (1) {
          int v = ans.back();
          ans.pop_back();
          has.erase(v);
          if (v == A[i])
            break;
        }
      } else {
        ans.eb(A[i]);
        has.insert(A[i]);
      }
    }
    for (int x : ans) {
      cout << x << " ";
    }
    cout << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

// #define DEBUG_ //!!提出時にコメントアウト!!
#ifdef DEBUG_
#define dump(x) cerr << #x << " = " << (x) << endl;
#else
#define dump(x) ;
#endif

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back

// #define int long long

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

template <typename T> std::string printVector(const std::vector<T> &data) {
  std::stringstream ss;
  std::ostream_iterator<T> out_it(ss, ", ");
  ss << "[";
  std::copy(data.begin(), data.end() - 1, out_it);
  ss << data.back() << "]";
  return ss.str();
}

const int MOD = 1e9 + 7;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;

LL nex[60][212345] = {};

signed main(int argc, char const *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  LL K;
  cin >> K;
  VL A(N);
  map<int, VI> idx;
  REP(i, N) {
    cin >> A[i];
    idx[A[i]].eb(i);
  }
  for (auto kv : idx) {
    int s = kv.second.size();
    kv.second.eb(kv.second[0] + N);
    REP(i, s) { nex[0][kv.second[i]] = kv.second[i + 1] - kv.second[i] + 1; }
  }

  for (int k = 1; k < 60; k++) {
    for (int i = 0; i < N; i++) {
      nex[k][i] =
          min(LINF, nex[k - 1][i] + nex[k - 1][(i + nex[k - 1][i]) % N]);
      // dump(nex[k][i])
    }
  }
  // REP(k,3){
  //     dump(k)
  //     REP(i,N){
  //         cout << nex[k][i] <<" ";
  //     }
  //     cout << endl;
  // }
  LL dest = N * K;
  LL cur = 0;

  for (int i = 59; i >= 0; i--) {
    if (cur + nex[i][cur % N] <= dest) {
      cur += nex[i][cur % N];
    }
  }

  if (cur == dest) {
    cout << endl;
    return 0;
  } else {
    VI ans;
    set<int> has;
    for (int i = cur % N; i < N; i++) {
      if (has.count(A[i])) {
        while (1) {
          int v = ans.back();
          ans.pop_back();
          has.erase(v);
          if (v == A[i])
            break;
        }
      } else {
        ans.eb(A[i]);
        has.insert(A[i]);
      }
    }
    for (int x : ans) {
      cout << x << " ";
    }
    cout << endl;
  }
}