Problem p02964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define pb push_back
#define pll pair<ll, ll>
#define mp make_pair
#define pyshnapyshnakaa                                                        \
  ios_base ::sync_with_stdio(0);                                               \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define x first
#define y second
#pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
#define plll pair<ll, pair<ll, ll>>
#define pllll pair<pair<ll, ll>, pair<ll, ll>>
#define psl pair<string, ll>
#define all(a) a.begin(), a.end()
#define vvl vector<vector<ll>>

typedef long long ll;

typedef long double ld;

using namespace std;

const ll maxn = 1e6 + 100;
const ll maxc = 1e9;

ll n, m, k, t;

ll A[maxn];

vector<ll> I[maxn];

ll J[maxn];

ll F[maxn];

void solve(ll i) {
  vector<ll> ANS;
  // cout << " SOLVING " << i << endl;
  ll q, w;
  while (1) {
    ll j = J[i] + 1;
    // cout << i << " " << j << endl;
    if (I[A[i]].back() == i) {
      // cur++;
      // j = 0;
      j--;
      ANS.pb(A[i]);
    }
    i = (I[A[i]][j] + 1);
    if (i >= n) {
      break;
    }
  }
  for (q = 0; q < ANS.size(); q++) {
    cout << ANS[q] << " ";
  }
  exit(0);
}

int main() {
  pyshnapyshnakaa;
  ll q, w, e, a, b, c;
  cin >> n >> k;
  if (n == 1) {
    while (1) {
      n++;
    }
  }
  for (q = 0; q < n; q++) {
    cin >> A[q];
    J[q] = I[A[q]].size();
    I[A[q]].pb(q);
  }
  // cout << I[1].size() << endl;
  // if (n == 1) {
  //     if (k % 2) {
  //         cout << A[0];
  //     }
  //     return 0;
  // }
  vector<ll> starts = {0};
  for (q = 1; q < n; q++) {
    F[q] = -1;
  }
  ll i = 0;
  ll cur = 0;
  while (1) {
    // cout << i << " " << cur << " I CUR" << endl;
    if (cur == k - 1) {
      solve(i);
    }
    ll j = J[i] + 1;
    if (I[A[i]].back() == i) {
      cur++;
      j = 0;
      // cout << "HERRE " << j << endl;
      i = (I[A[i]][j] + 1) % n;
      starts.pb(i);
      F[i] = cur;
    } else {
      // cout << "ANOTHER WAY OUT" << endl;
      i = (I[A[i]][j] + 1) % n;
    }
    if (cur == k - 1 && i == 0) {
      break;
    }
    if (i == 0) {
      cur++;
      // cout << cur << " " << k << endl;
      starts.pb(i);
      // if (F[i] != -1) {
      // cout << "PYMAV 2 " << i << " " << cur << " " << F[i] << endl;
      ll k1 = k - 1;
      ll c = k1 % (cur);
      // cout << c << " C" << endl;
      ll ind = starts[c];
      // cout << "IND " << ind << endl;
      solve(ind);
      // }
      // F[i] = cur;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define pll pair<ll, ll>
#define mp make_pair
#define pyshnapyshnakaa                                                        \
  ios_base ::sync_with_stdio(0);                                               \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define x first
#define y second
#pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
#define plll pair<ll, pair<ll, ll>>
#define pllll pair<pair<ll, ll>, pair<ll, ll>>
#define psl pair<string, ll>
#define all(a) a.begin(), a.end()
#define vvl vector<vector<ll>>

typedef long long ll;

typedef long double ld;

using namespace std;

const ll maxn = 1e6 + 100;
const ll maxc = 1e9;

ll n, m, k, t;

ll A[maxn];

vector<ll> I[maxn];

ll J[maxn];

ll F[maxn];

void solve(ll i) {
  vector<ll> ANS;
  // cout << " SOLVING " << i << endl;
  ll q, w;
  while (1) {
    ll j = J[i] + 1;
    // cout << i << " " << j << endl;
    if (I[A[i]].back() == i) {
      // cur++;
      // j = 0;
      j--;
      ANS.pb(A[i]);
    }
    i = (I[A[i]][j] + 1);
    if (i >= n) {
      break;
    }
  }
  for (q = 0; q < ANS.size(); q++) {
    cout << ANS[q] << " ";
  }
  exit(0);
}

int main() {
  pyshnapyshnakaa;
  ll q, w, e, a, b, c;
  cin >> n >> k;
  // if (n == 1) {
  //     while(1) {
  //         n++;
  //     }
  // }
  for (q = 0; q < n; q++) {
    cin >> A[q];
    J[q] = I[A[q]].size();
    I[A[q]].pb(q);
  }
  // cout << I[1].size() << endl;
  // if (n == 1) {
  //     if (k % 2) {
  //         cout << A[0];
  //     }
  //     return 0;
  // }
  vector<ll> starts = {0};
  for (q = 1; q < n; q++) {
    F[q] = -1;
  }
  ll i = 0;
  ll cur = 0;
  while (1) {
    // cout << i << " " << cur << " I CUR" << endl;
    if (cur == k - 1) {
      solve(i);
    }
    ll j = J[i] + 1;
    if (I[A[i]].back() == i) {
      cur++;
      j = 0;
      // cout << "HERRE " << j << endl;
      i = (I[A[i]][j] + 1) % n;
      starts.pb(i);
      F[i] = cur;
    } else {
      // cout << "ANOTHER WAY OUT" << endl;
      i = (I[A[i]][j] + 1) % n;
    }
    if (cur == k - 1 && i == 0) {
      break;
    }
    if (i == 0) {
      cur++;
      // cout << cur << " " << k << endl;
      starts.pb(i);
      // if (F[i] != -1) {
      // cout << "PYMAV 2 " << i << " " << cur << " " << F[i] << endl;
      ll k1 = k - 1;
      ll c = k1 % (cur);
      // cout << c << " C" << endl;
      ll ind = starts[c];
      // cout << "IND " << ind << endl;
      solve(ind);
      // }
      // F[i] = cur;
    }
  }
  return 0;
}