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 ll long long
#define ld long double
#define mod ((int)1e9 + 7)
#define maxx ((int)2e6 + 6)
#define pb push_back
#define ins insert
#define F first
#define S second
#define ins insert
#define pii pair<int, int>
#define lb lower_bound
#define ub upper_bound
#define usefile                                                                \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("ou(tp)ut.txt", "w", stdout);
#define fast                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define test                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
int exp(int x, int y, int p) {
  int res = 1;
  while (y) {
    if (y % 2)
      res = (res * x % p) % p;
    x = (x * x) % p;
    y /= 2;
  }
  return res;
}
int expo(int x, int y) {
  int res = 1;
  while (y) {
    if (y % 2)
      res = (res * x % mod) % mod;
    x = (x * x) % mod;
    y /= 2;
  }
  return res;
}
int add(int a, int b) { return (a % mod + b % mod + mod) % mod; }
int sub(int a, int b) { return (a % mod - b % mod + mod) % mod; }
int mul(int a, int b) { return ((a % mod) * (b % mod) + mod) % mod; }
int inv(int x) { return expo(x, mod - 2); }
#define time__(d)                                                              \\
  for (long blockTime = 0;                                                     \\
       (blockTime == 0 ? (blockTime = clock()) != 0 : false);                  \\
       debug("%s time : %.4fs", d,                                             \\
             (double)(clock() - blockTime) / CLOCKS_PER_SEC))
// int sv[2000006]={0};
// int fact[1000006]={0};
// void facto() {fact[0]=1;fact[1]=1;for(int
// i=2;i<1000006;i++)fact[i]=(fact[i-1]*i)%mod;} void sve()
// {sv[0]=1;sv[1]=1;for(int i=2;i*i<maxx;i++){if(!sv[i]){for(int
// j=i*i;j<maxx;j+=i)sv[j]=1;}}}//0 hai to prime int ncr(int n,int r) {int
// res=1; res=fact[n]; res=(res*(expo(fact[r],mod-2)))%mod;
// res=(res*(expo(fact[n-r],mod-2)))%mod; return res;} int npr(int n,int r) {int
// res=1; res=fact[n]; res=(res*(expo(fact[n-r],mod-2)))%mod; return res;} using
// namespace __gnu_pbds; #define p_set tree< pair<int,int> ,  null_type ,
// less<pair<int,int>> ,  rb_tree_tag ,  tree_order_statistics_node_update>
// #define i_set tree< int ,  null_type ,  less< int > ,  rb_tree_tag ,
// tree_order_statistics_node_update>
using namespace std;
// #define N 1000006
vector<string> vec_splitter(string s) {
  s += ',';
  vector<string> res;
  while (!s.empty()) {
    res.push_back(s.substr(0, s.find(',')));
    s = s.substr(s.find(',') + 1);
  }
  return res;
}
void debug_out(vector<string> __attribute__((unused)) args,
               __attribute__((unused)) int idx,
               __attribute__((unused)) int LINE_NUM) {
  cerr << endl;
}
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
  if (idx > 0)
    cerr << ", ";
  else
    cerr << "Line(" << LINE_NUM << ") ";
  stringstream ss;
  ss << H;
  cerr << args[idx] << " = " << ss.str();
  debug_out(args, idx + 1, LINE_NUM, T...);
}
#ifdef XOX
#define debug(...)                                                             \\
  debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define debug(...) 42
#endif

