Problem p02964 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#include <bits/stdc++.h>
#include <stdio.h>

#define _USE_MATH_DEFINES_
#define ll long long
#define ld long double
#define Accepted 0
#define pb push_back
#define mp make_pair
#define sz(x) (int)(x.size())
#define every(x) x.begin(), x.end()
#define F first
#define S second
#define For(i, x, y) for (ll i = x; i <= y; i++)
#define FOr(i, x, y) for (ll i = x; i >= y; i--)
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
// ROAD to... Red

using namespace __gnu_pbds;
using namespace std;

typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

inline bool isvowel(char c) {
  c = tolower(c);
  if (c == 'a' || c == 'e' || c == 'i' || c == 'y' || c == 'o' || c == 'u')
    return 1;
  return 0;
}

const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 2e5 + 123;
const int M = 22;
const int pri = 997;
const int Magic = 2101;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

int n;
ll k;
int lst[N];
int a[N];
int in[N];
int nxt[N];

int main() {
  scanf("%d%lld", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
    lst[a[i]] = i;
  }
  for (int i = 1; i <= n; i++) {
    nxt[lst[a[i]]] = i;
    lst[a[i]] = i;
  }
  int p = 1;
  vector<int> pos;

  while (p <= n) {
    if (nxt[p] <= p) {
      pos.pb(p);
    }
    p = nxt[p] + 1;
  }

  k = (k - 1) % (1 + pos.size());
  vector<int> ans;
  for (int i = pos[k]; i <= n; i++) {
    if (in[a[i]]) {
      while (ans.back() != a[i]) {
        in[ans.back()] = 0;
        ans.pop_back();
      }
      in[ans.back()] = 0;
      ans.pop_back();

    } else {
      ans.pb(a[i]);
      in[a[i]] = 1;
    }
  }

  for (auto x : ans)
    cout << x << ' ';

  return Accepted;
}

// B...a

Fixed submission:
#pragma GCC optimize("O3")

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#include <bits/stdc++.h>
#include <stdio.h>

#define _USE_MATH_DEFINES_
#define ll long long
#define ld long double
#define Accepted 0
#define pb push_back
#define mp make_pair
#define sz(x) (int)(x.size())
#define every(x) x.begin(), x.end()
#define F first
#define S second
#define For(i, x, y) for (ll i = x; i <= y; i++)
#define FOr(i, x, y) for (ll i = x; i >= y; i--)
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
// ROAD to... Red

using namespace __gnu_pbds;
using namespace std;

typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

inline bool isvowel(char c) {
  c = tolower(c);
  if (c == 'a' || c == 'e' || c == 'i' || c == 'y' || c == 'o' || c == 'u')
    return 1;
  return 0;
}

const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 2e5 + 123;
const int M = 22;
const int pri = 997;
const int Magic = 2101;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

int n;
ll k;
int lst[N];
int a[N];
int in[N];
int nxt[N];

int main() {
  scanf("%d%lld", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
    lst[a[i]] = i;
  }
  for (int i = 1; i <= n; i++) {
    nxt[lst[a[i]]] = i;
    lst[a[i]] = i;
  }
  int p = 1;
  vector<int> pos;

  while (p <= n) {
    if (nxt[p] <= p) {
      pos.pb(p);
    }
    p = nxt[p] + 1;
  }

  pos.pb(1);
  k = (k - 1) % pos.size();
  vector<int> ans;
  for (int i = pos[k]; i <= n; i++) {
    if (in[a[i]]) {
      while (ans.back() != a[i]) {
        in[ans.back()] = 0;
        ans.pop_back();
      }
      in[ans.back()] = 0;
      ans.pop_back();

    } else {
      ans.pb(a[i]);
      in[a[i]] = 1;
    }
  }

  for (auto x : ans)
    cout << x << ' ';

  return Accepted;
}

// B...a