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

// Author : Sukarna Paul

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#define pi acos(-1)
#define pb push_back
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll int64_t
#define MAX 300005
#define INF 0x3f3f3f3f
template <class T> inline T bigmod(T p, T e, T M) {
  ll ret = 1LL;
  for (; e > 0LL; e >>= 1LL) {
    if (e & 1LL)
      ret = (ret * p) % M;
    p = (p * p) % M;
  }
  return (T)ret;
}
template <class T> inline T modinverse(T a, T M) {
  return bigmod(a, M - 2, M);
} // M is prime}

using namespace std;
using namespace __gnu_pbds;

typedef trie<string, null_type, trie_string_access_traits<>, pat_trie_tag,
             trie_prefix_search_node_update>
    pref_trie;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_multiset;

int arr[200005];
vector<int> vec;
int main() {
  IOS
      // freopen("output.txt","w",stdout);
      int n;
  cin >> n;

  ll k;
  cin >> k;

  for (int i = 1; i <= n; i++)
    cin >> arr[i];

  vec.pb(1);
  map<int, int> mark;
  mark[1] = vec.size();
  int cur = 1;
  for (int i = 0; i < n; i++) {
    cur = arr[cur];
    if (mark[cur] > 0) {
      // cout<<mark[cur]<<"\\n";
      vector<int> ans;
      for (int j = mark[cur] - 1; j < vec.size(); j++) {
        ans.pb(vec[j]);
      }
      ll sz = ans.size();
      ll v = vec.size();
      k -= (v - sz);
      if (k <= 0) {
        k += (v - sz);
        // cout<<"hell";
        cout << vec[k] << "\\n";
        return 0;
      }
      // cout<<k<<" "<<sz<<"\\n";
      k %= sz;
      cout << ans[k] << "\\n";
      return 0;
    }
    vec.pb(cur);
    mark[cur] = vec.size();
  }
}


Error: 0

Fixed submission:

// Author : Sukarna Paul

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#define pi acos(-1)
#define pb push_back
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
#define MAX 300005
#define INF 0x3f3f3f3f
template <class T> inline T bigmod(T p, T e, T M) {
  ll ret = 1LL;
  for (; e > 0LL; e >>= 1LL) {
    if (e & 1LL)
      ret = (ret * p) % M;
    p = (p * p) % M;
  }
  return (T)ret;
}
template <class T> inline T modinverse(T a, T M) {
  return bigmod(a, M - 2, M);
} // M is prime}

using namespace std;
using namespace __gnu_pbds;

typedef trie<string, null_type, trie_string_access_traits<>, pat_trie_tag,
             trie_prefix_search_node_update>
    pref_trie;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_multiset;

int arr[200005];
int main() {
  IOS
      // freopen("output.txt","w",stdout);
      int n;
  cin >> n;

  ll k;
  cin >> k;

  for (int i = 1; i <= n; i++)
    cin >> arr[i];

  vector<int> vec;
  vec.pb(1);
  map<int, int> mark;
  mark[1] = vec.size();
  int cur = 1;
  for (int i = 0; i < n; i++) {
    cur = arr[cur];
    if (mark[cur] > 0) {
      // cout<<mark[cur]<<"\\n";
      vector<int> ans;
      for (int j = mark[cur] - 1; j < vec.size(); j++) {
        ans.pb(vec[j]);
      }
      ll sz = ans.size();
      ll v = vec.size();
      k -= v - sz;
      k %= sz;
      cout << ans[k] << "\\n";
      return 0;
    }
    vec.pb(cur);
    mark[cur] = vec.size();
  }
}