Problem p02964 (C++)

Buggy submission (status: Runtime Error):
/* cerberus97 - Hanit Banga */

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#define pb push_back
#define fast_cin()                                                             \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int N = 2e5 + 10;

int a[N], ctr[N], pos[N], nxt[N];
int g[N], depth[N], c_len, c_st;
bool seen[N];

void dfs(int u);

int main() {
  fast_cin();
  int n;
  ll k;
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    a[i + n] = a[i];
  }
  for (int i = 2 * n - 1; i >= 0; --i) {
    if (pos[a[i]]) {
      nxt[i] = pos[a[i]];
    } else {
      nxt[i] = 2 * n;
    }
    pos[a[i]] = i;
  }
  g[n] = 0;
  for (int i = n - 1; i >= 0; --i) {
    if ((nxt[i] + 1) >= n) {
      g[i] = nxt[i] + 1;
    } else {
      g[i] = g[nxt[i] + 1];
    }
    if (g[i] < 2 * n) {
      g[i] %= n;
    } else {
      g[i] = n;
    }
  }
  dfs(0);
  --k;
  int s = 0;
  while (k) {
    if (s == c_st) {
      k %= c_len;
    }
    if (k) {
      s = g[s];
      --k;
    }
  }
  vector<int> ans;
  for (int i = s; i < n; ++i) {
    if (ctr[a[i]]) {
      while (ctr[a[i]]) {
        --ctr[ans.back()];
        ans.pop_back();
      }
    } else {
      ++ctr[a[i]];
      ans.pb(a[i]);
    }
  }
  for (auto &i : ans) {
    cout << i << ' ';
  }
}

void dfs(int u) {
  seen[u] = true;
  if (seen[g[u]]) {
    c_st = g[u];
    c_len = depth[u] - depth[g[u]] + 1;
    return;
  }
  depth[g[u]] = depth[u] + 1;
  dfs(g[u]);
}

Fixed submission:
/* cerberus97 - Hanit Banga */

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#define pb push_back
#define fast_cin()                                                             \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int N = 4e5 + 10;

int a[N], ctr[N], pos[N], nxt[N];
int g[N], depth[N], c_len, c_st;
bool seen[N];

void dfs(int u);

int main() {
  fast_cin();
  int n;
  ll k;
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    a[i + n] = a[i];
  }
  for (int i = 2 * n - 1; i >= 0; --i) {
    if (pos[a[i]]) {
      nxt[i] = pos[a[i]];
    } else {
      nxt[i] = 2 * n;
    }
    pos[a[i]] = i;
  }
  g[n] = 0;
  for (int i = n - 1; i >= 0; --i) {
    if ((nxt[i] + 1) >= n) {
      g[i] = nxt[i] + 1;
    } else {
      g[i] = g[nxt[i] + 1];
    }
    if (g[i] < 2 * n) {
      g[i] %= n;
    } else {
      g[i] = n;
    }
  }
  dfs(0);
  --k;
  int s = 0;
  while (k) {
    if (s == c_st) {
      k %= c_len;
    }
    if (k) {
      s = g[s];
      --k;
    }
  }
  vector<int> ans;
  for (int i = s; i < n; ++i) {
    if (ctr[a[i]]) {
      while (ctr[a[i]]) {
        --ctr[ans.back()];
        ans.pop_back();
      }
    } else {
      ++ctr[a[i]];
      ans.pb(a[i]);
    }
  }
  for (auto &i : ans) {
    cout << i << ' ';
  }
}

void dfs(int u) {
  seen[u] = true;
  if (seen[g[u]]) {
    c_st = g[u];
    c_len = depth[u] - depth[g[u]] + 1;
    return;
  }
  depth[g[u]] = depth[u] + 1;
  dfs(g[u]);
}