Problem p02964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll, P> P3;
typedef pair<P, P> PP;
constexpr ll MOD = ll(1e9) + 7;
constexpr int IINF = INT_MAX;
constexpr ll LLINF = LLONG_MAX;
constexpr int MAX_N = int(2e5) + 5;
constexpr double EPS = 1e-8;
constexpr int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define ALL(v) (v).begin(), (v).end()

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n * 2), nxt(n * 2), len(n * 2);
  vector<vector<int>> pos(MAX_N);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i + n] = a[i];
    pos[a[i]].push_back(i);
    pos[a[i]].push_back(i + n);
  }
  for (int i = 0; i < MAX_N; i++) {
    sort(pos[i].begin(), pos[i].end());
  }
  for (int id = 0; id != n;) {
    nxt[id] = *upper_bound(pos[a[id]].begin(), pos[a[id]].end(), id) + 1;
    ll l = nxt[id] - id;
    if (nxt[id] > n)
      nxt[id] %= n;
    len[nxt[id]] = len[id] + l;
    id = nxt[id];
  }
  ll m = (k * n) % len[n];
  int bg = 0;
  while (m >= len[nxt[bg]]) {
    bg = nxt[bg];
  }
  m -= len[bg];
  vector<int> s;
  vector<bool> used(MAX_N, false);
  for (int i = 0, j = bg; i < m; i++, j = (j + 1) % n) {
    if (used[a[j]]) {
      while (s.back() != a[j]) {
        used[s.back()] = false;
        s.pop_back();
      }
      used[a[j]] = false;
      s.pop_back();
    } else {
      used[a[j]] = true;
      s.push_back(a[j]);
    }
  }
  for (int i = 0; i < s.size(); i++) {
    cout << s[i] << " \n"[i == int(s.size()) - 1];
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll, P> P3;
typedef pair<P, P> PP;
constexpr ll MOD = ll(1e9) + 7;
constexpr int IINF = INT_MAX;
constexpr ll LLINF = LLONG_MAX;
constexpr int MAX_N = int(2e5) + 5;
constexpr double EPS = 1e-8;
constexpr int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define ALL(v) (v).begin(), (v).end()

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n * 2), nxt(n * 2), len(n * 2);
  vector<vector<int>> pos(MAX_N);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i + n] = a[i];
    pos[a[i]].push_back(i);
    pos[a[i]].push_back(i + n);
  }
  for (int i = 0; i < MAX_N; i++) {
    sort(pos[i].begin(), pos[i].end());
  }
  for (int id = 0; id != n;) {
    nxt[id] = *upper_bound(pos[a[id]].begin(), pos[a[id]].end(), id) + 1;
    ll l = nxt[id] - id;
    while (nxt[id] > n)
      nxt[id] -= n;
    len[nxt[id]] = len[id] + l;
    id = nxt[id];
  }
  ll m = (k * n) % len[n];
  int bg = 0;
  while (m >= len[nxt[bg]]) {
    bg = nxt[bg];
  }
  m -= len[bg];
  vector<int> s;
  vector<bool> used(MAX_N, false);
  for (int i = 0, j = bg; i < m; i++, j = (j + 1) % n) {
    if (used[a[j]]) {
      while (s.back() != a[j]) {
        used[s.back()] = false;
        s.pop_back();
      }
      used[a[j]] = false;
      s.pop_back();
    } else {
      used[a[j]] = true;
      s.push_back(a[j]);
    }
  }
  for (int i = 0; i < s.size(); i++) {
    cout << s[i] << " \n"[i == int(s.size()) - 1];
  }
  return 0;
}