CF675E.Trains and Statistic

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

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

输入格式

The first line of the input contains a single integer nn ( 2<=n<=1000002<=n<=100000 ) — the number of stations.

The second line contains n1n-1 integer aia_{i} ( i+1<=ai<=ni+1<=a_{i}<=n ), the ii -th of them means that at the ii -th station one may buy tickets to each station from i+1i+1 to aia_{i} inclusive.

输出格式

Print the sum of ρi,jρ_{i,j} among all pairs of 1<=i<j<=n .

输入输出样例

  • 输入#1

    4
    4 4 4
    

    输出#1

    6
    
  • 输入#2

    5
    2 3 5 5
    

    输出#2

    17
    

说明/提示

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 66 , so the answer is also 66 .

Consider the second sample:

  • ρ1,2=1ρ_{1,2}=1
  • ρ1,3=2ρ_{1,3}=2
  • ρ1,4=3ρ_{1,4}=3
  • ρ1,5=3ρ_{1,5}=3
  • ρ2,3=1ρ_{2,3}=1
  • ρ2,4=2ρ_{2,4}=2
  • ρ2,5=2ρ_{2,5}=2
  • ρ3,4=1ρ_{3,4}=1
  • ρ3,5=1ρ_{3,5}=1
  • ρ4,5=1ρ_{4,5}=1

Thus the answer equals 1+2+3+3+1+2+2+1+1+1=171+2+3+3+1+2+2+1+1+1=17 .

首页