E. Trains and Statistic

题目连接:

http://www.codeforces.com/contest/675/problem/E

Description

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 ≤ 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 ai (i + 1 ≤ ai ≤ 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

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

Sample Input

4

4 4 4

Sample Output

6

Hint

题意

你可以从第i个城市买的从i到[i+1,a[i]]的车票,现在Pij表示从i到j的最小车票花费

现在让你求sigma p[i][j]

题解:

考虑 dp[i]表示从i到[i+1,n]的p[i][j]和

那么如果j<=a[i]的话,就+=1,否则就贪心找到[i+1,a[i]]里面a[i]最大的那个车站,转移到这个车站去

dp[i][j] = dp[m][j]+1,这样贪心肯定是最小的

然后根据这个莽一波dp就好了

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
typedef pair<int,int> SgTreeDataType;
struct treenode
{
int L , R ;
SgTreeDataType sum;
void update(int v)
{
sum=make_pair(v,L);
}
}; treenode tree[maxn*4]; inline void push_down(int o)
{ } inline void push_up(int o)
{
if(tree[2*o].sum.first<=tree[2*o+1].sum.first)
tree[o].sum.second=tree[2*o+1].sum.second;
else
tree[o].sum.second=tree[2*o].sum.second;
tree[o].sum.first = max(tree[2*o].sum.first,tree[2*o+1].sum.first);
} inline void build_tree(int L , int R , int o)
{
tree[o].L = L , tree[o].R = R,tree[o].sum = make_pair(0,0);
if (R > L)
{
int mid = (L+R) >> 1;
build_tree(L,mid,o*2);
build_tree(mid+1,R,o*2+1);
}
} inline void update(int QL,int QR,int v,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) tree[o].update(v);
else
{
push_down(o);
int mid = (L+R)>>1;
if (QL <= mid) update(QL,QR,v,o*2);
if (QR > mid) update(QL,QR,v,o*2+1);
push_up(o);
}
} inline pair<int,int> query(int QL,int QR,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) return tree[o].sum;
else
{
push_down(o);
int mid = (L+R)>>1;
SgTreeDataType res = make_pair(-1,0);
if (QL <= mid)
{
pair<int,int> tmp = query(QL,QR,2*o);
if(res.first<=tmp.first)
res=tmp;
}
if (QR > mid)
{
pair<int,int> tmp = query(QL,QR,2*o+1);
if(res.first<=tmp.first)
res=tmp;
}
push_up(o);
return res;
}
} long long dp[maxn];
int a[maxn];
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n-1;i++)scanf("%d",&a[i]);
a[n]=n;
build_tree(1,n,1);
for(int i=1;i<=n;i++)update(i,i,a[i],1);
long long ans = 0;
for(int i=n-1;i>=1;i--)
{
pair<int,int> tmp = query(i+1,a[i],1);
int m=tmp.second;
dp[i]=dp[m]-(a[i]-m)+n-i;
ans+=dp[i];
}
cout<<ans<<endl;
}

Codeforces Round #353 (Div. 2) E. Trains and Statistic dp 贪心的更多相关文章

  1. Codeforces Round #353 (Div. 2) E. Trains and Statistic 线段树+dp

    题目链接: http://www.codeforces.com/contest/675/problem/E 题意: 对于第i个站,它与i+1到a[i]的站有路相连,先在求所有站点i到站点j的最短距离之 ...

  2. Codeforces Round #288 (Div. 2) E. Arthur and Brackets [dp 贪心]

    E. Arthur and Brackets time limit per test 2 seconds memory limit per test 128 megabytes input stand ...

  3. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

  4. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

  5. Codeforces Round #353 (Div. 2)

    数学 A - Infinite Sequence 等差数列,公差是0的时候特判 #include <bits/stdc++.h> typedef long long ll; const i ...

  6. Codeforces Round #353 (Div. 2) C Money Transfers

    题目链接: http://www.codeforces.com/contest/675/problem/C 题意: 给一个数组,每个数与他相邻的数相连,第一个与最后一个相连,每个数的数值可以左右移动, ...

  7. Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树

    题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...

  8. Codeforces Round #353 (Div. 2) C. Money Transfers (思维题)

    题目链接:http://codeforces.com/contest/675/problem/C 给你n个bank,1~n形成一个环,每个bank有一个值,但是保证所有值的和为0.有一个操作是每个相邻 ...

  9. Codeforces Round #353 (Div. 2) D. Tree Construction (二分,stl_set)

    题目链接:http://codeforces.com/problemset/problem/675/D 给你一个如题的二叉树,让你求出每个节点的父节点是多少. 用set来存储每个数,遍历到a[i]的时 ...

随机推荐

  1. aarch64_g2

    ghc-cryptonite-devel-0.21-1.fc26.aarch64.rpm 2017-02-28 01:28 3.1M fedora Mirroring Project ghc-css- ...

  2. 简单ORACLE分区表、分区索引

    前一段听说CSDN.COM里面很多好东西,同事建议看看合适自己也可以写一写,呵呵,今天第一次开通博客,随便写点东西,就以第一印象分区表简单写第一个吧. ORACLE对于分区表方式其实就是将表分段存储, ...

  3. supervisor管理uwsgi

    1. 前言 传统的管理uwsgi服务: 1. 通过shell脚本来编写start restart stop函数来控制 2. 比较麻烦,有时候控制写的烂,还会出现意想不到的错误 supervisor进行 ...

  4. 微信小程序蓝牙模块

    蓝牙部分知识 关于Service: 每个设备包含有多个Service,每个Service对应一个uuid 关于Characteristic 每个Service包含多个Characteristic,每个 ...

  5. sass问题

     用sass的minix定义一些代码片段,且可传参数 /** * @module 功能 * @description 生成全屏方法 * @method fullscreen * @version 1. ...

  6. box-sizing 盒子模型不改变大小

    参考 box-sizing 相关说明 在需要设置元素的内边距或者外边距而不想改变整体大小时,可以使用该属性,记录之. .simple { width: 500px; margin: 20px auto ...

  7. html中<b>标签和<Strong>标签的区别

    关于html标签中b和strong两个的区别,我也是今早上才注意的,以前都是混着用的,早上看书的时候才注意到这两个标签的区别. 用在网页上,默认情况下它们起的均是加粗字体的作用,二者所不同的是,< ...

  8. 设置Loadrunner负载机临时文件目录

    设置Loadrunner负载机临时文件目录 最近在跑稳定性测试 3 X 24小时的时候,发现负载机产生的日志还运行记录等等竟然有100多G! C盘空间不足,但是D盘还有700多G空间呢,怎么让临时文件 ...

  9. 终端(terminal)、tty、shell、控制台(console)、bash之间的区别与联系

    1.终端(terminal) 终端(termimal)= tty(Teletypewriter, 电传打印机),作用是提供一个命令的输入输出环境,在linux下使用组合键ctrl+alt+T打开的就是 ...

  10. React Native性能优化之可取消的异步操作

    前沿 在前端的项目开发中,异步操作是一个不可获取的,从用户的角度来说,异步操作所带来的体验是美妙的,但有时候也会带来一些性能隐患.比如说:有一个异步请求还没有返回结果,但是页面却关闭了,这时由于异步操 ...