Problem p02964 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <utility>
#include <vector>

#define ios ios_base::sync_with_stdio(0), cout.tie(0), cin.tie(0);
#define S second
#define F first
#define pb push_back
#define nl '\n'
#define all(s) s.begin(), s.end()
#define fo(i, start, finish) for (int i = start; i <= finish; i++)
#define y1 kdfjsalsdkf

const long long MXN = 2e5 + 10;
const long long MNN = 5e3 + 1;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
const long long OO = 1e9 + 500;

typedef long long llong;
typedef unsigned long long ullong;

using namespace std;

llong n, k, b[MXN], pos[MXN], d[64][MXN], contain[MXN];
vector<llong> v;

int main() {
  ios;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    b[i + n] = b[i];
  }
  for (int i = 2 * n - 1; i >= 0; i--) {
    if (i < n)
      d[0][i] = pos[b[i]] - i + 1;
    pos[b[i]] = i;
  }
  for (int i = 1; i <= 60; i++) {
    for (int j = 0; j < n; j++) {
      d[i][j] = min(INF, d[i - 1][j] + d[i - 1][(j + d[i - 1][j]) % n]);
    }
  }
  llong cur = 0;
  for (int i = 60; i >= 0; i--) {
    if (cur + d[i][cur % n] <= n * k)
      cur += d[i][cur % n];
  }
  for (; cur < n * k; cur++) {
    if (contain[b[cur % n]] == 0) {
      v.pb(b[cur % n]);
      contain[b[cur % n]]++;
    } else {
      while (contain[b[cur % n]] != 0) {
        contain[v[v.size() - 1]]--;
        v.pop_back();
      }
    }
  }
  for (int i = 0; i < v.size(); i++) {
    cout << v[i] << ' ';
  }
  return 0;
}

//
//
//


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <utility>
#include <vector>

#define ios ios_base::sync_with_stdio(0), cout.tie(0), cin.tie(0);
#define S second
#define F first
#define pb push_back
#define nl '\n'
#define all(s) s.begin(), s.end()
#define fo(i, start, finish) for (int i = start; i <= finish; i++)
#define y1 kdfjsalsdkf

const long long MXN = 2e5 + 10;
const long long MNN = 5e3 + 1;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
const long long OO = 1e9 + 500;

typedef long long llong;
typedef unsigned long long ullong;

using namespace std;

llong n, k, b[MXN * 2], pos[MXN], d[64][MXN], contain[MXN];
vector<llong> v;

int main() {
  ios;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    b[i + n] = b[i];
  }
  for (int i = 2 * n - 1; i >= 0; i--) {
    if (i < n)
      d[0][i] = pos[b[i]] - i + 1;
    pos[b[i]] = i;
  }
  for (int i = 1; i <= 60; i++) {
    for (int j = 0; j < n; j++) {
      d[i][j] = min(INF, d[i - 1][j] + d[i - 1][(j + d[i - 1][j]) % n]);
    }
  }
  llong cur = 0;
  for (int i = 60; i >= 0; i--) {
    if (cur + d[i][cur % n] <= n * k)
      cur += d[i][cur % n];
  }
  for (; cur < n * k; cur++) {
    if (contain[b[cur % n]] == 0) {
      v.pb(b[cur % n]);
      contain[b[cur % n]]++;
    } else {
      while (contain[b[cur % n]] != 0) {
        contain[v[v.size() - 1]]--;
        v.pop_back();
      }
    }
  }
  for (int i = 0; i < v.size(); i++) {
    cout << v[i] << ' ';
  }
  return 0;
}

//
//
//