贪心,递推,线段树,$RMQ$。

假设我们记$ans[i]$是以$i$点为起点对答案的贡献,那么答案就是$\sum\limits_{i = 1}^n {ans[i]}$。

$ans[i]$怎么计算呢?

首先,$[i+1,a[i]]$区间上肯定都是$1$(即上图紫线)。

然后在$[i+1,a[i]]$上找到一个$tmp$,使得$tmp$点能够达到的最右端是$[i+1,a[i]]$中最大的,那么$[a[i]+1,a[tmp]]$肯定都是2(即上图绿线)。

然后在$[a[i]+1,a[tmp]]$找一个$tmp2$......依次下去,计算出以$i$为起点对答案的贡献。

但是这样做复杂度太高,需要进行优化。

如果我们知道了$ans[tmp]$,那么就可以$O(1)$知道$ans[i]$,递推一下就可以了。

反过来想,如果我们想知道$ans[i]$,也就是要找到$tmp$,然后从$ans[tmp]$转移过来。

找$tmp$的话可以用线段树,也可以用$RMQ$预处理一下。

$RMQ$:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c=getchar(); x=;
while(!isdigit(c)) c=getchar();
while(isdigit(c)) {x=x*+c-''; c=getchar();}
} const int maxn=;
int a[maxn],n,tmp,dp[maxn][];
LL ans[maxn]; void RMQ_init()
{
for(int i=;i<n;i++) dp[i][]=i;
for(int j=;(<<j)<=n;j++)
for(int i=;i+(<<j)-<n;i++){
if(a[dp[i][j-]]>a[dp[i+(<<(j-))][j-]]) dp[i][j]=dp[i][j-];
else dp[i][j]=dp[i+(<<(j-))][j-];
}
} int RMQ(int L,int R)
{
int k=;
while((<<(k+))<=R-L+) k++;
if(a[dp[L][k]]>a[dp[R-(<<k)+][k]]) return dp[L][k];
return dp[R-(<<k)+][k];
} int main()
{
scanf("%d",&n);
for(int i=;i<n-;i++) scanf("%d",&a[i]),a[i]--;
a[n-]=n-; RMQ_init(); ans[n-]=; LL d=;
for(int i=n-;i>=;i--)
{
tmp=RMQ(i+,a[i]);
ans[i]=ans[tmp]-(a[i]-tmp)+n--a[i]+a[i]-i;
d=d+ans[i];
}
printf("%lld\n",d);
return ;
}

线段树:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c=getchar(); x=;
while(!isdigit(c)) c=getchar();
while(isdigit(c)) {x=x*+c-''; c=getchar();}
} const int maxn=;
int a[maxn],n,s[*maxn],M,tmp;
LL ans[maxn]; void build(int l,int r,int rt)
{
if(l==r) { s[rt]=a[l]; return; }
int m=(l+r)/; build(l,m,*rt); build(m+,r,*rt+);
s[rt]=max(s[*rt],s[*rt+]);
} void f(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R) { M=max(M,s[rt]); return; }
int m=(l+r)/;
if(L<=m) f(L,R,l,m,*rt);
if(R>m) f(L,R,m+,r,*rt+);
} void force(int l,int r,int rt)
{
if(l==r) {tmp=l; return;}
int m=(l+r)/;
if(s[*rt]==M) force(l,m,*rt);
else force(m+,r,*rt+);
} void h(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
if(s[rt]<M) return;
force(l,r,rt); return;
}
int m=(l+r)/;
if(L<=m) h(L,R,l,m,*rt); if(tmp!=-) return;
if(R>m) h(L,R,m+,r,*rt+); if(tmp!=-) return;
} int main()
{
scanf("%d",&n);
for(int i=;i<=n-;i++) scanf("%d",&a[i]); a[n]=n;
build(,n,); ans[n]=; LL d=;
for(int i=n-;i>=;i--)
{
M=tmp=-; f(i+,a[i],,n,); h(i+,a[i],,n,);
ans[i]=ans[tmp]-(a[i]-tmp)+n-a[i]+a[i]-i;
d=d+ans[i];
}
printf("%lld\n",d);
return ;
}

