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

#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"
#define ordered_set                                                            \\
  tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,           \\
       tree_order_statistics_node_update>
using namespace std;
using namespace __gnu_pbds;
#define int long long int
#define pb push_back
#define vec vector<int>
#define fo(i, n) for (int i = 0; i < n; i++)
#define foo(i, a, b) for (int i = a; i < b; i++)
#define mp make_pair
#define deb(x) cout << #x << " --> " << x << endl;
#define deb1a(v)                                                               \\
  cout << #v << "--->" << endl;                                                \\
  for (auto it : v)                                                            \\
    cout << it << " ";                                                         \\
  cout << endl;
#define deb2a(v)                                                               \\
  cout << #v << "--->" << endl;                                                \\
  for (auto it : v) {                                                          \\
    for (auto it1 : it) {                                                      \\
      cout << it1 << " ";                                                      \\
    }                                                                          \\
    cout << endl;                                                              \\
  }
#define deb3a(v)                                                               \\
  cout << #v << "-->" << endl;                                                 \\
  for (auto it : v) {                                                          \\
    cout << it.fi << " " << it.se << endl;                                     \\
  }
#define mod 29996224275833
#define so(v) sort(v.begin(), v.end())
#define fi first
#define se second
#define db1(x) cout << #x << "=" << x << endl
#define db2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z)                                                           \\
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
int power(int x, unsigned int y, int p) {
  int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
// int mI(int a, int m)  {return power(a, m - 2, m);}
// vector<vector<int>> dir = {{1, 0}, {-1, 0}, {0, 1}, {0,
// -1},{1,1},{1,-1},{-1,1},{-1,-1}};

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  fo(i, n) cin >> v[i];
  fo(i, n) v[i]--;
  map<int, int> m;
  int val = 0;
  vector<int> ans;
  while (m[val] == 0) {
    m[val] = 1;
    ans.pb(val);
    val = v[val];
  }
  int in = 0;
  fo(i, ans.size()) {
    if (ans[i] == val)
      in = i;
  }
  // deb(in);
  if (k < ans.size()) {
    cout << ans[k] + 1;
  } else {
    k -= (ans.size() - 1);
    int remain = k % (ans.size() - in);
    if (remain == 0) {
      remain = ans.size() - in;
    }
    cout << ans[in + remain - 1] + 1;
  }
}


Error: -8

Fixed submission:

#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"
#define ordered_set                                                            \\
  tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,           \\
       tree_order_statistics_node_update>
using namespace std;
using namespace __gnu_pbds;
#define int long long int
#define pb push_back
#define vec vector<int>
#define fo(i, n) for (int i = 0; i < n; i++)
#define foo(i, a, b) for (int i = a; i < b; i++)
#define mp make_pair
#define deb(x) cout << #x << " --> " << x << endl;
#define deb1a(v)                                                               \\
  cout << #v << "--->" << endl;                                                \\
  for (auto it : v)                                                            \\
    cout << it << " ";                                                         \\
  cout << endl;
#define deb2a(v)                                                               \\
  cout << #v << "--->" << endl;                                                \\
  for (auto it : v) {                                                          \\
    for (auto it1 : it) {                                                      \\
      cout << it1 << " ";                                                      \\
    }                                                                          \\
    cout << endl;                                                              \\
  }
#define deb3a(v)                                                               \\
  cout << #v << "-->" << endl;                                                 \\
  for (auto it : v) {                                                          \\
    cout << it.fi << " " << it.se << endl;                                     \\
  }
#define mod 29996224275833
#define so(v) sort(v.begin(), v.end())
#define fi first
#define se second
#define db1(x) cout << #x << "=" << x << endl
#define db2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z)                                                           \\
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
int power(int x, unsigned int y, int p) {
  int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
// int mI(int a, int m)  {return power(a, m - 2, m);}
// vector<vector<int>> dir = {{1, 0}, {-1, 0}, {0, 1}, {0,
// -1},{1,1},{1,-1},{-1,1},{-1,-1}};

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  fo(i, n) cin >> v[i];
  fo(i, n) v[i]--;
  map<int, int> m;
  int val = 0;
  vector<int> ans;
  while (m[val] == 0) {
    m[val] = 1;
    ans.pb(val);
    val = v[val];
  }
  int in = 0;
  fo(i, n) {
    if (ans[i] == val)
      in = i;
  }
  // deb(in);
  if (k <= in) {
    cout << ans[k];
  } else {
    k -= in;
    int remain = k % (ans.size() - in);
    if (remain == 0) {
      remain = ans.size() - in;
    }
    // deb1a(ans);
    // deb(remain);
    cout << v[ans[in + remain - 1]] + 1;
  }
}