Problem p02964 (C++)

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

#if MYDEBUG
#include "lib/cp_debug.hpp"
#else
#define DBG(...) ;
#endif
#if __cplusplus <= 201402L
template <typename T> T gcd(T a, T b) {
  return ((a % b == 0) ? b : gcd(b, a % b));
}
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#endif
using LL = long long;
constexpr LL LINF = 334ll << 53;
constexpr int INF = 15 << 26;
constexpr LL MOD = 1E9 + 7;

namespace Problem {
using namespace std;
#define int LL
class Solver2 {
public:
  LL n, k;
  vector<LL> a;
  vector<vector<int>> A;
  vector<vector<LL>> tr;
  Solver2(LL n, LL k)
      : n(n), k(k), a(n), A(200001), tr(57, vector<LL>(n, -1)){};

  void nextRow(int i) {
    if (tr[0][i] >= 0)
      return;
    int now = a[i];
    auto nxtp = upper_bound(A[now].begin(), A[now].end(), i);
    if (nxtp < A[now].end()) {
      tr[0][i] = *nxtp - i + 1;
    } else {
      tr[0][i] = A[now][0] + n - i + 1;
    }
  }

  void solve() {
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
      A[a[i]].push_back(i);
    }
    for (int i = 0; i < n; ++i) {
      nextRow(i);
    }
    DBG(a)
    for (int i = 1; i < tr.size(); ++i) {
      for (int j = 0; j < n; ++j) {
        int nxt = (tr[i - 1][j] + j) % n;
        tr[i][j] = tr[i - 1][j] + tr[i - 1][nxt];
      }
      DBG(i, tr[i])
    }
    DBG(a)
    LL sum = 0;
    LL now = 0;
    while (sum < n * k) {
      int P = 0;
      for (int i = tr.size() - 1; i >= 0; --i) {
        if (sum + tr[i][now] <= n * k) {
          P = i;
          break;
        }
        if (i == 0)
          P = -1;
      }
      if (P == -1)
        break;
      sum += tr[P][now];
      DBG(P, sum)
      now = sum % n;
    }
    DBG(a)
    DBG(now)
    if (sum == n * k) {
      cout << endl;
      return;
    }
    vector<int> ans = calc(now);
    DBG(ans)
    for (int i = 0; i < ans.size(); ++i) {
      cout << ans[i];
      if (i < ans.size() - 1)
        cout << ' ';
    }
    cout << endl;
  }
  vector<int> calc(int p) {
    vector<vector<int>> memo(200001);
    set<int> st;
    int size = 0;
    for (int i = p; i < n; ++i) {
      int now = a[i];
      if (memo[now].size() == 0) {
        memo[now].push_back(size);
        size++;
        st.insert(now);
      } else {
        size = memo[now][0];
        set<int> er;
        for (auto x : st) {
          DBG(x, memo[x], size)
          while (memo[x].size() > 0 && memo[x].back() >= size)
            memo[x].pop_back();
          if (memo[x].size() == 0)
            er.insert(x);
        }
        for (auto x : er)
          st.erase(x);
      }
    }
    vector<int> ret(size);
    for (int i = 1; i <= 200000; ++i) {
      for (auto j : memo[i]) {
        ret[j] = i;
      }
    }
    return ret;
  }
};
} // namespace Problem

signed main() {
  std::cin.tie(0);
  std::ios_base::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(12);
  long long n = 0, k;
  std::cin >> n >> k;

  Problem::Solver2 sol(n, k);
  sol.solve();
  return 0;
}


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

#if MYDEBUG
#include "lib/cp_debug.hpp"
#else
#define DBG(...) ;
#endif
#if __cplusplus <= 201402L
template <typename T> T gcd(T a, T b) {
  return ((a % b == 0) ? b : gcd(b, a % b));
}
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#endif
using LL = long long;
constexpr LL LINF = 334ll << 53;
constexpr int INF = 15 << 26;
constexpr LL MOD = 1E9 + 7;

namespace Problem {
using namespace std;
#define int LL
class Solver2 {
public:
  LL n, k;
  vector<LL> a;
  vector<vector<int>> A;
  vector<vector<LL>> tr;
  Solver2(LL n, LL k)
      : n(n), k(k), a(n), A(200001), tr(42, vector<LL>(n, -1)){};

  void nextRow(int i) {
    if (tr[0][i] >= 0)
      return;
    int now = a[i];
    auto nxtp = upper_bound(A[now].begin(), A[now].end(), i);
    if (nxtp < A[now].end()) {
      tr[0][i] = *nxtp - i + 1;
    } else {
      tr[0][i] = A[now][0] + n - i + 1;
    }
  }

  void solve() {
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
      A[a[i]].push_back(i);
    }
    for (int i = 0; i < n; ++i) {
      nextRow(i);
    }
    DBG(a)
    for (int i = 1; i < tr.size(); ++i) {
      for (int j = 0; j < n; ++j) {
        int nxt = (tr[i - 1][j] + j) % n;
        tr[i][j] = tr[i - 1][j] + tr[i - 1][nxt];
      }
      DBG(i, tr[i])
    }
    DBG(a)
    LL sum = 0;
    LL now = 0;
    while (sum < n * k) {
      int P = 0;
      for (int i = tr.size() - 1; i >= 0; --i) {
        if (sum + tr[i][now] <= n * k) {
          P = i;
          break;
        }
        if (i == 0)
          P = -1;
      }
      if (P == -1)
        break;
      sum += tr[P][now];
      DBG(P, sum)
      now = sum % n;
    }
    DBG(a)
    DBG(now)
    if (sum == n * k) {
      cout << endl;
      return;
    }
    vector<int> ans = calc(now);
    DBG(ans)
    for (int i = 0; i < ans.size(); ++i) {
      cout << ans[i];
      if (i < ans.size() - 1)
        cout << ' ';
    }
    cout << endl;
  }
  vector<int> calc(int p) {
    vector<vector<int>> memo(200001);
    set<int> st;
    int size = 0;
    for (int i = p; i < n; ++i) {
      int now = a[i];
      if (memo[now].size() == 0) {
        memo[now].push_back(size);
        size++;
        st.insert(now);
      } else {
        size = memo[now][0];
        set<int> er;
        for (auto x : st) {
          DBG(x, memo[x], size)
          while (memo[x].size() > 0 && memo[x].back() >= size)
            memo[x].pop_back();
          if (memo[x].size() == 0)
            er.insert(x);
        }
        for (auto x : er)
          st.erase(x);
      }
    }
    vector<int> ret(size);
    for (int i = 1; i <= 200000; ++i) {
      for (auto j : memo[i]) {
        ret[j] = i;
      }
    }
    return ret;
  }
};
} // namespace Problem

signed main() {
  std::cin.tie(0);
  std::ios_base::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(12);
  long long n = 0, k;
  std::cin >> n >> k;

  Problem::Solver2 sol(n, k);
  sol.solve();
  return 0;
}