codeforces 675E E. Trains and Statistic(线段树+dp)
题目链接:
E. Trains and Statistic
2 seconds
256 megabytes
standard input
standard output
Vasya commutes by train every day. There are n train stations in the city, and at the i-th station it's possible to buy only tickets to stations from i + 1 to ai inclusive. No tickets are sold at the last station.
Let ρi, j be the minimum number of tickets one needs to buy in order to get from stations i to station j. As Vasya is fond of different useless statistic he asks you to compute the sum of all values ρi, j among all pairs 1 ≤ i < j ≤ n.
The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of stations.
The second line contains n - 1 integer ai (i + 1 ≤ ai ≤ n), the i-th of them means that at the i-th station one may buy tickets to each station from i + 1 to ai inclusive.
Print the sum of ρi, j among all pairs of 1 ≤ i < j ≤ n.
4
4 4 4
6
5
2 3 5 5
17 题意: a[i]表示从第i个车站可以一张票到第[i+1,a[i]]这些车站;
p[i][j]表示从第i个车站到第j个车站的最少的票数,现在要求∑dp[i][j](1<=i<=n,i<j<=n); 思路: dp[i]=∑p[i][j](i<j<=n);
转移方程为dp[i]=dp[temp]+(n-i)-(a[i]-temp);
其中temp表示[i+1,a[i]]中a[temp]最大的位置; AC代码:
#include <bits/stdc++.h>
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
const LL mod=1e9+;
const double PI=acos(-1.0);
const int inf=0x3f3f3f3f;
const int N=1e5+;
int n,a[N];
LL dp[N];
struct Tree
{
int l,r,ans;
}tr[*N];
void pushup(int o)
{
if(a[tr[*o].ans]>a[tr[*o+].ans])tr[o].ans=tr[*o].ans;
else tr[o].ans=tr[*o+].ans;
}
void build(int o,int L,int R)
{
tr[o].l=L;
tr[o].r=R;
if(L==R)
{
tr[o].ans=L;
return ;
}
int mid=(L+R)>>;
build(*o,L,mid);
build(*o+,mid+,R);
pushup(o);
}
int query(int o,int L,int R)
{
if(L<=tr[o].l&&R>=tr[o].r)return tr[o].ans;
int mid=(tr[o].l+tr[o].r)>>;
if(R<=mid)return query(*o,L,R);
else if(L>mid)return query(*o+,L,R);
else
{
int fl=query(*o,L,mid),fr=query(*o+,mid+,R);
if(a[fl]>a[fr])return fl;
else return fr;
}
}
int main()
{
scanf("%d",&n);
Riep(n-)scanf("%d",&a[i]);
a[n]=n-;
build(,,n);
LL ans=;
dp[n]=;
for(int i=n-;i>;i--)
{
int temp=query(,i+,a[i]);
dp[i]=dp[temp]+(LL)(n-i-(a[i]-temp));
ans+=dp[i];
}
cout<<ans<<"\n"; return ;
}
codeforces 675E E. Trains and Statistic(线段树+dp)的更多相关文章
- 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的最短距离之 ...
- Codeforces 675E Trains and Statistic - 线段树 - 动态规划
题目传送门 快速的vjudge通道 快速的Codeforces通道 题目大意 有$n$个火车站,第$i$个火车站出售第$i + 1$到第$a_{i}$个火车站的车票,特殊地,第$n$个火车站不出售车票 ...
- codeforces 675E Trains and Statistic 线段树+贪心统计
分析:这个题刚看起来无从下手 但是我们可以先简化问题,首先可以固定起点i,求出i+1到n的最小距离 它可以到达的范围是[i+1,a[i]],贪心的想,我们希望换一次车可以到达的距离尽量远 即:找一个k ...
- 【34.54%】【codeforces 675E】Trains and Statistic
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- codeforces#426(div1) B - The Bakery (线段树 + dp)
B. The Bakery Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought req ...
- [Codeforces 266E]More Queries to Array...(线段树+二项式定理)
[Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...
- [Codeforces 280D]k-Maximum Subsequence Sum(线段树)
[Codeforces 280D]k-Maximum Subsequence Sum(线段树) 题面 给出一个序列,序列里面的数有正有负,有两种操作 1.单点修改 2.区间查询,在区间中选出至多k个不 ...
- codeforces 1217E E. Sum Queries? (线段树
codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...
- Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)
[题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...
随机推荐
- 说下Fedora下把SpiderMonkey放入Eclipse内编译的过程
首先要知道SpiderMonkey是个什么玩意 详细的可以看看这里(当然,如果你有google翻译的话看起来也一样费劲,你可以在语言那里选择中文.看完了再转回英文-因为中文有很多文档都没有的,比如:B ...
- CMSIS Example - Signal and Yield
/*---------------------------------------------------------------------------- * RL-ARM - RTX *----- ...
- C++程序员的javascript教程
本文主要目的是向c++程序员阐述javascript的编程思想,以及编程中的误区. 变量声明: 1.变量声明的解析早于代码运行.JavaScript引擎的工作方式是,先解析代码,获取所有被声明的变 ...
- Server对象的Execute方法
Server 对象是专门为处理服务器上的特定任务而设计的,它提供了对服务器上的方法和属性的访问,通过调用这些方法和属性的设置,可以允许用户使用服务器上的许多功能,如可以取得服务器运行环境的功能,但最重 ...
- Python int与string之间的转化
string-->int 1.10进制string转化为int int('12') 2.16进制string转化为int int('12', 16) int-->string 1.int转 ...
- JBoss提供的常用的对称加密算法
package com.test.resteasy; import java.io.File; import java.net.MalformedURLException; import java.n ...
- Android6.0动态权限申请步骤以及需要注意的一些坑
因为工作需要,简单研究了一下Android6.0权限申请,在Google提供的sample的基础上,写了一个简单的demo.算是自己的笔记吧,可能会比较混乱,主要是方便以后查看.后期有别的问题,随时更 ...
- iOS开发——数据持久化Swift篇&(二)沙盒文件
沙盒文件 //******************** 5.2 文件操作 func use_FileOperations() { //1.获取程序的Home目录 let homeDirectory = ...
- iOS开发——数据持久化Swift篇&(一)NSUserDefault
NSUserDefault //******************** 5.1 NSUserDefault和对象归档 func useNSUserDefault() { //通过单利来创建一个NSU ...
- 深入了解android平台的jni(二)
Android.mk是Android提供的一种makefile文件,用来指定诸如编译生成so库名.引用的头文件目录.需要编译的.c/.cpp文件和.a静态库文件等.要掌握jni,就必须熟练掌握Andr ...