Problem p02964 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = (int)2 * 1e5 + 5;

int n;
long long len;
int a[N];
long long jump[N][70], gap[N][70];
int rem[N];

int main() {
  freopen("input.txt", "r", stdin);
  iostream::sync_with_stdio(0);

  cin >> n >> len;
  for (int i = 1; i <= n; i++)
    cin >> a[i];

  for (int i = n; i >= 1; i--)
    jump[i][0] = rem[a[i]], rem[a[i]] = i;
  for (int i = n; i >= 1; i--)
    if (jump[i][0] == 0)
      jump[i][0] = rem[a[i]], gap[i][0] = 1;

#define nxt(x) ((x) == n ? 1 : (x) + 1)

  for (int k = 1; k < 70; k++)
    for (int i = 1; i <= n; i++)
      jump[i][k] = jump[nxt(jump[i][k - 1])][k - 1],
      gap[i][k] = gap[i][k - 1] + gap[nxt(jump[i][k - 1])][k - 1] +
                  (jump[i][k - 1] == n),
      gap[i][k] = min(gap[i][k], (long long)1e18);
  long long tmp = len;
  int cur = 1;
  while (len > 1) {
    for (int _ = 69; _ >= 0; _--) {
      if (len > gap[cur][_] + (jump[cur][_] == n)) {
        len -= gap[cur][_] + (jump[cur][_] == n);
        cur = nxt(jump[cur][_]);
        break;
      }
    }
    // cout << cur << ' ' << len << endl;
  }
  // for(int i=1;i<=n;i++)   cout << jump[i][0] << ' ' << gap[i][0] << endl;
  // cout << jump[1][2] << endl;

  assert(len == 1);
  vector<int> st;
  map<int, int> mp;
  for (int i = cur; i <= n; i++) {
    if (mp[a[i]] == 0) {
      st.push_back(a[i]);
      mp[a[i]] = 1;
    } else {
      while (st.back() != a[i])
        mp[st.back()] = 0, st.pop_back();
      st.pop_back();
      mp[a[i]] = 0;
    }
  }
  for (auto v : st)
    cout << v << ' ';

  // cout << "asd" << endl;
  // st.clear();
  // mp.clear();
  // for(int i=1;i<=tmp;i++){
  //     for(int j=1;j<=n;j++){
  //         if (mp[a[j]] == 0){
  //             st.push_back(a[j]);
  //             mp[a[j]] = 1;
  //         }
  //         else{
  //             while(st.back() != a[j])   mp[st.back()] = 0, st.pop_back();
  //             st.pop_back();
  //             mp[a[j]] = 0;
  //         }
  //         if (st.size() == 0) cout << j << ' ' << tmp+1-i << endl;
  //         //for(auto v: st) cout << v << ' '; cout << endl;
  //     }
  // }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = (int)2 * 1e5 + 5;

int n;
long long len;
int a[N];
long long jump[N][70], gap[N][70];
int rem[N];

int main() {
  iostream::sync_with_stdio(0);

  cin >> n >> len;
  for (int i = 1; i <= n; i++)
    cin >> a[i];

  for (int i = n; i >= 1; i--)
    jump[i][0] = rem[a[i]], rem[a[i]] = i;
  for (int i = n; i >= 1; i--)
    if (jump[i][0] == 0)
      jump[i][0] = rem[a[i]], gap[i][0] = 1;

#define nxt(x) ((x) == n ? 1 : (x) + 1)

  for (int k = 1; k < 70; k++)
    for (int i = 1; i <= n; i++)
      jump[i][k] = jump[nxt(jump[i][k - 1])][k - 1],
      gap[i][k] = gap[i][k - 1] + gap[nxt(jump[i][k - 1])][k - 1] +
                  (jump[i][k - 1] == n),
      gap[i][k] = min(gap[i][k], (long long)1e18);
  long long tmp = len;
  int cur = 1;
  while (len > 1) {
    for (int _ = 69; _ >= 0; _--) {
      if (len > gap[cur][_] + (jump[cur][_] == n)) {
        len -= gap[cur][_] + (jump[cur][_] == n);
        cur = nxt(jump[cur][_]);
        break;
      }
    }
    // cout << cur << ' ' << len << endl;
  }
  // for(int i=1;i<=n;i++)   cout << jump[i][0] << ' ' << gap[i][0] << endl;
  // cout << jump[1][2] << endl;

  assert(len == 1);
  vector<int> st;
  map<int, int> mp;
  for (int i = cur; i <= n; i++) {
    if (mp[a[i]] == 0) {
      st.push_back(a[i]);
      mp[a[i]] = 1;
    } else {
      while (st.back() != a[i])
        mp[st.back()] = 0, st.pop_back();
      st.pop_back();
      mp[a[i]] = 0;
    }
  }
  for (auto v : st)
    cout << v << ' ';

  // cout << "asd" << endl;
  // st.clear();
  // mp.clear();
  // for(int i=1;i<=tmp;i++){
  //     for(int j=1;j<=n;j++){
  //         if (mp[a[j]] == 0){
  //             st.push_back(a[j]);
  //             mp[a[j]] = 1;
  //         }
  //         else{
  //             while(st.back() != a[j])   mp[st.back()] = 0, st.pop_back();
  //             st.pop_back();
  //             mp[a[j]] = 0;
  //         }
  //         if (st.size() == 0) cout << j << ' ' << tmp+1-i << endl;
  //         //for(auto v: st) cout << v << ' '; cout << endl;
  //     }
  // }
}