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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<long long> vll;
typedef vector<pair<int, int>> vpi;
typedef vector<pair<long long, long long>> vpl;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
#define INF (int)(1e9)
#define MAXX 1.1529215e+18
#define inf 999999
#define EPS (1e-7)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i <= (int)(n); i++)
#define FOR(i, k, n) for (int i = (k); i < (int)(n); i++)
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.begin(), a.end(), greater<int>()
#define ROT(a) a.begin(), a.begin() + 1, a.end()
#define RROT(a) a.begin(), a.end() - 1, a.end()
#define PB push_back
#define MP make_pair
#define PI acos(-1.0)
#define sz(a) a.size()
const ll MOD = 1e9 + 7;
const int MAX = 10000000;
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> T gcd(T a, T b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
// cout << 'H' << endl;
/*--------------------------------------------*/

int main() {
  // cout << fixed << setprecision(10)
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N, K;
  cin >> N >> K;

  vector<ll> a(N);
  rep(i, N) {
    cin >> a[i];
    --a[i];
  }

  map<ll, ll> mp;
  // mp[0] = -1;

  vector<ll> loop;
  vector<ll> tmp;

  ll st = 0;
  ll siz = 0;
  ll now = 0;

  for (int i = 0; i < 2 * N; i++) {
    if (mp[a[now]]) {
      st = mp[a[now]] - 1;
      siz = i - st;
      // cout << st << siz << endl;
      break;
    } else {
      tmp.PB(a[now]);
      mp[a[now]] = i + 1;
      now = a[now];
      // cout << now << endl;
    }
  }
  for (int i = st; i < st + siz; i++) {
    loop.PB(tmp[i]);
  }

  cout << 1 + ((K > st ? (loop[(K - st - 1) % siz]) : tmp[K - 1])) << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<long long> vll;
typedef vector<pair<int, int>> vpi;
typedef vector<pair<long long, long long>> vpl;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
#define INF (int)(1e9)
#define MAXX 1.1529215e+18
#define inf 999999
#define EPS (1e-7)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i <= (int)(n); i++)
#define FOR(i, k, n) for (int i = (k); i < (int)(n); i++)
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.begin(), a.end(), greater<int>()
#define ROT(a) a.begin(), a.begin() + 1, a.end()
#define RROT(a) a.begin(), a.end() - 1, a.end()
#define PB push_back
#define MP make_pair
#define PI acos(-1.0)
#define sz(a) a.size()
const ll MOD = 1e9 + 7;
const int MAX = 10000000;
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> T gcd(T a, T b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
// cout << 'H' << endl;
/*--------------------------------------------*/

int main() {
  // cout << fixed << setprecision(10)
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N, K;
  cin >> N >> K;

  vector<ll> a(N);
  rep(i, N) {
    cin >> a[i];
    --a[i];
  }

  map<ll, ll> mp;
  // mp[0] = -1;

  vector<ll> loop;
  vector<ll> tmp;

  ll st = 0;
  ll siz = 0;
  ll now = 0;

  for (int i = 0; i < N; i++) {
    if (mp[a[now]]) {
      st = mp[a[now]] - 1;
      siz = i - st;
      // cout << st << siz << endl;
      break;
    } else {
      tmp.PB(a[now]);
      mp[a[now]] = i + 1;
      now = a[now];
      // cout << now << endl;
    }
  }
  for (int i = st; i < st + siz; i++) {
    loop.PB(tmp[i]);
  }

  cout << 1 + ((K > st ? (loop[(K - st - 1) % siz]) : loop[K])) << endl;
}