题目链接:

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 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.

 
Examples
 
input
4
4 4 4
output
6
input
5
2 3 5 5
output
17

题意:

a[i]表示从第i个车站可以一张票到第[i+1,a[i]]这些车站;
p[i][j]表示从第i个车站到第j个车站的最少的票数,现在要求∑dp[i][j](1<=i<=n,i<j<=n); 思路: dp[i]=∑p[i][j](i<j<=n);
转移方程为dp[i]=dp[temp]+(n-i)-(a[i]-temp);
其中temp表示[i+1,a[i]]中a[temp]最大的位置; AC代码:
#include <bits/stdc++.h>
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
const LL mod=1e9+;
const double PI=acos(-1.0);
const int inf=0x3f3f3f3f;
const int N=1e5+;
int n,a[N];
LL dp[N];
struct Tree
{
int l,r,ans;
}tr[*N];
void pushup(int o)
{
if(a[tr[*o].ans]>a[tr[*o+].ans])tr[o].ans=tr[*o].ans;
else tr[o].ans=tr[*o+].ans;
}
void build(int o,int L,int R)
{
tr[o].l=L;
tr[o].r=R;
if(L==R)
{
tr[o].ans=L;
return ;
}
int mid=(L+R)>>;
build(*o,L,mid);
build(*o+,mid+,R);
pushup(o);
}
int query(int o,int L,int R)
{
if(L<=tr[o].l&&R>=tr[o].r)return tr[o].ans;
int mid=(tr[o].l+tr[o].r)>>;
if(R<=mid)return query(*o,L,R);
else if(L>mid)return query(*o+,L,R);
else
{
int fl=query(*o,L,mid),fr=query(*o+,mid+,R);
if(a[fl]>a[fr])return fl;
else return fr;
}
}
int main()
{
scanf("%d",&n);
Riep(n-)scanf("%d",&a[i]);
a[n]=n-;
build(,,n);
LL ans=;
dp[n]=;
for(int i=n-;i>;i--)
{
int temp=query(,i+,a[i]);
dp[i]=dp[temp]+(LL)(n-i-(a[i]-temp));
ans+=dp[i];
}
cout<<ans<<"\n"; return ;
}

codeforces 675E 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 675E Trains and Statistic - 线段树 - 动态规划

    题目传送门 快速的vjudge通道 快速的Codeforces通道 题目大意 有$n$个火车站,第$i$个火车站出售第$i + 1$到第$a_{i}$个火车站的车票,特殊地,第$n$个火车站不出售车票 ...

  3. codeforces 675E Trains and Statistic 线段树+贪心统计

    分析:这个题刚看起来无从下手 但是我们可以先简化问题,首先可以固定起点i,求出i+1到n的最小距离 它可以到达的范围是[i+1,a[i]],贪心的想,我们希望换一次车可以到达的距离尽量远 即:找一个k ...

  4. 【34.54%】【codeforces 675E】Trains and Statistic

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. codeforces#426(div1) B - The Bakery (线段树 + dp)

    B. The Bakery   Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought req ...

  6. [Codeforces 266E]More Queries to Array...(线段树+二项式定理)

    [Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...

  7. [Codeforces 280D]k-Maximum Subsequence Sum(线段树)

    [Codeforces 280D]k-Maximum Subsequence Sum(线段树) 题面 给出一个序列,序列里面的数有正有负,有两种操作 1.单点修改 2.区间查询,在区间中选出至多k个不 ...

  8. codeforces 1217E E. Sum Queries? (线段树

    codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...

  9. Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)

    [题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...

随机推荐

  1. 用DependanceProperty做Dynamic换Icon

    1:做Icon用Canvas还是DrawingBrush? Canvas的例子:

  2. 高性能的JavaScript -- 读书笔记

    高性能的JavaScript 一.      加载和运行 将脚本放在底部 脚本下载解析执行时,页面已经加载完成并显示在用户面前 成组脚本 减少外部脚本文件数量,整合成一个文件 延迟脚本 动态脚本元素 ...

  3. cocos2dx libcurl

    转自:http://www.himigame.com/curl-libcurl/878.html 本篇介绍使用libcurl编程的一般原则和一些基本方法.本文主要是介绍 c 语言的调用接口,同时也可能 ...

  4. 启动Tomcat的时候遇到错误

    严重: IOException while loading persisted sessions: java.io.EOFException java.io.EOFException at java. ...

  5. 【C#】ASP.NET网页中添加单点登录功能

    背景 首先,要说明的是,原先需求定义的是,同一个账号只能同时有一个人来登录,如果另外一个登录的话,前一个登陆者就自动被踢掉.本来原先要做成存储到服务器的数据库中,但是后来如果是非正常退出的话 下次就没 ...

  6. 由“大数据量Excel入库高效方式”瞥见“并联系统”之优势

    使用场景:         当你有一个Excel文件,需要把其中的数据高速录入到数据库中,文件中包含10万条以上数据. 设计方案:        我们将整个过程分成三个阶段,A(装载Excel文件). ...

  7. org.apache.catalina.mbeans.ServerLifecycleListener

    Tomcat 启动报错: java.lang.ClassNotFoundException: org.apache.catalina.mbeans.ServerLifecycleListener at ...

  8. [Angular2 Form] Build Select Dropdowns for Angular 2 Forms

    Select Dropdowns in Angular 2 a built with select and option elements. You use *ngFor to loop throug ...

  9. 单片机modebus RTU通信实现,採用C语言,可适用于单片机,VC,安卓等

    当前使用的是STM32+ucos_ii编写的,能够移植到安卓以及VC .NET等方便移植使用,採用modebus poll測试过. 仅仅须要改动响应的通信接口就可以,方便多串口使用 //modebus ...

  10. 大型JavaScript应用程序架构模式

    11月中旬在伦敦举行的jQuery Summit顶级大会上有个session讲的是大型JavaScript应用程序架构,看完PPT以后觉得甚是不错,于是整理一下发给大家共勉. PDF版的PPT下载地址 ...