题目传送门

  快速的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. unity3d生命周期

  2. HTML5中的audio在手机端和 微信端的自动播放

    再做H5页面的时候,发现audio在手机端和微信端添加了autoplay以后还是不可以自动播放,这是因为手机端为了节约流浪所设置的 通常解决方法是给一个交互事件,一定要是交互事件 标签:<aud ...

  3. mysql 5.6 每天凌晨12:00 重置sequence表中的某个值

    #.创建evevt要调用的存储过程update_current_value_procedure delimiter // drop procedure if exists update_current ...

  4. 在caffe-ssd的环境搭建中遇到报错信息:Makefile:588: recipe for target '.build_release/cuda/src/caffe/layers/softmax_loss_layer.o' failed

    错误原因: 1.计算机没有安装GPU 2.有GPU但是NVCCFLAGS设置错误 解决方法: 1.对没有GPU的计算机,需要将Makefile中的CPU之前的#注释去掉,是的caffe运行的处理器进行 ...

  5. 最近点对HDU1007

    利用二分的方法来计算,应该是说利用分治的方法吧! 刚开始感觉时间会爆 后来发现嘎嘎居然没有 ,嗨自己算错了时间: #include <iostream> #include<cstdi ...

  6. 定时调度任务quartz

    依赖 <!-- quartz --> <dependency> <groupId>org.quartz-scheduler</groupId> < ...

  7. noip200605能量项链

    题解: 状态转移方程为:dp[i][j] = max{dp[i][k] + dp[k+1][j] + a[i]*a[k+1]*a[j+1]}, k=i, ..., j-1, i和j都从1开始.dp[i ...

  8. LinkedList 底层实现原理

    LinkedList的底层实现原理 LinkedList 底层数据结构为双向链表,链表结构,基于一个个链表节点Node 1,Inner Class 内部类 private static class N ...

  9. 设计模式之State(状态)(转)

    State的定义: 不同的状态,不同的行为;或者说,每个状态有着相应的行为. 何时使用? State模式在实际使用中比较多,适合"状态的切换".因为我们经常会使用If elseif ...

  10. Compare AURO OtoSys IM100 with OtoSys IM600

    The main difference lies in Mercedes-Benz, VW, Audi software and adapters to work with. Software dif ...