Problem p02964 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
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;
}

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
int firstappear[205000];
int lastappear[205000];

ll N, K;
ll A[205000];
l_l dp[205000][102];
vector<int> ans;
bool used[205000];

l_l now;

void f(int index) {
  if (index == -1)
    return;
  if (dp[now.first][index].second + now.second < K) {
    now.second += dp[now.first][index].second;
    now.first = dp[now.first][index].first;
  }
  f(index - 1);
}

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int val = 0; val <= 2e5; val++) {
    firstappear[val] = -1;
    lastappear[val] = -1;
  }
  for (int i = 0; i < N; i++) {
    if (firstappear[A[i]] != -1) {
      dp[lastappear[A[i]]][0].first = (i + 1) % N;
      if (i == (N - 1))
        dp[lastappear[A[i]]][0].second++;
      lastappear[A[i]] = i;
      continue;
    } else {
      firstappear[A[i]] = i;
      lastappear[A[i]] = i;
      continue;
    }
  }
  for (int val = 1; val <= 2e5; val++) {
    if (firstappear[val] == -1)
      continue;
    dp[lastappear[val]][0] = {(firstappear[val] + 1) % N,
                              1 + (firstappear[val] / (N - 1))};
  }
  /*
  for(int i = 0; i < N; i++) {
      cerr << i << " " << dp[i][0].first << " " << dp[i][0].second << endl;
  }
  */
  for (int timer = 1; timer <= 100; timer++) {
    for (int index = 0; index < N; index++) {
      dp[index][timer].second += dp[index][timer - 1].second;
      int now = dp[index][timer - 1].first;
      dp[index][timer].second += dp[now][timer - 1].second;
      chmin(dp[index][timer].second, (ll)1e17);
      dp[index][timer].first = dp[now][timer - 1].first;
    }
  }
  now = {0, 0};
  f(100);
  // cerr << now.first << " " << now.second << endl;
  while (now.second < K) {
    int pos = now.first;
    if (used[A[pos]]) {
      while (used[A[pos]]) {
        used[ans.back()] = false;
        ans.pop_back();
      }
    } else {
      used[A[pos]] = true;
      ans.push_back(A[pos]);
    }
    now.first++;
    if (now.first == N) {
      now.first = 0;
      now.second++;
    }
  }
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i] << " ";
  }
  cout << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
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;
}

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
int firstappear[205000];
int lastappear[205000];

ll N, K;
ll A[205000];
l_l dp[205000][102];
vector<int> ans;
bool used[205000];

l_l now;

void f(int index) {
  if (index == -1)
    return;
  if (dp[now.first][index].second + now.second < K) {
    now.second += dp[now.first][index].second;
    now.first = dp[now.first][index].first;
  }
  f(index - 1);
}

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int val = 0; val <= 2e5; val++) {
    firstappear[val] = -1;
    lastappear[val] = -1;
  }
  for (int i = 0; i < N; i++) {
    if (firstappear[A[i]] != -1) {
      dp[lastappear[A[i]]][0].first = (i + 1) % N;
      if (i == (N - 1))
        dp[lastappear[A[i]]][0].second++;
      lastappear[A[i]] = i;
      continue;
    } else {
      firstappear[A[i]] = i;
      lastappear[A[i]] = i;
      continue;
    }
  }
  for (int val = 1; val <= 2e5; val++) {
    if (firstappear[val] == -1)
      continue;
    dp[lastappear[val]][0] = {(firstappear[val] + 1) % N,
                              1 + ((firstappear[val] + 1) / (N))};
  }
  /*
  for(int i = 0; i < N; i++) {
      cerr << i << " " << dp[i][0].first << " " << dp[i][0].second << endl;
  }
  */
  for (int timer = 1; timer <= 100; timer++) {
    for (int index = 0; index < N; index++) {
      dp[index][timer].second += dp[index][timer - 1].second;
      int now = dp[index][timer - 1].first;
      dp[index][timer].second += dp[now][timer - 1].second;
      chmin(dp[index][timer].second, (ll)1e17);
      dp[index][timer].first = dp[now][timer - 1].first;
    }
  }
  now = {0, 0};
  f(100);
  // cerr << now.first << " " << now.second << endl;
  while (now.second < K) {
    int pos = now.first;
    if (used[A[pos]]) {
      while (used[A[pos]]) {
        used[ans.back()] = false;
        ans.pop_back();
      }
    } else {
      used[A[pos]] = true;
      ans.push_back(A[pos]);
    }
    now.first++;
    if (now.first == N) {
      now.first = 0;
      now.second++;
    }
  }
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i] << " ";
  }
  cout << endl;
  return 0;
}