Problem p02964 (C++)

Buggy submission (status: Runtime Error):
// #include "pch.h"
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <time.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

#define rip(i, n, s) for (int i = (s); i < (int)(n); i++)
#define mpa make_pair
#define all(a) a.begin(), a.end()

template <typename T> void vdeb(vector<T> &da) {
  int size = da.size();
  for (int i = 0; i < size; i++) {
    if (i == size - 1) {
      cout << da[i] << endl;
    } else {
      cout << da[i] << ' ';
    }
  }
}
template <typename T> void vvdeb(vector<T> &da) {
  int size = da.size();
  for (int i = 0; i < size; i++) {
    vdeb(da[i]);
  }
}

void gene(vector<int> &da, vector<int> &ans, int be) {
  vector<bool> ju(201000, false);
  rip(i, da.size(), be) {
    if (ju[da[i]]) {
      while (ans.back() != da[i]) {
        ju[ans.back()] = false;
        ans.pop_back();
      }
      ans.pop_back();
      ju[da[i]] = false;
    } else {
      ans.push_back(da[i]);
      ju[da[i]] = true;
    }
  }
  return;
}

const int M = 11;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> da(n);
  rip(i, n, 0) cin >> da[i];
  vector<int> to(M), ot(M);
  rip(i, M, 0) {
    to[i] = i;
    ot[i] = i;
  }
  // vdeb(da);
  rip(i, n, 0) {
    to[ot[da[i]]] = 0;
    to[ot[0]] = da[i];
    swap(ot[da[i]], ot[0]);
  }
  int co = 1, now = to[0];
  while (now) {
    co++;
    now = to[now];
  }
  co = k % co;
  now = 0;
  rip(i, co, 0) { now = to[now]; }
  int be = n - 1;
  while (be > -1) {
    if (da[be] == now) {
      break;
    }
    be--;
  }
  // vdeb(to);
  if (be == -1) {
    cout << endl;
    return 0;
  }
  vector<int> ans(0);
  gene(da, ans, be);
  vdeb(ans);
  // vdeb(ot);
}

Fixed submission:
// #include "pch.h"
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <time.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

#define rip(i, n, s) for (int i = (s); i < (int)(n); i++)
#define mpa make_pair
#define all(a) a.begin(), a.end()

template <typename T> void vdeb(vector<T> &da) {
  int size = da.size();
  for (int i = 0; i < size; i++) {
    if (i == size - 1) {
      cout << da[i] << endl;
    } else {
      cout << da[i] << ' ';
    }
  }
}
template <typename T> void vvdeb(vector<T> &da) {
  int size = da.size();
  for (int i = 0; i < size; i++) {
    vdeb(da[i]);
  }
}

void gene(vector<int> &da, vector<int> &ans, int be) {
  vector<bool> ju(201000, false);
  rip(i, da.size(), be) {
    if (ju[da[i]]) {
      while (ans.back() != da[i]) {
        ju[ans.back()] = false;
        ans.pop_back();
      }
      ans.pop_back();
      ju[da[i]] = false;
    } else {
      ans.push_back(da[i]);
      ju[da[i]] = true;
    }
  }
  return;
}

const int M = 201000;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> da(n);
  rip(i, n, 0) cin >> da[i];
  vector<int> to(M), ot(M);
  rip(i, M, 0) {
    to[i] = i;
    ot[i] = i;
  }
  // vdeb(da);
  rip(i, n, 0) {
    to[ot[da[i]]] = 0;
    to[ot[0]] = da[i];
    swap(ot[da[i]], ot[0]);
  }
  int co = 1, now = to[0];
  while (now) {
    co++;
    now = to[now];
  }
  co = k % co;
  now = 0;
  rip(i, co, 0) { now = to[now]; }
  int be = n - 1;
  while (be > -1) {
    if (da[be] == now) {
      break;
    }
    be--;
  }
  // vdeb(to);
  if (be == -1) {
    cout << endl;
    return 0;
  }
  vector<int> ans(0);
  gene(da, ans, be);
  vdeb(ans);
  // vdeb(ot);
}