A10426 | Trains and Statistic
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 $a_{i}$ 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<=i<j<=n$ .
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<=i<j<=n$ .
输入格式
The first line of the input contains a single integer $n$ ( $2<=n<=100000$ ) — the number of stations.
The second line contains $n-1$ integer $a_{i}$ ( $i+1<=a_{i}<=n$ ), the $i$ -th of them means that at the $i$ -th station one may buy tickets to each station from $i+1$ to $a_{i}$ inclusive.
The second line contains $n-1$ integer $a_{i}$ ( $i+1<=a_{i}<=n$ ), the $i$ -th of them means that at the $i$ -th station one may buy tickets to each station from $i+1$ to $a_{i}$ inclusive.
输出格式
Print the sum of $ρ_{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 $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$ .
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$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted