Problem p02684 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC optimize(3,"Ofast","inline")
// #pragma GCC optimize(2)
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
// #define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21,
// stdin), p1 == p2) ? EOF : *p1++) char buf[(1 << 21) + 1], * p1 = buf, * p2 =
// buf; #define int long long
#define lowbit(x) (x & (-x))
#define lson root << 1, l, mid
#define rson root << 1 | 1, mid + 1, r
#define pb push_back
typedef unsigned long long ull;
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll, ll> pll;
typedef std::vector<ll> VI;
#define rep(i, a, n) for (int i = a; i < n; ++i)
#define SZ(x) ((int)((x).size()))
#define bug puts("BUG")
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;
const double eps = 1e-6;
template <class T> inline void read(T &x) {
  int sign = 1;
  char c = getchar();
  x = 0;
  while (c > '9' || c < '0') {
    if (c == '-')
      sign = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x *= sign;
}
#ifdef LOCAL
FILE *_INPUT = freopen("input.txt", "r", stdin);
// FILE* _OUTPUT=freopen("output.txt", "w", stdout);
#endif
using namespace std;
const int maxn = 1e5 + 10;
const int DEG = 64;
ll des[maxn][DEG];
int main() {
  ll n, v, k;
  read(n), read(k);
  for (int i = 1; i <= n; ++i) {
    read(des[i][0]);
  }
  for (int i = 1; i < DEG; ++i) {
    for (int j = 1; j <= n; ++j) {
      des[j][i] = des[des[j][i - 1]][i - 1];
    }
  }
  int cur = 1;
  for (int i = 0; k; k >>= 1, ++i) {
    if (k & 1)
      cur = des[cur][i];
  }
  printf("%lld\n", cur);
}

Fixed submission:
// #pragma GCC optimize(3,"Ofast","inline")
// #pragma GCC optimize(2)
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
// #define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21,
// stdin), p1 == p2) ? EOF : *p1++) char buf[(1 << 21) + 1], * p1 = buf, * p2 =
// buf; #define int long long
#define lowbit(x) (x & (-x))
#define lson root << 1, l, mid
#define rson root << 1 | 1, mid + 1, r
#define pb push_back
typedef unsigned long long ull;
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll, ll> pll;
typedef std::vector<ll> VI;
#define rep(i, a, n) for (int i = a; i < n; ++i)
#define SZ(x) ((int)((x).size()))
#define bug puts("BUG")
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;
const double eps = 1e-6;
template <class T> inline void read(T &x) {
  int sign = 1;
  char c = getchar();
  x = 0;
  while (c > '9' || c < '0') {
    if (c == '-')
      sign = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x *= sign;
}
#ifdef LOCAL
FILE *_INPUT = freopen("input.txt", "r", stdin);
// FILE* _OUTPUT=freopen("output.txt", "w", stdout);
#endif
using namespace std;
const int maxn = 2e5 + 10;
const int DEG = 64;
ll des[maxn][DEG];
int main() {
  ll n, v, k;
  read(n), read(k);
  for (int i = 1; i <= n; ++i) {
    read(des[i][0]);
  }
  for (int i = 1; i < DEG; ++i) {
    for (int j = 1; j <= n; ++j) {
      des[j][i] = des[des[j][i - 1]][i - 1];
    }
  }
  int cur = 1;
  for (int i = 0; k; k >>= 1, ++i) {
    if (k & 1)
      cur = des[cur][i];
  }
  printf("%lld\n", cur);
}