Problem p02964 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  vector<vector<ll>> next(60, vector<ll>(n)); // ダブリング
  vector<int> prev(200010, -1);
  rep(i, 2) {
    rep(j, n) {
      if (prev[a[j]] != -1)
        next[0][prev[a[j]] % n] =
            n * i + j - prev[a[j]] + 1; // 同じ値までの距離+1
      prev[a[j]] = n * i + j;           // 座標
    }
  }
  for (int i = 1; i < 60; i++) {
    rep(j, n) {
      next[i][j] = next[i - 1][j] + next[i - 1][(next[i - 1][j] + j) % n];
    }
  }

  ll start = 0, sum = 0;
  while (1) {
    int power = 0;
    rep(i, 60) {
      if (next[i][start] + sum >= n * k) {
        power = i - 1;
        break;
      }
    }
    if (power == -1)
      break;
    sum += next[power][start];
    start = sum % n;
  }

  vector<bool> used(200010);
  deque<int> ans;
  for (int i = start; i < n; i++) { // 残りの長さはn以下
    if (!used[a[i]]) {
      ans.push_back(a[i]);
      used[a[i]] = true;
    } else {
      while (1) {
        int tmp = ans.back();
        ans.pop_back();
        if (tmp == a[i])
          break;
      }
    }
  }
  while (!ans.empty()) {
    printf("%d%c", ans.front(), ans.size() > 1 ? ' ' : '\n');
    ans.pop_front();
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  vector<vector<ll>> next(60, vector<ll>(n)); // ダブリング
  vector<int> prev(200010, -1);
  rep(i, 2) {
    rep(j, n) {
      if (prev[a[j]] != -1)
        next[0][prev[a[j]] % n] =
            n * i + j - prev[a[j]] + 1; // 同じ値までの距離+1
      prev[a[j]] = n * i + j;           // 座標
    }
  }
  for (int i = 1; i < 60; i++) {
    rep(j, n) {
      next[i][j] = next[i - 1][j] + next[i - 1][(next[i - 1][j] + j) % n];
    }
  }

  ll start = 0, sum = 0;
  while (1) {
    int power = 0;
    rep(i, 60) {
      if (next[i][start] + sum >= n * k) {
        power = i - 1;
        break;
      }
    }
    if (power == -1)
      break;
    sum += next[power][start];
    start = sum % n;
  }

  vector<bool> used(200010);
  deque<int> ans;
  for (int i = start; i < n; i++) { // 残りの長さはn以下
    if (!used[a[i]]) {
      ans.push_back(a[i]);
      used[a[i]] = true;
    } else {
      while (1) {
        int tmp = ans.back();
        ans.pop_back();
        used[tmp] = false;
        if (tmp == a[i])
          break;
      }
    }
  }
  while (!ans.empty()) {
    printf("%d%c", ans.front(), ans.size() > 1 ? ' ' : '\n');
    ans.pop_front();
  }
  return 0;
}