codeforces E. Trains and Statistic 线段树优化dp

我就是我 2022-07-24 08:16 313阅读 0赞

E. Trains and Statistic

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Input

The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — 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.

Output

Print the sum of ρi, j among all pairs of 1 ≤ i < j ≤ n.

Examples

input

  1. 4
  2. 4 4 4

output

  1. 6

input

  1. 5
  2. 2 3 5 5

output

  1. 17

Note

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.

题意: 懒得说了….

分析: 这题看的官方题解, 但是依然不懂状态转移方程,,,,(不要打我..), 只是学到了新的线段树姿势, 求l和r之间的最大值的下标位置… 等以后学到了新的姿势, 再来看这题.

官方题解:

Let the indexation will be from zero. So we should subtract one from all a**i. Also let a**n - 1 = n - 1.

dp**i is sum of shortests pathes from i to i + 1… n - 1.

dp**n - 1 = 0

dp**i = dp**m - (a**i - m) + n - i - 1 where m belongs to range from i + 1 to a**i and a**m is maximal. We can find m with segment tree or binary indexed tree or sparse table.

Now answer equals to sum of all dp**i.

  1. #include<bits/stdc++.h>
  2. #define inf 0x3f3f3f3f
  3. using namespace std;
  4. typedef long long ll;
  5. typedef pair<int,int> pii;
  6. const int N=100010,MOD=1e9+7;
  7. ll dp[N];
  8. int b[N];
  9. struct st
  10. {
  11. int l,r;
  12. pii mx;
  13. }a[N<<2];
  14. int n;
  15. void build(int l,int r,int i)
  16. {
  17. a[i].l=l,a[i].r=r;
  18. if(l==r){
  19. if(l==n){
  20. a[i].mx = pii(n,n);
  21. return;
  22. }
  23. scanf("%d",&a[i].mx.first);
  24. a[i].mx.first--;
  25. b[l-1]=a[i].mx.first;
  26. a[i].mx.second=l;
  27. return;
  28. }
  29. int mid=l+r>>1;
  30. build(l,mid,i<<1);
  31. build(mid+1,r,i<<1|1);
  32. a[i].mx = max(a[i<<1].mx,a[i<<1|1].mx);
  33. }
  34. pii query(int l,int r,int i)
  35. {
  36. if(a[i].l>=l && a[i].r<=r) return a[i].mx;
  37. if(a[i].l>r || a[i].r<l) return pii(-1,-1);
  38. return max(query(l,r,i<<1),query(l,r,i<<1|1));
  39. }
  40. int main()
  41. {
  42. cin>>n;
  43. build(1,n,1);
  44. ll ans=0;
  45. dp[n-1]=0;
  46. for(int i=n-2;i>=0;i--){
  47. int m = query(i+1+1,b[i]+1,1).second-1;
  48. dp[i] = dp[m] - (b[i]-m)+n-1-i;
  49. ans += dp[i];
  50. }
  51. cout<<ans<<endl;
  52. return 0;
  53. }

发表评论

表情:
评论列表 (有 0 条评论,313人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Codeforces 750E 线段DP

    题意:给你一个字符串,有两种操作:1:把某个位置的字符改变。2:询问l到r的子串最少需要删除多少个字符,使得这个子串含有2017子序列,并且没有2016子序列? 思路:线段树

    相关 Codeforces 735E 树形DP

    题意:给你一棵树,你需要在这棵树上选择一些点染成黑色,要求染色之后树中任意节点到离它最近的黑色节点的距离不超过m,问满足这种条件的染色方案有多少种? 思路:设dp\[x\]\