Problem p02964 (C++)

Buggy submission (status: Runtime Error):
// #define DEB1 //input from input.txt
// #define NDEBUG
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <mutex>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <vector>

#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
#define INF(a) memset(a, 0x3f, sizeof(a))
#define ALL(t) (t).begin(), (t).end()
#define FILL(a, c) fill(a, a + sizeof(a) / sizeof(*a), c)
#define FILL2D(A, c) fill(A[0], A[0] + sizeof(A) / sizeof(**A), c)
#define FILL3D(A, c) fill(A[0][0], A[0][0] + sizeof(A) / sizeof(***A), c)
#define REP(i, m) for (int i = 0; i < (m); ++i)
// closed interval, inverse repeat
#define CIREP(i, start, fin) for (int i = (start); i >= (fin); i--)
#define CREP(i, start, fin) for (int i = (start); i <= (fin); i++)
#define RNG(i, start, fin) for (int i = (start); i < (fin); i++)
#define IN(a, x, b) (a <= x && x < b)
#define PB push_back
#define SZ(x) (int)(x).size()

using namespace std;

using LL = long long;
using PAIR = pair<int, int>;
using PAIRLL = pair<LL, LL>;
using VI = vector<int>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VVI = vector<VI>;
const LL LINF = 1001002003004005006ll;
/*
template<typename T>inline istream& operator>>(istream&i,vector<T>&v)
{REP(j,SZ(v))i>>v[j];return i;}

template<typename T>string join(const vector<T>&v)
{stringstream s;REP(i,SZ(v))s<<' '<<v[i];return s.str().substr(1);}

template<typename T>inline ostream& operator<<(ostream&o,const vector<T>&v)
{if(SZ(v))o<<join(v);return o;}
*/

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

/*
constant values
*/

// const int dx[] = {1,0,-1,0};
// const int dy[] = {0,1,0,-1};
// const int MX = 31001;

// const ll INF= 1LL<<60 ;//INF = 1001001001001; //1e9

// Main program
void solve() {
  LL N;
  cin >> N;
  LL K;
  cin >> K;
  K *= N;

  // int max_k = log2(N*K);

  map<int, VLL> key_id; // key_id

  // input, next[0][]
  VLL A(N);
  VLL W(N);
  // VI B(N);
  cerr << "error test" << endl;

  REP(i, 2 * N) {

    if (i < N) {
      cin >> A[i];
      // A[N+i]=B[i]; // 1 2 3 2 3 1 2 3 2 3
    }

    // same exist before
    int a = A[i % N];
    if (key_id.count(a) != 0) {
      int id = key_id[a].back(); //- - - 1 2, 0 3 4 ..
      if (id < N)
        W[id] = i - id + 1;
    }
    // add key
    key_id[a].PB(i);
    // several id for one key
  }

  /*
  REP(i,N){
      cin >> A[i];
      key_id[A[i]].PB(i);
  }

  VLL W(N);
  for (auto kv:key_id){
      int s = kv.second.size();
      kv.second.PB(kv.second[0]+N);
      REP(i,s){
          W[kv.second[i]] = kv.second[i+1] - kv.second[i] +1;
      }
  }


  */

  // doubling

  const int L = 60;
  // const int inf = LLONG_MAX/3;
  VVLL nx(L, W);

  /*
  REP(_,L){
      VLL&x=nx.back();
      VLL Y(N);
      REP(i,N) Y[i]=min(LINF,x[i] + x[(i+x[i])%N]);
      nx.PB(Y);
  }
  */

  REP(k, L) {
    REP(i, N) {
      LL nowid = i + nx[k][i];
      LL nextid = nowid + nx[k][nowid % N];
      nx[k + 1][i] = min(LINF, nextid - i); //= next[k][i] + next[k][nowid%N]
    }
  }

  LL cur = 0;
  CIREP(i, L, 0) {
    if (cur + nx[i][cur % N] <= K)
      cur += nx[i][cur % N];
  }

  // do as the task
  if (cur == K)
    return;

  VLL ans;
  set<int> has;
  CREP(i, cur % N, N - 1) {
    if (has.count(A[i])) {
      while (1) {
        int v = ans.back();
        ans.pop_back();
        has.erase(v);
        if (v == A[i])
          break;
      }
    } else {
      ans.PB(A[i]);
      has.insert(A[i]);
    }
  }

  REP(i, SZ(ans)) {
    cout << ans[i];
    if (i < SZ(ans) - 1)
      cout << " ";
    else
      cout << endl;
  }
  return;
}

// ----------------------------------------------
int main() {

#ifdef DEB1
  cout << "DEBUG MODE" << endl;
  ifstream in("input.txt"); // for debug
  cin.rdbuf(in.rdbuf());    // for debug
#endif

  int T = 1;

  for (int i = 0; i < T; ++i) {
    solve();
  }

  return 0;
}

