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

#include <algorithm>
#include <array>
#include <chrono>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
//<< setprecision(5)
using namespace std;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
#define dme                                                                    \\
  cout << -1 << endl;                                                          \\
  return 0
const int mod = 1000000007;
const int big = 1e9;
const long double pai = 3.141592653589793238462643383279502884197;
const long double eps = 1e-7;
template <class T, class U> void mineq(T &a, U b) {
  if (a > b) {
    a = b;
  }
}
template <class T, class U> void maxeq(T &a, U b) {
  if (a < b) {
    a = b;
  }
}
llint gcd(llint a, llint b) {
  if (a % b == 0) {
    return b;
  } else
    return gcd(b, a % b);
}
llint lcm(llint a, llint b) { return a / gcd(a, b) * b; }
int main(void) {
  int i, j, n;
  cin >> n;
  vector<int> p(n + 3);
  vector<int> ans(n + 3);
  vector<bool> ku(n + 3);
  p[0] = n + 2;
  for (i = 1; i <= n; i++) {
    for (j = i; j >= 0; j--) {
      ans[j] = 0;
      ku[j] = 0;
    }
    cin >> p[i];
    for (j = i; j >= 0; j--) {
      if (ku[j]) {
        ans[j]++;
      } else {
        ku[p[j]] = true;
      }
      ans[p[j]] += ans[j];
    }
    cout << ans[0] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <chrono>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
//<< setprecision(5)
using namespace std;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
#define dme                                                                    \\
  cout << -1 << endl;                                                          \\
  return 0
const int mod = 1000000007;
const int big = 1e9;
const long double pai = 3.141592653589793238462643383279502884197;
const long double eps = 1e-7;
template <class T, class U> void mineq(T &a, U b) {
  if (a > b) {
    a = b;
  }
}
template <class T, class U> void maxeq(T &a, U b) {
  if (a < b) {
    a = b;
  }
}
llint gcd(llint a, llint b) {
  if (a % b == 0) {
    return b;
  } else
    return gcd(b, a % b);
}
llint lcm(llint a, llint b) { return a / gcd(a, b) * b; }
int main(void) {
  int i, j, n;
  cin >> n;
  vector<int> p(n + 3);
  vector<int> ans(n + 1);
  vector<bool> ku(n + 1);
  p[0] = n + 2;
  for (i = 1; i <= n; i++) {
    for (j = i; j >= 0; j--) {
      ans[j] = 0;
      ku[j] = 0;
    }
    cin >> p[i];
    for (j = i; j >= 0; j--) {
      if (ku[j]) {
        ans[j]++;
      } else {
        ku[p[j]] = true;
      }
      ans[p[j]] += ans[j];
    }
    cout << ans[0] << endl;
  }
  return 0;
}