Problem p02964 (C++)

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

using namespace std;

#define int long long
#define double long double
#define mod 1000000007ll
#define INF 1000000000000000000ll

#define MAX_N 200010

int tab[MAX_N];
vector<int> v[MAX_N];

int skok[MAX_N][50];
int dist[MAX_N][50];

bool jest[MAX_N];

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n = 200000, k = 1000000000000ll;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    tab[i] = i;
    cin >> tab[i];
    v[tab[i]].push_back(i);
  }

  for (int i = 0; i < n; i++) {
    int a = tab[i];
    auto it = upper_bound(v[a].begin(), v[a].end(), i);
    if (it == v[a].end()) {
      dist[i][0] = n - i + *v[a].begin() + 1;
    } else {
      dist[i][0] = *it - i + 1;
    }
    // cout << dist[i][0] << " ";
  }
  // cout << endl;

  for (int j = 1; j < 50; j++) {
    for (int i = 0; i < n; i++) {
      dist[i][j] = dist[i][j - 1] + dist[(i + dist[i][j - 1]) % n][j - 1];
    }
  }

  int suma = 0;
  bool czy = true;
  while (czy) {
    czy = false;
    int gdzie = suma % n;
    for (int j = 49; j >= 0; j--) {
      if (suma + dist[gdzie][j] < n * k && suma + dist[gdzie][j] > suma) {
        suma += dist[gdzie][j];
        czy = true;
        break;
      }
    }
  }

  int pocz = suma % n;
  // cout << pocz;

  vector<int> res;
  for (int i = pocz; i < n; i++) {
    if (!jest[tab[i]]) {
      res.push_back(tab[i]);
      jest[tab[i]] = true;
    } else {
      while (jest[tab[i]]) {
        jest[res.back()] = false;
        res.pop_back();
      }
    }
  }
  for (int i : res) {
    cout << i << " ";
  }

  return 0;
}
/*

*/


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

using namespace std;

#define int long long
#define double long double
#define mod 1000000007ll
#define INF 1000000000000000000ll

#define MAX_N 200010

int tab[MAX_N];
vector<int> v[MAX_N];

int skok[MAX_N][50];
int dist[MAX_N][50];

bool jest[MAX_N];

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n = 200000, k = 1000000000000ll;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    tab[i] = i;
    cin >> tab[i];
    v[tab[i]].push_back(i);
  }

  for (int i = 0; i < n; i++) {
    int a = tab[i];
    auto it = upper_bound(v[a].begin(), v[a].end(), i);
    if (it == v[a].end()) {
      dist[i][0] = n - i + *v[a].begin() + 1;
    } else {
      dist[i][0] = *it - i + 1;
    }
    // cout << dist[i][0] << " ";
  }
  // cout << endl;

  for (int j = 1; j < 50; j++) {
    for (int i = 0; i < n; i++) {
      int a = dist[i][j - 1], b = dist[(i + dist[i][j - 1]) % n][j - 1];
      dist[i][j] = a;
      if (a + b > a && a + b > b) {
        dist[i][j] = a + b;
      }
    }
  }

  int suma = 0;
  bool czy = true;
  while (czy) {
    czy = false;
    int gdzie = suma % n;
    for (int j = 49; j >= 0; j--) {
      if (suma + dist[gdzie][j] < n * k && suma + dist[gdzie][j] > suma) {
        suma += dist[gdzie][j];
        czy = true;
        break;
      }
    }
  }

  int pocz = suma % n;
  // cout << pocz;

  vector<int> res;
  for (int i = pocz; i < n; i++) {
    if (!jest[tab[i]]) {
      res.push_back(tab[i]);
      jest[tab[i]] = true;
    } else {
      while (jest[tab[i]]) {
        jest[res.back()] = false;
        res.pop_back();
      }
    }
  }
  for (int i : res) {
    cout << i << " ";
  }

  return 0;
}
/*

*/