Language: C++

#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>

using namespace std;

#define rep(i, a, b) for(int i = (a); i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) ((int)(x).size())
#define pb push_back
#define fst first
#define snd second
#define PI (acos(-1))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))

#define MAXN 1123456
#define MOD 1000000007

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<pii> vii;
typedef vector<vi> vvi;

__gnu_pbds::gp_hash_table<ll,int> back;
__gnu_pbds::gp_hash_table<ll,int> backinv;

//unordered_map<int, int> back;
//unordered_map<int, int> backinv;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    ll n, k;
    cin >> n >> k;

    vi cur;

    vi a(n);
    rep(i,0,n) cin >> a[i];

    ll p=-1;

    ll cyc = 0;

    rep(i,1,k+1) {
        rep(j,0,n) {
            ll l = a[j];
            //cerr << "l: " << l << endl;
            if (back.find(l) != back.end() && back[l] <= p && back[l] != -1) {
                p=back[l]-1;
                back[l] = -1;
                //cerr << "p = " << p << endl;
            } else {
                if (p+1 >= sz(cur)) cur.pb(l);
                else cur[p+1] = l;
                ++p;

                if (backinv.find(p) != backinv.end() && back.find(backinv[p]) != back.end() && back[backinv[p]] == p) {
                    //cerr << "erasing " << backinv[p] << endl;
                    back[backinv[p]] = -1;
                }

                if (back.find(l) != back.end() && backinv.find(back[l]) != backinv.end()) backinv[back[l]] = -1;
                
                back[l]=p;
                //cerr << "back[" << l << "] = " << p << endl;
                backinv[p]=l;
            }
        }
        if (p == -1) {
            cyc = i;   
            break;
        }

        if (i == k) {
            rep(u,0,p+1) cout << cur[u] << " " ;
            cout << endl;
            return 0;
        }
        //getchar();
    }

    if (cyc)
    k %= cyc;

    if (!k) {
        cout << endl;
        return 0;
    }

    rep(i,1,k+1) {
        rep(j,0,n) {
            ll l = a[j];
            //cerr << "l: " << l << endl;
            if (back.find(l) != back.end() && back[l] <= p && back[l] != -1) {
                p=back[l]-1;
                back[l] = -1;
                //cerr << "p = " << p << endl;
            } else {
                if (p+1 >= sz(cur)) cur.pb(l);
                else cur[p+1] = l;
                ++p;

    
                if (backinv.find(p) != backinv.end() && back.find(backinv[p]) != back.end() && back[backinv[p]] == p) {
                    //cerr << "erasing " << backinv[p] << endl;
                    back[backinv[p]] = -1;
                }

                if (back.find(l) != back.end() && backinv.find(back[l]) != backinv.end()) backinv[back[l]] = -1;
                
                back[l]=p;
                //cerr << "back[" << l << "] = " << p << endl;
                backinv[p]=l;
            }
        }

        if (i == k) {
            rep(u,0,p+1) cout << cur[u] << " " ;
            cout << endl;
            return 0;
        }
        //getchar();
    }

}


Compiler output:
a.cc:27:1: error: '__gnu_pbds' does not name a type
   27 | __gnu_pbds::gp_hash_table<ll,int> back;
      | ^~~~~~~~~~
a.cc:28:1: error: '__gnu_pbds' does not name a type
   28 | __gnu_pbds::gp_hash_table<ll,int> backinv;
      | ^~~~~~~~~~
a.cc: In function 'int main()':
a.cc:53:17: error: 'back' was not declared in this scope
   53 |             if (back.find(l) != back.end() && back[l] <= p && back[l] != -1) {
      |                 ^~~~
a.cc:62:21: error: 'backinv' was not declared in this scope
   62 |                 if (backinv.find(p) != backinv.end() && back.find(backinv[p]) != back.end() && back[backinv[p]] == p) {
      |                     ^~~~~~~
a.cc:67:51: error: 'backinv' was not declared in this scope
   67 |                 if (back.find(l) != back.end() && backinv.find(back[l]) != backinv.end()) backinv[back[l]] = -1;
      |                                                   ^~~~~~~
a.cc:71:17: error: 'backinv' was not declared in this scope
   71 |                 backinv[p]=l;
      |                 ^~~~~~~
a.cc:99:17: error: 'back' was not declared in this scope
   99 |             if (back.find(l) != back.end() && back[l] <= p && back[l] != -1) {
      |                 ^~~~
a.cc:109:21: error: 'backinv' was not declared in this scope
  109 |                 if (backinv.find(p) != backinv.end() && back.find(backinv[p]) != back.end() && back[backinv[p]] == p) {
      |                     ^~~~~~~
a.cc:114:51: error: 'backinv' was not declared in this scope
  114 |                 if (back.find(l) != back.end() && backinv.find(back[l]) != backinv.end()) backinv[back[l]] = -1;
      |                                                   ^~~~~~~
a.cc:118:17: error: 'backinv' was not declared in this scope
  118 |                 backinv[p]=l;
      |                 ^~~~~~~