Language: C++

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

ll N, K;
ll A[200010];
ll next[60][200010], over[60][200010];
ll pre[200010];

int main()
{
    cin >> N >> K;
    for(int i = 0; i < N; i++)cin >> A[i];

    for(int i = 0; i < 2 * N; i++)
    {
        if(pre[A[i % N]] == 0)pre[A[i % N]] = i + 1;
        else
        {
            int idx = (pre[A[i % N]] - 1) % N;
            next[0][idx] = (i + 1) % N;
            over[0][idx] = ((pre[A[i % N]] - 1 < N && i + 1 >= N) | (i + 1) % N == 0) ? 1 : 0;
            pre[A[i % N]] = i + 1;
        }
    }

    for(int i = 1; i < 60; i++)
    {
        for(int j = 0; j < N; j++)
        {
            next[i][j] = next[i - 1][next[i - 1][j]];
            over[i][j] = over[i - 1][j] + over[i - 1][next[i - 1][j]];
        }
    }

    ll l = 0, r = 1LL << 60;
    ll mem;
    for(int i = 0; i < 64; i++)
    {
        ll mid = (l + r) / 2;
        ll now = 0, cnt = 0;
        for(int j = 0; j < 60; j++)if(mid & (1LL << j))
        {
            cnt += over[j][now];
            now = next[j][now];
        }
        if(K - 1 == cnt)
        {
            mem = now;
            break;
        }
        if(cnt < K - 1)l = mid;
        else r = mid;
    }

    int bit[200010] = {0};
    vector<int> ans;
    for(int i = mem; i < N; i++)
    {
        if(bit[A[i]])
        {
            while(ans.back() != A[i])
            {
                bit[ans.back()] ^= 1;
                ans.pop_back();
            }
            bit[ans.back()] ^= 1;
            ans.pop_back();
        }
        else
        {
            bit[A[i]] ^= 1;
            ans.push_back(A[i]);
        }
    }

    for(int i = 0; i < ans.size(); i++)
    {
        cout << ans[i];
        if(i != ans.size())cout << " ";
    }
    cout << endl;

    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:22:13: error: reference to 'next' is ambiguous
   22 |             next[0][idx] = (i + 1) % N;
      |             ^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:66,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:8:4: note:                 'll next [60][200010]'
    8 | ll next[60][200010], over[60][200010];
      |    ^~~~
a.cc:32:13: error: reference to 'next' is ambiguous
   32 |             next[i][j] = next[i - 1][next[i - 1][j]];
      |             ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:8:4: note:                 'll next [60][200010]'
    8 | ll next[60][200010], over[60][200010];
      |    ^~~~
a.cc:32:26: error: reference to 'next' is ambiguous
   32 |             next[i][j] = next[i - 1][next[i - 1][j]];
      |                          ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:8:4: note:                 'll next [60][200010]'
    8 | ll next[60][200010], over[60][200010];
      |    ^~~~
a.cc:32:38: error: reference to 'next' is ambiguous
   32 |             next[i][j] = next[i - 1][next[i - 1][j]];
      |                                      ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:8:4: note:                 'll next [60][200010]'
    8 | ll next[60][200010], over[60][200010];
      |    ^~~~
a.cc:33:55: error: reference to 'next' is ambiguous
   33 |             over[i][j] = over[i - 1][j] + over[i - 1][next[i - 1][j]];
      |                                                       ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:8:4: note:                 'll next [60][200010]'
    8 | ll next[60][200010], over[60][200010];
      |    ^~~~
a.cc:46:19: error: reference to 'next' is ambiguous
   46 |             now = next[j][now];
      |                   ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:8:4: note:                 'll next [60][200010]'
    8 | ll next[60][200010], over[60][200010];
      |    ^~~~