Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fastIO()                                                               \
  ;                                                                            \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define fr(i, a, b) for (lli i = a; i < b; i++)
#define f0(a, b) for (lli i = a; i < b; i++)
#define fn(b, a) for (lli i = b; i > a; i--)
#define dbg(x)                                                                 \
  ;                                                                            \
  cout << "\n" << #x << ": " << x;
#define ret return
#define pb push_back
#define pf push_front
#define mp make_pair
#define E9 1000000007
#define E5 100007
#define INF 1e18 + 7
#define PI 3.1415926
#define sq(a) (a) * (a)
#define fs first
#define sc second
#define el "\n"
#define all(a) a.begin(), a.end()
typedef vector<long long int> vll;
typedef vector<vector<long long int>> vvll;
typedef map<long long int, long long int> mll;
typedef pair<long long int, long long int> pll;
typedef long long int lli; //  e-18 to e18
void solve() {
  lli n, k;
  cin >> n >> k;
  vector<lli> a(n + 1);
  f0(1, n + 1) cin >> a[i];
  vll b;
  mll c;
  lli pos = 1, h = 1;
  while (c.find(pos) == c.end()) {
    c[pos] = h++;
    b.pb(pos);
    pos = a[pos];
  }
  if (k < c[pos]) {
    cout << b[k] << el;
  } else {
    k -= c[pos];
    vll d;
    f0(c[pos], b.size()) d.pb(b[i]);
    cout << d[k % d.size()] << el;
  }
}
int main() {
  fastIO();
  lli t(1);
  // cin>>t;
  while (t--)
    solve();
  ret 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fastIO()                                                               \
  ;                                                                            \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define fr(i, a, b) for (lli i = a; i < b; i++)
#define f0(a, b) for (lli i = a; i < b; i++)
#define fn(b, a) for (lli i = b; i > a; i--)
#define dbg(x)                                                                 \
  ;                                                                            \
  cout << "\n" << #x << ": " << x;
#define ret return
#define pb push_back
#define pf push_front
#define mp make_pair
#define E9 1000000007
#define E5 100007
#define INF 1e18 + 7
#define PI 3.1415926
#define sq(a) (a) * (a)
#define fs first
#define sc second
#define el "\n"
#define all(a) a.begin(), a.end()
typedef vector<long long int> vll;
typedef vector<vector<long long int>> vvll;
typedef map<long long int, long long int> mll;
typedef pair<long long int, long long int> pll;
typedef long long int lli; //  e-18 to e18
void solve() {
  lli n, k;
  cin >> n >> k;
  vector<lli> a(n + 1);
  f0(1, n + 1) cin >> a[i];
  vll b;
  mll c;
  lli pos = 1, h = 0;
  while (c.find(pos) == c.end()) {
    c[pos] = h++;
    b.pb(pos);
    pos = a[pos];
  }
  if (k < c[pos]) {
    cout << b[k] << el;
  } else {
    k -= c[pos];
    vll d;
    f0(c[pos], b.size()) d.pb(b[i]);
    cout << d[k % d.size()] << el;
  }
}
int main() {
  fastIO();
  lli t(1);
  // cin>>t;
  while (t--)
    solve();
  ret 0;
}