func_code_string: #include <bits/stdc++.h>   using namespace std;   using namespace std;   long long int **sparse;   void buildSparseTable(long long int arr[], long long int n,                         long long int **sparse) {     for (long long int i = 0; i < n; i++) {       sparse[i][0] = i;     }     for (long long int j = 1; pow(2, j) <= n; j++) {       for (long long int i = 0; i + pow(2, j) - 1 < n; i++) {         if (arr[sparse[i][j - 1]] >=             arr[sparse[i + (long long int)pow(2, j - 1)][j - 1]]) {           sparse[i][j] = sparse[i][j - 1];         } else {           sparse[i][j] = sparse[i + (long long int)pow(2, j - 1)][j - 1];         }       }     }   }   long long int query(long long int l, long long int r, long long int **sparse,                       long long int arr[]) {     long long int len = r - l + 1;     long long int k = (log(len) / log(2.0));     long long int m;     if (arr[sparse[l][k]] >= arr[sparse[l + len - (long long int)pow(2, k)][k]]) {       return sparse[l][k];     } else       return sparse[l + len - (long long int)pow(2, k)][k];   }   int main() {     long long int n;     cin >> n;     long long int arr[n];     sparse = new long long int *[n];     for (long long int i = 0; i < n; i++) {       sparse[i] = new long long int[(long long int)floor(log(n) / log(2.0)) + 1];     }     arr[n - 1] = n - 1;     for (long long int i = 0; i < n - 1; i++) {       cin >> arr[i];       arr[i]--;     }     buildSparseTable(arr, n, sparse);     long long int dp[n];     dp[n - 1] = 0;     long long int sum = 0;     for (long long int i = n - 2; i >= 0; i--) {       long long int m = query(i + 1, arr[i], sparse, arr);       dp[i] = dp[m] - (arr[i] - m) + n - 1 - i;       sum += dp[i];     }     cout << sum << endl;   }   