Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

// #pragma comment(linker, "/STACK:16777216")
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("unswitch-loops")
// #pragma GCC
// target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,abm,mmx,avx,avx2,popcnt,tune=native")

using namespace std;

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

#define ll long long
#define ull unsigned long long
// #define double long double
#define int ll
// #define int ull
// #define int short
// #define int unsigned short
// #define int unsigned int
#define mp make_pair
#define F first
#define S second
#define pii pair<int, int>
#define hash hash228
const int SZ = 2e5 + 4;
const double EPS = 1e-7;
const int mod = 1e9 + 7;
const int hashpow = 2;
const int inf = 2e9;

struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};

int n, k;
int a[SZ][61];

inline void read() {
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> a[i][0];
    --a[i][0];
  }
}

int get(int k) {
  int ans = 0;
  int t = 0;
  while (k) {
    if (k & 1) {
      ans = a[ans][t];
    }
    k >>= 1;
    ++t;
  }

  return ans;
}

inline void solve() {
  for (int j = 1; j < 61; ++j) {
    for (int i = 0; i < n; ++i) {
      a[i][j] = a[a[i][j - 1]][j - 1];
    }
  }

  cout << get(k) + 1 << "\\n";
}

signed main() {

#ifdef __LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#else
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#endif

  //    int t;
  //    cin >> t;
  //    for (int i = 0; i < t; ++i) {
  read();
  solve();
  //    }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

// #pragma comment(linker, "/STACK:16777216")
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("unswitch-loops")
// #pragma GCC
// target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,abm,mmx,avx,avx2,popcnt,tune=native")

using namespace std;

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

#define ll long long
#define ull unsigned long long
// #define double long double
#define int ll
// #define int ull
// #define int short
// #define int unsigned short
// #define int unsigned int
#define mp make_pair
#define F first
#define S second
#define pii pair<int, int>
#define hash hash228
const int SZ = 12 + 4;
const double EPS = 1e-7;
const int mod = 1e9 + 7;
const int hashpow = 2;
const int inf = 2e9;

struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};

int n, k;
int a[SZ][61];

inline void read() {
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> a[i][0];
    --a[i][0];
  }
}

int get(int k) {
  int ans = 0;
  int t = 0;
  while (k) {
    if (k & 1) {
      ans = a[ans][t];
    }
    k >>= 1;
    ++t;
  }

  return ans;
}

inline void solve() {
  for (int j = 1; j < 61; ++j) {
    for (int i = 0; i < n; ++i) {
      a[i][j] = a[a[i][j - 1]][j - 1];
    }
  }

  cout << get(k) + 1 << "\\n";
}

signed main() {

#ifdef __LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#else
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#endif

  //    int t;
  //    cin >> t;
  //    for (int i = 0; i < t; ++i) {
  read();
  solve();
  //    }

  return 0;
}