2324.Trains and Statistic

Time Limit: 1s Memory Limit: 256MB

Vasya commutes by train every day. There are n train stations in the city, and at the i-th station it's possible to buy only tickets to stations from i+1 to ai inclusive. No tickets are sold at the last station.

Let ρi,j be the minimum number of tickets one needs to buy in order to get from stations i to station j. As Vasya is fond of different useless statistic he asks you to compute the sum of all values ρi,j among all pairs 1 \le i \lt j \le n.

Input Format(From the terminal/stdin)

The first line of the input contains a single integer n (2 \le n \le 100000)- the number of stations.

The second line contains n-1 integer ai (i+1 \le ai \le n), the i-th of them means that at the i-th station one may buy tickets to each station from i+1 to ai inclusive.

Output Format(To the terminal/stdout)

Print the sum of ρi,j among all pairs of 1 \le i \lt j \le n.

Sample Input 1

Copy
4
4 4 4
 \n
 · · \n

Sample Output 1

Copy
6
 \n

Sample Input 2

Copy
5
2 3 5 5
 \n
 · · · \n

Sample Output 2

Copy
17
  \n

Hints

In the first sample it's possible to get from any station to any other (with greater index) using only one ticket. The total number of pairs is 6, so the answer is also 6.

Consider the second sample:
ρ1,2=1 ρ1,3=2 ρ1,4=3 ρ1,5=3 ρ2,3=1 ρ2,4=2 ρ2,5=2 ρ3,4=1 ρ3,5=1 ρ4,5=1
Thus the answer equals 1+2+3+3+1+2+2+1+1+1=17.

Submit

请先 登录

© 2025 FAQs