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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
// memo  fixed  setprecision(20);
using vvll = vector<vector<ll>>;
ll mod = 1e9 + 7;
/*"itob" int to "N"base */ template <typename TypeInt>
string itob(const TypeInt v, int base) {
  static const char table[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  string ret;
  static numeric_limits<TypeInt> t;
  TypeInt n = v;
  if (t.is_signed) {
    if (v < 0)
      n *= -1;
  }
  while (n >= base) {
    ret += table[n % base];
    n /= base;
  }
  ret += table[n];
  if (t.is_signed) {
    if (v < 0 && base == 10)
      ret += '-';
  }
  reverse(ret.begin(), ret.end());
  return ret;
}
/*"chmin" a = MAX*/ template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
/*"chmax" a = MIN*/ template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
/*"ctoi"  char to int*/ int ctoi(char c) { return c - '0'; }
/*"gcd"   MAX Euclidean */ ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
/*"lcm"   MIN*/ ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
/*"primecheck"If prime,return true.*/ bool primecheck(ll n) {
  if (n < 2)
    return false;
  else {
    for (int i = 2; i * i <= n; i++) {
      if (n % i == 0)
        return false;
    }
    return true;
  }
}
string reverserange(string s, ll a, ll b) {
  reverse(s.begin() + a - 1, s.begin() + b);
  return s;
}
ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n % 2 == 1) {
      res = res * a % mod;
    }
    a = a * a % mod;
    n /= 2;
  }
  return res;
}
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> retu(n);
  for (int i = 0; i < n; i++) {
    cin >> retu.at(i);
    retu.at(i)--;
  }
  map<ll, ll> ma;
  ll now = 0;
  ll next = 0;
  ll che;
  ll value;
  vector<ll> za;
  while (1) {
    if (ma.count(next)) {
      che = ma.at(next);
      value = next;
      za.push_back(next);
      break;
    }
    ma[next] = now;
    za.push_back(next);
    next = retu.at(next);
    now++;
  }
  if (k <= za.size()) {
    ll ans = za.at(k) + 1;
    cout << ans << endl;
    return 0;
  }
  if (che == 0) {
    ll d = za.size() - 1;
    k = k % d;
    ll ans = za.at(k) + 1;
    cout << ans << endl;
  } else {
    k -= che;
    vector<ll> dd;
    bool w = 0;
    for (int i = 0; i < za.size(); i++) {
      if (za.at(i) == value) {
        w = 1;
      }
      if (w) {
        dd.push_back(za.at(i));
      }
    }
    ll ddd = dd.size() - 1;
    if (k >= ddd) {
      k = k % ddd;
    }

    ll ans = dd.at(k) + 1;
    cout << ans << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
// memo  fixed  setprecision(20);
using vvll = vector<vector<ll>>;
ll mod = 1e9 + 7;
/*"itob" int to "N"base */ template <typename TypeInt>
string itob(const TypeInt v, int base) {
  static const char table[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  string ret;
  static numeric_limits<TypeInt> t;
  TypeInt n = v;
  if (t.is_signed) {
    if (v < 0)
      n *= -1;
  }
  while (n >= base) {
    ret += table[n % base];
    n /= base;
  }
  ret += table[n];
  if (t.is_signed) {
    if (v < 0 && base == 10)
      ret += '-';
  }
  reverse(ret.begin(), ret.end());
  return ret;
}
/*"chmin" a = MAX*/ template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
/*"chmax" a = MIN*/ template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
/*"ctoi"  char to int*/ int ctoi(char c) { return c - '0'; }
/*"gcd"   MAX Euclidean */ ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
/*"lcm"   MIN*/ ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
/*"primecheck"If prime,return true.*/ bool primecheck(ll n) {
  if (n < 2)
    return false;
  else {
    for (int i = 2; i * i <= n; i++) {
      if (n % i == 0)
        return false;
    }
    return true;
  }
}
string reverserange(string s, ll a, ll b) {
  reverse(s.begin() + a - 1, s.begin() + b);
  return s;
}
ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n % 2 == 1) {
      res = res * a % mod;
    }
    a = a * a % mod;
    n /= 2;
  }
  return res;
}
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> retu(n);
  for (int i = 0; i < n; i++) {
    cin >> retu.at(i);
    retu.at(i)--;
  }
  map<ll, ll> ma;
  ll now = 0;
  ll next = 0;
  ll che;
  ll value;
  vector<ll> za;
  while (1) {
    if (ma.count(next)) {
      che = ma.at(next);
      value = next;
      za.push_back(next);
      break;
    }
    ma[next] = now;
    za.push_back(next);
    next = retu.at(next);
    now++;
  }

  if (che == 0) {
    ll d = za.size() - 1;
    k = k % d;
    ll ans = za.at(k) + 1;
    cout << ans << endl;
  } else {
    if (k < che) {
      ll ans = za.at(k) + 1;
      cout << ans << endl;
    }
    k -= che;
    vector<ll> dd;
    bool w = 0;
    for (int i = 0; i < za.size(); i++) {
      if (za.at(i) == value) {
        w = 1;
      }
      if (w) {
        dd.push_back(za.at(i));
      }
    }
    ll ddd = dd.size() - 1;
    if (k >= ddd) {
      k = k % ddd;
    }

    ll ans = dd.at(k) + 1;
    cout << ans << endl;
  }
}