Problem p02964 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n)-1; i >= 0; i--)

using namespace std;
using ll = long long;

void naive() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  vector<int> v;
  rep(i, k) {
    rep(j, n) {
      if (find(v.begin(), v.end(), a[j]) == v.end()) {
        v.push_back(a[j]);
      } else {
        while (v.back() != a[j])
          v.pop_back();
        v.pop_back();
      }
    }
    for (int x : v)
      cout << x << ' ';
    cout << endl;
  }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  ll n, k;
  cin >> n >> k;
  vector<int> a(n * 2);
  rep(i, n) cin >> a[i], a[i + n] = a[i];
  vector<vector<int>> succ(62, vector<int>(n));
  vector<vector<ll>> sum(62, vector<ll>(n));
  vector<int> pos(200001, -1);
  repr(i, n * 2) {
    if (i < n) {
      succ[0][i] = (pos[a[i]] + 1) % n;
      sum[0][i] = pos[a[i]] - i + 1;
    }
    pos[a[i]] = i;
  }
  const ll INF = 3e18;
  rep(i, 61) rep(j, n) {
    succ[i + 1][j] = succ[i][succ[i][j]];
    sum[i + 1][j] = min(INF, sum[i][j] + sum[i][succ[i][j]]);
  }
  ll x = 0;
  repr(i, 62) {
    if (x + sum[i][x % n] < n * k) {
      x += sum[i][x % n];
    }
  }
  vector<bool> in(200001);
  vector<int> ans;
  while (x < n * k) {
    ll v = a[x % n];
    if (in[v]) {
      while (ans.back() != v) {
        ans.pop_back();
      }
      ans.pop_back();
      in[v] = false;
    } else {
      in[v] = true;
      ans.push_back(v);
    }
    x++;
  }
  for (int x : ans)
    cout << x << ' ';
  cout << endl;
}

Fixed submission:
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n)-1; i >= 0; i--)

using namespace std;
using ll = long long;

void naive() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  vector<int> v;
  rep(i, k) {
    rep(j, n) {
      if (find(v.begin(), v.end(), a[j]) == v.end()) {
        v.push_back(a[j]);
      } else {
        while (v.back() != a[j])
          v.pop_back();
        v.pop_back();
      }
    }
    for (int x : v)
      cout << x << ' ';
    cout << endl;
  }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  ll n, k;
  cin >> n >> k;
  vector<int> a(n * 2);
  rep(i, n) cin >> a[i], a[i + n] = a[i];
  vector<vector<int>> succ(62, vector<int>(n));
  vector<vector<ll>> sum(62, vector<ll>(n));
  vector<int> pos(200001, -1);
  repr(i, n * 2) {
    if (i < n) {
      succ[0][i] = (pos[a[i]] + 1) % n;
      sum[0][i] = pos[a[i]] - i + 1;
    }
    pos[a[i]] = i;
  }
  const ll INF = 3e18;
  rep(i, 61) rep(j, n) {
    succ[i + 1][j] = succ[i][succ[i][j]];
    sum[i + 1][j] = min(INF, sum[i][j] + sum[i][succ[i][j]]);
  }
  ll x = 0;
  repr(i, 62) {
    if (x + sum[i][x % n] < n * k) {
      x += sum[i][x % n];
    }
  }
  vector<bool> in(200001);
  vector<int> ans;
  while (x < n * k) {
    ll v = a[x % n];
    if (in[v]) {
      while (ans.back() != v) {
        in[ans.back()] = false;
        ans.pop_back();
      }
      ans.pop_back();
      in[v] = false;
    } else {
      in[v] = true;
      ans.push_back(v);
    }
    x++;
  }
  for (int x : ans)
    cout << x << ' ';
  cout << endl;
}