Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
// #include <boost/array.hpp>
// #include <boost/range/numeric.hpp>
// #include <boost/range/algorithm.hpp>
// #include <boost/multiprecision/cpp_int.hpp>

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;
// using namespace boost::multiprecision;

constexpr long long INF = LLONG_MAX;
constexpr long long MOD = 1000000007;
constexpr long double PI = M_PI;

#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); ++i)
#define RFOR(i, n, r) for (ll i = (ll)(n - 1); i >= r; --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, n, 0)
#define ALL(x) x.begin(), x.end()
#define CALL(x) x.cbegin(), x.cend()
#define RALL(x) x.rbegin(), x.rend()
#define CRALL(x) x.crbegin(), x.crend()

#define cout cout << fixed << setprecision(12)
#define dbglf cerr << '\\n'

#define ACCM(arr) accumulate(ALL(arr), 0LL)
#define Yes(ex) cout << ((ex) ? "Yes" : "No") << endl
#define YES(ex) cout << ((ex) ? "YES" : "NO") << endl

using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using vi = vector<ll>;
using vvi = vector<vi>;
using vp = vector<pair<ll, ll>>;
using vs = vector<string>;
using vc = vector<char>;
using lst = list<ll>;
using pq = priority_queue<ll>;

template <typename T> inline void veccin(vector<T> &v) {
  for (auto &&e : v) {
    cin >> e;
  }
}

template <typename T> inline void dbg(T a) { cerr << a << ' '; }

ll n, m, k;
ll ans = 0, sum = 0, cnt = 0;
// ld d;
string s /*1, s2, s3*/;
char c /*1, c2, c3*/;
bool flag = false;

/*--------------------template--------------------*/

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> n >> k;
  vi a(n), tst, b(n, 0);
  veccin(a);
  ll i = 1;
  while (k--) {
    // dbg(i);
    tst.push_back(i);
    b[i - 1]++;
    i = a[i - 1];
    cnt++;
    // auto it = find(ALL(tst),i);
    if (b[i - 1]) {
      dbg(cnt);
      auto it = find(ALL(tst), i);
      sum = distance(tst.begin(), it);
      cnt -= sum;
      break;
    }
    if (cnt == n) {
      // cout << tst
    }
  }
  // dbg(k);
  if (k == -1) {
    cout << i << endl;
    return 0;
  }
  // for(auto&&e:tst){dbg(e);}
  /*dbg(cnt);
  dbglf;
  dbg(k);*/
  k %= cnt;
  /*dbg(k);
  dbglf;
  for(auto&&e:tst){dbg(e);}*/
  tst.erase(tst.begin(), tst.begin() + sum);
  cout << tst[(k % cnt)] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// #include <boost/array.hpp>
// #include <boost/range/numeric.hpp>
// #include <boost/range/algorithm.hpp>
// #include <boost/multiprecision/cpp_int.hpp>

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;
// using namespace boost::multiprecision;

constexpr long long INF = LLONG_MAX;
constexpr long long MOD = 1000000007;
constexpr long double PI = M_PI;

#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); ++i)
#define RFOR(i, n, r) for (ll i = (ll)(n - 1); i >= r; --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, n, 0)
#define ALL(x) x.begin(), x.end()
#define CALL(x) x.cbegin(), x.cend()
#define RALL(x) x.rbegin(), x.rend()
#define CRALL(x) x.crbegin(), x.crend()

#define cout cout << fixed << setprecision(12)
#define dbglf cerr << '\\n'

#define ACCM(arr) accumulate(ALL(arr), 0LL)
#define Yes(ex) cout << ((ex) ? "Yes" : "No") << endl
#define YES(ex) cout << ((ex) ? "YES" : "NO") << endl

using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using vi = vector<ll>;
using vvi = vector<vi>;
using vp = vector<pair<ll, ll>>;
using vs = vector<string>;
using vc = vector<char>;
using lst = list<ll>;
using pq = priority_queue<ll>;

template <typename T> inline void veccin(vector<T> &v) {
  for (auto &&e : v) {
    cin >> e;
  }
}

template <typename T> inline void dbg(T a) { cerr << a << ' '; }

ll n, m, k;
ll ans = 0, sum = 0, cnt = 0;
// ld d;
string s /*1, s2, s3*/;
char c /*1, c2, c3*/;
bool flag = false;

/*--------------------template--------------------*/

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> n >> k;
  vi a(n), tst;
  veccin(a);
  ll i = 1;
  while (k--) {
    // dbg(i);
    tst.push_back(i);
    i = a[i - 1];
    cnt++;
    auto it = find(ALL(tst), i);
    if (it != tst.end()) {
      dbg(cnt);
      sum = distance(tst.begin(), it);
      cnt -= sum;
      break;
    }
  }
  // dbg(k);
  if (k == -1) {
    cout << i << endl;
    return 0;
  }
  // for(auto&&e:tst){dbg(e);}
  /*dbg(cnt);
  dbglf;
  dbg(k);*/
  k %= cnt;
  /*dbg(k);
  dbglf;
  for(auto&&e:tst){dbg(e);}*/
  tst.erase(tst.begin(), tst.begin() + sum);
  cout << tst[(k % cnt)] << endl;
}