Problem p02964 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll INF = 1e9 + 7;
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<ll> ar = a;
  reverse(ar.begin(), ar.end());
  vector<set<ll>> pos(2 * 1e5);
  for (int i = 0; i < n; i++) {
    pos[ar[i]].insert(i + 1);
    if (pos[ar[i]].size() == 1) {
      pos[ar[i]].insert(i + 1 + n);
    }
  }

  vector<ll> start(n + 1);
  int p = 1;
  ll v = ar[0];
  ll ans = 0;
  while (true) {
    ll next = *(pos[v].lower_bound(p + 1));
    if (next == n) {
      start[ans] = next;
      break;
    } else if (next == 2 * n) {
      ans++;
      start[ans] = next - n;
      break;
    } else if (next > n) {
      p = next + 1 - n;
      ans++;
      start[ans] = p - 1;
    } else {
      p = next + 1;
      start[ans] = next;
    }
    v = ar[p - 1];
  }
  ans++;
  ll count = k % ans;
  if (count == 0)
    return 0;
  ll s = start[ans - count];
  stack<ll> sta;
  set<ll> tmp;
  for (int i = n - s; i < n; i++) {
    if (tmp.find(a[i]) == tmp.end()) {
      tmp.insert(a[i]);
      sta.push(a[i]);
    } else {
      while (true) {
        ll cand = sta.top();
        sta.pop();
        tmp.erase(cand);
        if (cand == a[i]) {
          break;
        }
      }
    }
  }
  stack<ll> ansq;
  ll size = sta.size();
  for (int i = 0; i < size; i++) {
    ll cand = sta.top();
    sta.pop();
    ansq.push(cand);
  }
  ll anss = ansq.size();
  for (int i = 0; i < anss; i++) {
    ll cand = ansq.top();
    ansq.pop();
    cout << cand << " ";
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll INF = 1e9 + 7;
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<ll> ar = a;
  reverse(ar.begin(), ar.end());
  vector<set<ll>> pos(2 * 1e5 + 10);
  for (int i = 0; i < n; i++) {
    pos[ar[i]].insert(i + 1);
    if (pos[ar[i]].size() == 1) {
      pos[ar[i]].insert(i + 1 + n);
    }
  }

  vector<ll> start(n + 1);
  int p = 1;
  ll v = ar[0];
  ll ans = 0;
  while (true) {
    ll next = *(pos[v].lower_bound(p + 1));
    if (next == n) {
      start[ans] = next;
      break;
    } else if (next == 2 * n) {
      ans++;
      start[ans] = next - n;
      break;
    } else if (next > n) {
      p = next + 1 - n;
      ans++;
      start[ans] = p - 1;
    } else {
      p = next + 1;
      start[ans] = next;
    }
    v = ar[p - 1];
  }
  ans++;
  ll count = k % ans;
  if (count == 0)
    return 0;
  ll s = start[ans - count];
  stack<ll> sta;
  set<ll> tmp;
  for (int i = n - s; i < n; i++) {
    if (tmp.find(a[i]) == tmp.end()) {
      tmp.insert(a[i]);
      sta.push(a[i]);
    } else {
      while (true) {
        ll cand = sta.top();
        sta.pop();
        tmp.erase(cand);
        if (cand == a[i]) {
          break;
        }
      }
    }
  }
  stack<ll> ansq;
  ll size = sta.size();
  for (int i = 0; i < size; i++) {
    ll cand = sta.top();
    sta.pop();
    ansq.push(cand);
  }
  ll anss = ansq.size();
  for (int i = 0; i < anss; i++) {
    ll cand = ansq.top();
    ansq.pop();
    cout << cand << " ";
  }
}