Problem p02964 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC optimize ("-O3","unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define print(x) cout << x << '\n';
#define pe(x) cout << x << " ";
#define lb(v, n) lower_bound(v.begin(), v.end(), n);
#define ub(v, n) upper_bound(v.begin(), v.end(), n);
#define int long long
#define all(x) (x).begin(), (x).end()
// #define double long double

using namespace std;

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

const int MOD = 1e9 + 7;
const ll INF = 1e17;
const int INT_INF = 1e9;
const double pi = acos(-1);
const double EPS = 1e-10;
typedef pair<int, int> P;
const int MAX = 200020;

int A[400020], nxt[200020], idx[200020];
int a[60][200020]; // a[k][i]=A[i]の数字を先頭とする2^k個のブロックの要素数
int N, K;
int func(int k, int i) {
  if (a[k][i] > 0)
    return a[k][i];
  int res = func(k - 1, i) + func(k - 1, (i + func(k - 1, i)) % N);
  a[k][i] = res;
  return res;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  // clock_t start = clock();

  cin >> N >> K;
  REP(i, N) {
    cin >> A[i];
    A[i + N] = A[i];
  }

  REP(i, 200020) idx[i] = -1;
  REPR(i, N * 2 - 1) {
    if (idx[A[i]] == -1) {
      idx[A[i]] = i;
    } else {
      nxt[i] = idx[A[i]];
    }
    nxt[i] = idx[A[i]];
    if (i < N) {
      a[0][i] = nxt[i] - i + 1;
    }
    idx[A[i]] = i;
  }
  /*REP(i, N) {
          pe(a[0][i]);
  }cout << endl;*/

  ll nex = 0;
  ll pos = 0;
  while (pos < (N) * (K - 1)) {
    REP(k, 60) {
      if (pos + func(k, pos % N) > N * K) {
        pos = pos + func(k - 1, pos % N);
        break;
      }
    }
    // print(pos);
  }
  pos %= N;
  // print(pos);
  REP(i, N) { nxt[i] %= N; }
  vector<int> ans;
  for (int i = pos; i < N; i++) {
    if (nxt[i] <= i) {
      ans.pb(A[i]);
    } else {
      i = nxt[i];
    }
  }
  REP(i, ans.size()) { pe(ans[i]); }
  cout << endl;
}


Fixed submission:
// #pragma GCC optimize ("-O3","unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define print(x) cout << x << '\n';
#define pe(x) cout << x << " ";
#define lb(v, n) lower_bound(v.begin(), v.end(), n);
#define ub(v, n) upper_bound(v.begin(), v.end(), n);
#define int long long
#define all(x) (x).begin(), (x).end()
// #define double long double

using namespace std;

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

const int MOD = 1e9 + 7;
const ll INF = 1e17;
const int INT_INF = 1e9;
const double pi = acos(-1);
const double EPS = 1e-10;
typedef pair<int, int> P;
const int MAX = 200020;

int A[400020], nxt[400020], idx[200020];
int a[60][200020]; // a[k][i]=A[i]の数字を先頭とする2^k個のブロックの要素数
int N, K;
int func(int k, int i) {
  if (a[k][i] > 0)
    return a[k][i];
  int res = func(k - 1, i) + func(k - 1, (i + func(k - 1, i)) % N);
  a[k][i] = res;
  return res;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  // clock_t start = clock();

  cin >> N >> K;
  REP(i, N) {
    cin >> A[i];
    A[i + N] = A[i];
  }

  REP(i, 200020) idx[i] = -1;
  REPR(i, N * 2 - 1) {
    if (idx[A[i]] == -1) {
      idx[A[i]] = i;
    } else {
      nxt[i] = idx[A[i]];
    }
    nxt[i] = idx[A[i]];
    if (i < N) {
      a[0][i] = nxt[i] - i + 1;
    }
    idx[A[i]] = i;
  }
  /*REP(i, N) {
          pe(a[0][i]);
  }cout << endl;*/

  ll nex = 0;
  ll pos = 0;
  while (pos < (N) * (K - 1)) {
    REP(k, 60) {
      if (pos + func(k, pos % N) > N * K) {
        pos = pos + func(k - 1, pos % N);
        break;
      }
    }
    // print(pos);
  }
  pos %= N;
  // print(pos);
  REP(i, N) { nxt[i] %= N; }
  vector<int> ans;
  for (int i = pos; i < N; i++) {
    if (nxt[i] <= i) {
      ans.pb(A[i]);
    } else {
      i = nxt[i];
    }
  }
  REP(i, ans.size()) { pe(ans[i]); }
  cout << endl;
}