CodeForces 675E Trains and Statistic的更多相关文章

  1. Codeforces 675E Trains and Statistic - 线段树 - 动态规划

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

  2. Codeforces 675E Trains and Statistic(DP + 贪心 + 线段树)

    题目大概说有n(<=10W)个车站,每个车站i卖到车站i+1...a[i]的票,p[i][j]表示从车站i到车站j所需买的最少车票数,求所有的p[i][j](i<j)的和. 好难,不会写. ...

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

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

  4. codeforces E. Trains and Statistic(线段树+dp)

    题目链接:http://codeforces.com/contest/675/problem/E 题意:你可以从第 i 个车站到 [i + 1, a[i]] 之间的车站花一张票.p[i][j]表示从 ...

  5. CF 675E Trains and Statistic

    草稿和一些题解而已 因为指针太恶心了 所以query决定还是要试试自己yy一下 #include<cstdio> #include<cstring> #include<i ...

  6. codeforces 675E E. Trains and Statistic(线段树+dp)

    题目链接: E. Trains and Statistic time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  7. Codeforces Round #353 (Div. 2) E. Trains and Statistic dp 贪心

    E. Trains and Statistic 题目连接: http://www.codeforces.com/contest/675/problem/E Description Vasya comm ...

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

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

  9. 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的最短距离之 ...

随机推荐

  1. 最小生成树算法prim and kruskal

    一.最小生成树定义:  从不同顶点出发或搜索次序不同,可得到不同的生成树  生成树的权:对连通网络来说,边附上权,生成树也带权,我们把生成树各边的权值总和称为生成树的权  最小代价生成树:在一个连通网 ...

  2. js中常见的问题

    js中常见的问题 原文链接 1.js获取select标签选中的值 原生js var obj = document.getElementByIdx_x(”testSelect”); //定位id var ...

  3. [转]LLVM MC Project

    Intro to the LLVM MC Project The LLVM Machine Code (aka MC) sub-project of LLVM was created to solve ...

  4. phantomjs初次认识

    phantomjs初次认识 最近没什么重要的任务,就抽空看了看项目组爬虫小组的代码,因为我们的爬虫主要是以python的scrapy框架为主,看起来比较方便.在看代码的时候看到一个叫phantomjs ...

  5. C#下丢掉.asmx文件的WebService的实现

    C#下丢掉.asmx文件的WebService的实现 我在用.net实现Webservice的时候发现需要一个没有任何用处的.asmx文件,但是却没法删除,这两天我实现一个通过接口时想实现dll直接部 ...

  6. 飘逸的python - zlib压缩存到数据库

    当每天有大量的数据存到kv数据库中去,且value数据很大,于是想压缩后再存进去. 之前提到了 gzip压缩,为什么不直接用gzip呢. 其实更确切的说gzip是一种文件格式,它压缩成gzip文件,而 ...

  7. PHP 数组拼接成字符串

    PHP[知识分享] 数组拼接成字符串 <?php // 格式: [二维数组] Array ( [0] => Array ( [topicid] => 1 ) [1] => Ar ...

  8. [置顶] (游戏编程-04)JAVA版雷电(奇迹冬瓜)

    注:运行环境必须要JDK 先为大家送上游戏截图 接着在最后有代码下载的链接地址 1.游戏开始动画和主界面 关卡与boss 结束画面 代码下载地址 点击打开链接

  9. java调用存储过程超时及DBCP参数配置说明

    问题:            生产环境实时打标超时: 分析原因:        “实时打标java服务中,只创建数据库Connection,没有关闭数据库Connection,导致数据库连接池耗尽,无 ...

  10. $.each()方法,其实挺不错的

    例子为主 html主要代码 <div class="fl search">厂商:<select id="firms"><optio ...