Problem p02964 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define debug(n) cerr << #n << ':' << n << endl;
#define dline cerr << __LINE__ << endl;
#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)
#define per(i, s, e) for (int i = (int)(s)-1; i >= (int)(e); --i)

using ll = long long;
template <class T, class U> using P = pair<T, U>;
template <class T> using Heap = priority_queue<T>;
template <class T> using heaP = priority_queue<T, vector<T>, greater<T>>;

template <class T> bool ChangeMax(T &a, const T &b) {
  if (a >= b)
    return false;
  a = b;
  return true;
}

template <class T> bool ChangeMin(T &a, const T &b) {
  if (a <= b)
    return false;
  a = b;
  return true;
}

template <class T, size_t N, class U> void Fill(T (&a)[N], const U &v) {
  fill((U *)a, (U *)(a + N), v);
}

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &e : v)
    is >> e;
  return is;
}

int main() {
  int n;
  cin >> n;
  ll k;
  cin >> k;
  vector<int> v(n);
  cin >> v;
  vector<set<int>> sv(n + 1);
  rep(i, 0, n) sv[v[i]].insert(i);
  int cnt = 1;
  {
    int p = n - 1;
    while (1) {
      int val = v[p];
      auto it = lower_bound(sv[val].begin(), sv[val].end(), p);
      if (it == sv[val].begin()) {
        cnt++;
        it = sv[val].end();
      }
      it--;
      p = *it;
      if (p == 0)
        break;
      p--;
    }
  }
  int t = k % cnt;
  cnt = 1;
  int p = 0;
  vector<int> ans;
  while (1) {

    int val = v[p];

    auto it = upper_bound(sv[val].begin(), sv[val].end(), p);
    if (it == sv[val].end()) {
      if (cnt >= t) {
        p++;
        ans.push_back(val);
        if (p >= n)
          break;
        continue;
      } else {
        cnt++;
        it = sv[val].begin();
      }
    }
    p = *it;
    p++;
    if (p >= n) {
      if (cnt >= t) {
        break;
      }
      cnt++;
      p = 0;
    }
  }
  rep(i, 0, ans.size()) {
    if (i)
      cout << ' ';
    cout << ans[i];
  }
  cout << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define debug(n) cerr << #n << ':' << n << endl;
#define dline cerr << __LINE__ << endl;
#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)
#define per(i, s, e) for (int i = (int)(s)-1; i >= (int)(e); --i)

using ll = long long;
template <class T, class U> using P = pair<T, U>;
template <class T> using Heap = priority_queue<T>;
template <class T> using heaP = priority_queue<T, vector<T>, greater<T>>;

template <class T> bool ChangeMax(T &a, const T &b) {
  if (a >= b)
    return false;
  a = b;
  return true;
}

template <class T> bool ChangeMin(T &a, const T &b) {
  if (a <= b)
    return false;
  a = b;
  return true;
}

template <class T, size_t N, class U> void Fill(T (&a)[N], const U &v) {
  fill((U *)a, (U *)(a + N), v);
}

template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &e : v)
    is >> e;
  return is;
}

int main() {
  int n;
  cin >> n;
  ll k;
  cin >> k;
  vector<int> v(n);
  cin >> v;
  vector<set<int>> sv(200001);
  rep(i, 0, n) sv[v[i]].insert(i);
  int cnt = 1;
  {
    int p = n - 1;
    while (1) {
      int val = v[p];
      auto it = lower_bound(sv[val].begin(), sv[val].end(), p);
      if (it == sv[val].begin()) {
        cnt++;
        it = sv[val].end();
      }
      it--;
      p = *it;
      if (p == 0)
        break;
      p--;
    }
  }
  int t = k % cnt;
  cnt = 1;
  int p = 0;
  vector<int> ans;
  while (1) {

    int val = v[p];

    auto it = upper_bound(sv[val].begin(), sv[val].end(), p);
    if (it == sv[val].end()) {
      if (cnt >= t) {
        p++;
        ans.push_back(val);
        if (p >= n)
          break;
        continue;
      } else {
        cnt++;
        it = sv[val].begin();
      }
    }
    p = *it;
    p++;
    if (p >= n) {
      if (cnt >= t) {
        break;
      }
      cnt++;
      p = 0;
    }
  }
  rep(i, 0, ans.size()) {
    if (i)
      cout << ' ';
    cout << ans[i];
  }
  cout << endl;
  return 0;
}