Problem p02964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define rep11(i, n) for (int i = 1; i < (int)(n); ++i)
#define repo(i, o, n) for (int i = o; i < (int)(n); ++i)
#define repm(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define sperase(v, n) (v).erase(remove(all(v), n), (v).end())
#define vdelete(v) (v).erase(unique(all(v)), (v).end())
#define pb(n) push_back(n)
#define mp make_pair
#define MOD 1000000007
#define INF 9223372036854775807

int n, k, a[200000], z, memo[200001];
vector<int> v[200001];
vector<int> ANS;
bool f;

signed main() {
  cin >> n >> k;
  rep(i, n) {
    cin >> a[i];
    v[a[i]].pb(i);
    if (!memo[a[i]])
      memo[a[i]] = i + 1;
  }
  while (n) {
    if (z == n - 1)
      f = true;
    repo(i, z, n) {
      auto itr = upper_bound(all(v[a[i]]), i);
      if (itr != v[a[i]].end()) {
        i = *itr;
        if (i == n - 1)
          f = true;
      } else {
        ANS.pb(i + 1);
        z = memo[a[i]];
        break;
      }
    }
    if (f) {
      int hoge = k % (ANS.size() + 1);
      if (hoge == 0) {
        cout << endl;
        return 0;
      }
      hoge--;
      z = ANS[hoge];
      z--;
      repo(i, z, n) {
        auto itr = upper_bound(all(v[a[i]]), i);
        if (itr != v[a[i]].end())
          i = *itr;
        else
          cout << a[i] << " ";
      }
      cout << endl;
      return 0;
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define rep11(i, n) for (int i = 1; i < (int)(n); ++i)
#define repo(i, o, n) for (int i = o; i < (int)(n); ++i)
#define repm(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define sperase(v, n) (v).erase(remove(all(v), n), (v).end())
#define vdelete(v) (v).erase(unique(all(v)), (v).end())
#define pb(n) push_back(n)
#define mp make_pair
#define MOD 1000000007
#define INF 9223372036854775807

int n, k, a[200000], z, memo[200001];
vector<int> v[200001];
vector<int> ANS;
bool f;

signed main() {
  cin >> n >> k;
  rep(i, n) {
    cin >> a[i];
    v[a[i]].pb(i);
    if (!memo[a[i]])
      memo[a[i]] = i + 1;
  }
  while (n) {
    if (z >= n - 1)
      f = true;
    repo(i, z, n) {
      auto itr = upper_bound(all(v[a[i]]), i);
      if (itr != v[a[i]].end()) {
        i = *itr;
        if (i == n - 1)
          f = true;
      } else {
        ANS.pb(i + 1);
        z = memo[a[i]];
        break;
      }
    }
    if (f) {
      int hoge = k % (ANS.size() + 1);
      if (hoge == 0) {
        cout << endl;
        return 0;
      }
      hoge--;
      z = ANS[hoge];
      z--;
      repo(i, z, n) {
        auto itr = upper_bound(all(v[a[i]]), i);
        if (itr != v[a[i]].end())
          i = *itr;
        else
          cout << a[i] << " ";
      }
      cout << endl;
      return 0;
    }
  }
}