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的最短距离之 ...
随机推荐
- VIJOS1107 求树的最长链
vijos1107环游大同80天 学习了一下求树的最长链的方法 最简单的思路就是两次dfs 两次dfs分别有什么用呢? 第一次dfs,求出某个任意的点能到达的最远的点 第二次dfs,从所搜到的最远的点 ...
- jQuery实现返回顶部功能
整理两个实现功能,一个是右下角的返回顶部,一个是右侧的返回顶部,分别如图 第一种实现 一.JSP或HTML(主体结构) 在body中添加 <body id=" ...
- SP2-1503: 无法初始化 Oracle 调用界面
问题描述: win7下,cmd运行 输入sqlplus报一下错误 SP2-1503: 无法初始化 Oracle 调用界面 SP2-0152: ORACLE 不能正常工作 解决办法 1.cmd右键--以 ...
- struts2讲义----建立一个struts2工程
建立一个Struts2 工程 Ø 1在MyEclipse中新建web工程 Ø 2在struts-2.2.1.1-all\struts-2.2.1.1解压struts2-blank.war( 最基础的示 ...
- 记录下自己写的gulp打包脚本
var c = { rootPath: 'src',//项目文件夹 outputPath: 'output',//文件编译后的输出目录 revPath: 'manifest',//rev映射文件目录 ...
- js 获取页面内链接
今天有同学问如何用 JS 正则表达式获取一段文本中的超链接,并对超链接进行处理,想了几分钟,写了下面的代码: var re = /https?:\/\/[\w\.:~\-\d\/]+(?:\?[\w\ ...
- http学习笔记2(URL)
http学习笔记(二)—— 嘿!伙计,你在哪?(URL) 我们之所以希望浏览网页,其中一个重要的原因就是庞大的web世界中有很丰富的资源,他就像哆啦a梦的口袋,随时都能拿出我们想要的宝贝.这些资源通过 ...
- 了解OData(一)
了解OData(一) 最近做了一个小项目,其中用到了 WCF Data Service,之前是叫 ADO.NET Data Service 的.关于WCF Data Service,博客园里的介绍并不 ...
- 常用PHP正则表达式
获取所有图片网址preg_match_all(“/ src=(\”|\’){0,}(http:\/\/(.+?))(\”|\’|\s|>)/is”,$text,$img); 匹配中文字符的正则表 ...
- Java Concurrency (1)
Memory that can be shared betweenthreads is called shared memory or heap memory. The term variable a ...