Problem p02964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define INF 2000000000000000000
#define ll long long
using namespace std;

ll MAX = pow(10, 5) * 2 + 1;
ll N, K;

vector<ll> calc(vector<ll> &ans) {
  vector<vector<ll>> place(MAX);
  for (ll i = 0; i < ans.size(); ++i) {
    place.at(ans.at(i)).push_back(i);
  }
  vector<ll> res;
  ll index = 0;
  while (index < ans.size()) {
    if (lower_bound(place.at(ans.at(index)).begin(),
                    place.at(ans.at(index)).end(),
                    index + 1) == place.at(ans.at(index)).end()) {
      res.push_back(ans.at(index));
    } else {
      index = *lower_bound(place.at(ans.at(index)).begin(),
                           place.at(ans.at(index)).end(), index + 1);
    }
    index += 1;
  }
  return res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> N >> K;
  vector<ll> A(N);
  vector<vector<ll>> place(MAX);
  for (ll i = 0; i < N; ++i) {
    cin >> A.at(i);
    place.at(A.at(i)).push_back(i);
  }
  // A = calc(A);
  // for (ll i = 0; i < A.size(); ++i) {
  //   cout << A.at(i) << " ";
  // }
  // cout << "\n";
  ll power = 30;
  vector<vector<ll>> d(power, vector<ll>(N));
  for (ll i = 0; i < N; ++i) {
    ll index =
        lower_bound(place.at(A.at(i)).begin(), place.at(A.at(i)).end(), i + 1) -
        place.at(A.at(i)).begin();
    if (index == place.at(A.at(i)).size()) {
      index = 0;
    }
    d.at(0).at(i) = (place.at(A.at(i)).at(index) + 1) % N;
  }
  for (ll i = 0; i < power - 1; ++i) {
    for (ll j = 0; j < N; ++j) {
      d.at(i + 1).at(j) = d.at(i).at(d.at(i).at(j));
    }
  }
  vector<vector<ll>> len(power, vector<ll>(N));
  for (ll i = 0; i < N; ++i) {
    len.at(0).at(i) = (d.at(0).at(i) - 1) - i;
    if (len.at(0).at(i) <= 0) {
      len.at(0).at(i) += N;
    }
    if (len.at(0).at(i) <= 0) {
      len.at(0).at(i) += N;
    }
    len.at(0).at(i) += 1;
  }
  for (ll i = 0; i < power - 1; ++i) {
    for (ll j = 0; j < N; ++j) {
      len.at(i + 1).at(j) = len.at(i).at(d.at(i).at(j)) + len.at(i).at(j);
    }
  }
  // for (ll i = 0; i < power; ++i) {
  //   for (ll j = 0; j < N; ++j) {
  //     cout << len.at(i).at(j) << " ";
  //   }
  //   cout << "\n";
  // }
  ll now = 0;
  ll num = 0;
  K *= N;
  while (true) {
    bool update = false;
    for (ll i = power - 1; i >= 0; --i) {
      if (len.at(i).at(now) <= K) {
        update = true;
        K -= len.at(i).at(now);
        now = d.at(i).at(now);
        break;
      }
    }
    if (!update) {
      break;
    }
  }
  // cout << now << "\n";
  // cout << K << "\n";
  vector<ll> ans;
  for (ll i = 0; i < K; ++i) {
    ans.push_back(A.at(now + i));
  }
  ans = calc(ans);
  for (ll i = 0; i < ans.size(); ++i) {
    cout << ans.at(i) << " ";
  }
  cout << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
#define INF 2000000000000000000
#define ll long long
using namespace std;

ll MAX = pow(10, 5) * 2 + 1;
ll N, K;

vector<ll> calc(vector<ll> &ans) {
  vector<vector<ll>> place(MAX);
  for (ll i = 0; i < ans.size(); ++i) {
    place.at(ans.at(i)).push_back(i);
  }
  vector<ll> res;
  ll index = 0;
  while (index < ans.size()) {
    if (lower_bound(place.at(ans.at(index)).begin(),
                    place.at(ans.at(index)).end(),
                    index + 1) == place.at(ans.at(index)).end()) {
      res.push_back(ans.at(index));
    } else {
      index = *lower_bound(place.at(ans.at(index)).begin(),
                           place.at(ans.at(index)).end(), index + 1);
    }
    index += 1;
  }
  return res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> N >> K;
  vector<ll> A(N);
  vector<vector<ll>> place(MAX);
  for (ll i = 0; i < N; ++i) {
    cin >> A.at(i);
    place.at(A.at(i)).push_back(i);
  }
  // A = calc(A);
  // for (ll i = 0; i < A.size(); ++i) {
  //   cout << A.at(i) << " ";
  // }
  // cout << "\n";
  ll power = 40;
  vector<vector<ll>> d(power, vector<ll>(N));
  for (ll i = 0; i < N; ++i) {
    ll index =
        lower_bound(place.at(A.at(i)).begin(), place.at(A.at(i)).end(), i + 1) -
        place.at(A.at(i)).begin();
    if (index == place.at(A.at(i)).size()) {
      index = 0;
    }
    d.at(0).at(i) = (place.at(A.at(i)).at(index) + 1) % N;
  }
  for (ll i = 0; i < power - 1; ++i) {
    for (ll j = 0; j < N; ++j) {
      d.at(i + 1).at(j) = d.at(i).at(d.at(i).at(j));
    }
  }
  vector<vector<ll>> len(power, vector<ll>(N));
  for (ll i = 0; i < N; ++i) {
    len.at(0).at(i) = (d.at(0).at(i) - 1) - i;
    if (len.at(0).at(i) <= 0) {
      len.at(0).at(i) += N;
    }
    if (len.at(0).at(i) <= 0) {
      len.at(0).at(i) += N;
    }
    len.at(0).at(i) += 1;
  }
  for (ll i = 0; i < power - 1; ++i) {
    for (ll j = 0; j < N; ++j) {
      len.at(i + 1).at(j) = len.at(i).at(d.at(i).at(j)) + len.at(i).at(j);
    }
  }
  // for (ll i = 0; i < power; ++i) {
  //   for (ll j = 0; j < N; ++j) {
  //     cout << len.at(i).at(j) << " ";
  //   }
  //   cout << "\n";
  // }
  ll now = 0;
  ll num = 0;
  K *= N;
  while (true) {
    bool update = false;
    for (ll i = power - 1; i >= 0; --i) {
      if (len.at(i).at(now) <= K) {
        update = true;
        K -= len.at(i).at(now);
        now = d.at(i).at(now);
        break;
      }
    }
    if (!update) {
      break;
    }
  }
  // cout << now << "\n";
  // cout << K << "\n";
  vector<ll> ans;
  for (ll i = 0; i < K; ++i) {
    ans.push_back(A.at(now + i));
  }
  ans = calc(ans);
  for (ll i = 0; i < ans.size(); ++i) {
    cout << ans.at(i) << " ";
  }
  cout << "\n";
}