Fixed submission:
// #define DEB1 //input from input.txt
// #define NDEBUG
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <mutex>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <vector>

#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
#define INF(a) memset(a, 0x3f, sizeof(a))
#define ALL(t) (t).begin(), (t).end()
#define FILL(a, c) fill(a, a + sizeof(a) / sizeof(*a), c)
#define FILL2D(A, c) fill(A[0], A[0] + sizeof(A) / sizeof(**A), c)
#define FILL3D(A, c) fill(A[0][0], A[0][0] + sizeof(A) / sizeof(***A), c)
#define REP(i, m) for (int i = 0; i < (m); ++i)
// closed interval, inverse repeat
#define CIREP(i, start, fin) for (int i = (start); i >= (fin); i--)
#define CREP(i, start, fin) for (int i = (start); i <= (fin); i++)
#define RNG(i, start, fin) for (int i = (start); i < (fin); i++)
#define IN(a, x, b) (a <= x && x < b)
#define PB push_back
#define SZ(x) (int)(x).size()

using namespace std;

using LL = long long;
using PAIR = pair<int, int>;
using PAIRLL = pair<LL, LL>;
using VI = vector<int>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VVI = vector<VI>;
const LL LINF = 1001002003004005006ll;
/*
template<typename T>inline istream& operator>>(istream&i,vector<T>&v)
{REP(j,SZ(v))i>>v[j];return i;}

template<typename T>string join(const vector<T>&v)
{stringstream s;REP(i,SZ(v))s<<' '<<v[i];return s.str().substr(1);}

template<typename T>inline ostream& operator<<(ostream&o,const vector<T>&v)
{if(SZ(v))o<<join(v);return o;}
*/

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

/*
constant values
*/

// const int dx[] = {1,0,-1,0};
// const int dy[] = {0,1,0,-1};
// const int MX = 31001;

// const ll INF= 1LL<<60 ;//INF = 1001001001001; //1e9

// Main program
void solve() {
  LL N;
  cin >> N;
  LL K;
  cin >> K;
  K *= N;

  // int max_k = log2(N*K);

  map<int, VLL> key_id; // key_id

  // input, next[0][]
  VLL A(N);
  VLL W(N);
  // VI B(N);
  cerr << "error test" << endl;

  REP(i, 2 * N) {

    if (i < N) {
      cin >> A[i];
      // A[N+i]=B[i]; // 1 2 3 2 3 1 2 3 2 3
    }

    // same exist before
    int a = A[i % N];
    if (key_id.count(a) != 0) {
      int id = key_id[a].back(); //- - - 1 2, 0 3 4 ..
      if (id < N)
        W[id] = i - id + 1;
    }
    // add key
    key_id[a].PB(i);
    // several id for one key
  }

  /*
  REP(i,N){
      cin >> A[i];
      key_id[A[i]].PB(i);
  }

  VLL W(N);
  for (auto kv:key_id){
      int s = kv.second.size();
      kv.second.PB(kv.second[0]+N);
      REP(i,s){
          W[kv.second[i]] = kv.second[i+1] - kv.second[i] +1;
      }
  }


  */

  // doubling

  const int L = 60;
  // const int inf = LLONG_MAX/3;
  VVLL nx(L + 1, W);

  /*
  REP(_,L){
      VLL&x=nx.back();
      VLL Y(N);
      REP(i,N) Y[i]=min(LINF,x[i] + x[(i+x[i])%N]);
      nx.PB(Y);
  }
  */

  REP(k, L) {
    REP(i, N) {
      LL nowid = i + nx[k][i];
      LL nextid = nowid + nx[k][nowid % N];
      nx[k + 1][i] = min(LINF, nextid - i); //= next[k][i] + next[k][nowid%N]
    }
  }

  LL cur = 0;
  CIREP(i, L, 0) {
    if (cur + nx[i][cur % N] <= K)
      cur += nx[i][cur % N];
  }

  // do as the task
  if (cur == K)
    return;

  VLL ans;
  set<int> has;
  CREP(i, cur % N, N - 1) {
    if (has.count(A[i])) {
      while (1) {
        int v = ans.back();
        ans.pop_back();
        has.erase(v);
        if (v == A[i])
          break;
      }
    } else {
      ans.PB(A[i]);
      has.insert(A[i]);
    }
  }

  REP(i, SZ(ans)) {
    cout << ans[i];
    if (i < SZ(ans) - 1)
      cout << " ";
    else
      cout << endl;
  }
  return;
}

// ----------------------------------------------
int main() {

#ifdef DEB1
  cout << "DEBUG MODE" << endl;
  ifstream in("input.txt"); // for debug
  cin.rdbuf(in.rdbuf());    // for debug
#endif

  int T = 1;

  for (int i = 0; i < T; ++i) {
    solve();
  }

  return 0;
}