int main() {
  map<ll, ll> prev, moves;
  ll n, k;
  cin >> n >> k;
  vector<ll> v(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> v[i];

  prev[1] = 1;
  moves[1] = 1;
  ll next = 1;
  ll ans = 0;
  if (k <= n) {
    for (int i = 1; i <= k; i++) {
      ans = v[next];
      next = v[next];
    }
    cout << ans << endl;
  }

  else {
    int i;
    ll x = 0;

    for (i = 2; i <= n + 1; i++) {

      if (!prev[v[next]]) {
        prev[v[next]] = i;
        moves[i] = v[next];
        //  cout<<v[next]<<endl;
      }

      else {
        x = prev[v[next]];
        break;
      }
      next = v[next];
    }
    // cout<<(k-i+2)<<" "<<i-moves[x]<<endl;
    ans = (k - i + 2) % (i - x);

    //   cout<<i<<" "<<moves[i]<<endl;
    // cout<<i<<" "<<moves[x]<<endl;
    // cout<<i-moves[x]<<endl;

    //   cout<<k-i<<" "<<
    if (ans == 0)
      ans = i - x;
    // out<<moves[i]<<endl;
    cout << moves[x + ans - 1] << "\\n";
  }
}


Error: 0

Fixed submission:


#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define mod ((int)1e9 + 7)
#define maxx ((int)2e6 + 6)
#define pb push_back
#define ins insert
#define F first
#define S second
#define ins insert
#define pii pair<int, int>
#define lb lower_bound
#define ub upper_bound
#define usefile                                                                \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("ou(tp)ut.txt", "w", stdout);
#define fast                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define test                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
int exp(int x, int y, int p) {
  int res = 1;
  while (y) {
    if (y % 2)
      res = (res * x % p) % p;
    x = (x * x) % p;
    y /= 2;
  }
  return res;
}
int expo(int x, int y) {
  int res = 1;
  while (y) {
    if (y % 2)
      res = (res * x % mod) % mod;
    x = (x * x) % mod;
    y /= 2;
  }
  return res;
}
int add(int a, int b) { return (a % mod + b % mod + mod) % mod; }
int sub(int a, int b) { return (a % mod - b % mod + mod) % mod; }
int mul(int a, int b) { return ((a % mod) * (b % mod) + mod) % mod; }
int inv(int x) { return expo(x, mod - 2); }
#define time__(d)                                                              \\
  for (long blockTime = 0;                                                     \\
       (blockTime == 0 ? (blockTime = clock()) != 0 : false);                  \\
       debug("%s time : %.4fs", d,                                             \\
             (double)(clock() - blockTime) / CLOCKS_PER_SEC))
// int sv[2000006]={0};
// int fact[1000006]={0};
// void facto() {fact[0]=1;fact[1]=1;for(int
// i=2;i<1000006;i++)fact[i]=(fact[i-1]*i)%mod;} void sve()
// {sv[0]=1;sv[1]=1;for(int i=2;i*i<maxx;i++){if(!sv[i]){for(int
// j=i*i;j<maxx;j+=i)sv[j]=1;}}}//0 hai to prime int ncr(int n,int r) {int
// res=1; res=fact[n]; res=(res*(expo(fact[r],mod-2)))%mod;
// res=(res*(expo(fact[n-r],mod-2)))%mod; return res;} int npr(int n,int r) {int
// res=1; res=fact[n]; res=(res*(expo(fact[n-r],mod-2)))%mod; return res;} using
// namespace __gnu_pbds; #define p_set tree< pair<int,int> ,  null_type ,
// less<pair<int,int>> ,  rb_tree_tag ,  tree_order_statistics_node_update>
// #define i_set tree< int ,  null_type ,  less< int > ,  rb_tree_tag ,
// tree_order_statistics_node_update>
using namespace std;
// #define N 1000006
vector<string> vec_splitter(string s) {
  s += ',';
  vector<string> res;
  while (!s.empty()) {
    res.push_back(s.substr(0, s.find(',')));
    s = s.substr(s.find(',') + 1);
  }
  return res;
}
void debug_out(vector<string> __attribute__((unused)) args,
               __attribute__((unused)) int idx,
               __attribute__((unused)) int LINE_NUM) {
  cerr << endl;
}
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
  if (idx > 0)
    cerr << ", ";
  else
    cerr << "Line(" << LINE_NUM << ") ";
  stringstream ss;
  ss << H;
  cerr << args[idx] << " = " << ss.str();
  debug_out(args, idx + 1, LINE_NUM, T...);
}
#ifdef XOX
#define debug(...)                                                             \\
  debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define debug(...) 42
#endif

int main() {
  map<ll, ll> m;
  ll n, k;
  cin >> n >> k;
  vector<ll> v(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> v[i];
  m[1] = 1;
  ll next = 1;
  ll interval = 0;
  ll l = 0;
  ll temp = 0;
  bool flag = false;
  for (int i = 2; i <= n + 1 && i <= k; i++) {
    if (m[v[next]]) {
      interval = i - m[v[next]];
      l = i;
      temp = next;
      flag = true;
      break;

    }

    else {
      m[v[next]] = i;
    }
    next = v[next];
  }
  ll left = (k + 1) - l + 1;
  //   cout<<l<<endl;
  //   cout<<interval<<endl;
  ll ans = left % interval;
  // cout<<next<<" "<<temp<<endl;
  if (ans == 0)
    ans = interval;
  ll neext = temp;
  // cout<<next<<endl;

  for (int i = 1; i <= ans; i++) {
    neext = v[neext];
  }
  if (!flag && k <= n)
    cout << next << endl;
  else
    cout << neext << endl;
}