CodeForces 675E Trains and Statistic
贪心,递推,线段树,$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的更多相关文章
- Codeforces 675E Trains and Statistic - 线段树 - 动态规划
题目传送门 快速的vjudge通道 快速的Codeforces通道 题目大意 有$n$个火车站,第$i$个火车站出售第$i + 1$到第$a_{i}$个火车站的车票,特殊地,第$n$个火车站不出售车票 ...
- Codeforces 675E Trains and Statistic(DP + 贪心 + 线段树)
题目大概说有n(<=10W)个车站,每个车站i卖到车站i+1...a[i]的票,p[i][j]表示从车站i到车站j所需买的最少车票数,求所有的p[i][j](i<j)的和. 好难,不会写. ...
- codeforces 675E Trains and Statistic 线段树+贪心统计
分析:这个题刚看起来无从下手 但是我们可以先简化问题,首先可以固定起点i,求出i+1到n的最小距离 它可以到达的范围是[i+1,a[i]],贪心的想,我们希望换一次车可以到达的距离尽量远 即:找一个k ...
- codeforces E. Trains and Statistic(线段树+dp)
题目链接:http://codeforces.com/contest/675/problem/E 题意:你可以从第 i 个车站到 [i + 1, a[i]] 之间的车站花一张票.p[i][j]表示从 ...
- CF 675E Trains and Statistic
草稿和一些题解而已 因为指针太恶心了 所以query决定还是要试试自己yy一下 #include<cstdio> #include<cstring> #include<i ...
- codeforces 675E E. Trains and Statistic(线段树+dp)
题目链接: E. Trains and Statistic time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- 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 ...
- 【34.54%】【codeforces 675E】Trains and Statistic
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 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的最短距离之 ...
随机推荐
- 10.26最后的模拟DAY2 改造二叉树[中序遍历+严格递增的最长不下降子序列]
改造二叉树 [题目描述] 小Y在学树论时看到了有关二叉树的介绍:在计算机科学中,二叉树是每个结点最多有两个子结点的有序树.通常子结点被称作“左孩子”和“右孩子”.二叉树被用作二叉搜索树和二叉堆.随后他 ...
- [转]How to create an anonymous IDA PRO database (.IDB)
Source: http://www.0xebfe.net/blog/2013/01/13/how-to-create-an-anonymous-ida-pro-database-dot-idb/ P ...
- JS之For---in 语句
下面说一下for… in语句.可直接把下面的代码复制到浏览器的控制台或Node环境下去执行. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //用来快速迭代对象. var o ...
- C语言面试问答(3)
12个滑稽的C语言面试问答——<12个有趣的C语言问答>评析(3) 前文链接:http://www.cnblogs.com/pmer/p/3322429.html 5,atexit wit ...
- 验证码生成组件--JCaptcha的使用
以下为项目中所用代码,文笔有限,直接上代码. 所需jar包: 是否需要其他依赖包,不详 web.xml <servlet> <servlet-name>Jcaptcha< ...
- 利用WCF与Android实现图片上传并传参
利用WCF与Android实现图片上传并传参 最近做一个项目后端使用WCF接收Android手机拍照并带其它参数保存到服务器里:刚好把最近学习的WCF利用上,本以为是个比较简单的功能应该很好实现,没想 ...
- springMVC3学习(一)--框架搭建
由于项目需要,学习下springMVC,在此简单记录一下. 如有十万个为什么,暂且忽略,待以后研究. 本人是基于3.1.1版本开发,如遇jar包版本冲突等其他问题,概不负责. 下载地址:上传此zip资 ...
- Elmah错误日志工具
Elmah错误日志工具 Exception 对于异常的处理,以前基本就是跳转到一个自定义的错误页面,觉得这样不错挺友好的.同时还是需要记录下来这些异常,才能进一步的进行修改. 怎么去记录这些错误信息呢 ...
- (*p)++ 与 *p++ 与 ++*p 拨开一团迷雾
(*p)++ 与 *p++ 与 ++*p 拨开一团迷雾 环境:win7 IDE:DEV-C++ 编译器:GCC 1.先说++i和i++的基础 代码如下: ? 1 2 3 4 5 6 7 8 9 10 ...
- 用Linux命令行实现删除和复制指定类型的文件
(一)Linux 删除当前目录及子目录中所有某种类型的文件 方法1 : 此方法不能处理目录中带空格的那些. rm -rf `find . -name "*.example"` Li ...