我们可以想到一个dp方程:f[i][0]表示当前在i个栅栏的左端点,f[i][1]表示在右端点。

分两种情况:

第一种:假设现在要更新线段gh的左端点g,而它下来的路径被ef挡住了,那么必定是有ef来更新g。

为什么呢?因为其它点走到g必定要下落,比如说d到g,就相当于d到f再到g。

第二种:假设到ab的路径上没有东西挡着,那就可以直接从源点走过去再直接下落。

按照从上到下的顺序插入线段,线段树就是找当前的某个点被哪条id最大(也就是最低的)线段所覆盖。

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<ctime>
#include<queue>
#include<algorithm>
using namespace std; const int N=**,INF=(int)1e9;
int n,st,pl,tl,r[N],f[N][];
struct node{
int d,id,tmp;
}p[N];
struct nd{
int x1,x2;
}a[N];
struct trnode{
int l,r,lc,rc,id,lazy;
}t[*N]; bool cmp_d(node x,node y){return x.d<y.d;}
int myabs(int x){return x> ? x:-x;}
int minn(int x,int y){return x<y ? x:y;}
int maxx(int x,int y){return x>y ? x:y;} int bt(int l,int r)
{
int x=++tl;
t[x].l=l;t[x].r=r;
t[x].lc=t[x].rc=;
t[x].id=INF;t[x].lazy=INF;
if(l<r)
{
int mid=(l+r)/;
t[x].lc=bt(l,mid);
t[x].rc=bt(mid+,r);
}
return x;
} void upd(int x)
{
if(t[x].lazy==INF) return ;
int id=t[x].lazy,lc=t[x].lc,rc=t[x].rc;
t[x].lazy=INF;
t[x].id=minn(t[x].id,id);
if(lc) t[lc].lazy=minn(t[lc].lazy,id);
if(rc) t[rc].lazy=minn(t[rc].lazy,id);
} void change(int x,int l,int r,int id)
{
upd(x);
if(t[x].l==l && t[x].r==r)
{
t[x].lazy=minn(t[x].lazy,id);
upd(x);
return ;
}
int lc=t[x].lc,rc=t[x].rc,mid=(t[x].l+t[x].r)/;
if(r<=mid) change(lc,l,r,id);
else if(l>mid) change(rc,l,r,id);
else
{
change(lc,l,mid,id);
change(rc,mid+,r,id);
}
} int query(int x,int p)
{
upd(x);
if(t[x].l==t[x].r) return t[x].id;
int lc=t[x].lc,rc=t[x].rc,mid=(t[x].l+t[x].r)/;
if(p<=mid) return query(lc,p);
else return query(rc,p);
} int main()
{
// freopen("a.in","r",stdin);
// freopen("me.out","w",stdout);
freopen("obstacle.in","r",stdin);
freopen("obstacle.out","w",stdout);
scanf("%d%d",&n,&st);
int x,ed;ed=;pl=;tl=;
p[++pl].d=st;p[pl].id=n+;
p[++pl].d=ed;p[pl].id=n+;
for(int i=;i<=n;i++)
{
scanf("%d%d",&a[i].x1,&a[i].x2);
if(a[i].x1>a[i].x2) swap(a[i].x1,a[i].x2);
p[++pl].d=a[i].x1;p[pl].id=i;p[pl].tmp=;
p[++pl].d=a[i].x2;p[pl].id=i;p[pl].tmp=;
}
sort(p+,p++pl,cmp_d);
int mx=;p[].d=INF;
for(int i=;i<=pl;i++)
{
if(p[i].d!=p[i-].d) mx++,r[mx]=p[i].d;
if(p[i].id==n+) st=mx;
else if(p[i].id==n+) ed=mx;
else
{
if(p[i].tmp==) a[p[i].id].x1=mx;
else a[p[i].id].x2=mx;
}
}
bt(,mx);
change(,st,st,n+);
a[n+].x1=a[n+].x2=st;
f[n+][]=f[n+][]=;
a[].x1=a[].x2=ed;
for(int i=n;i>=;i--)
{
x=query(,a[i].x1);
if(x<INF) f[i][]=minn(f[x][]+myabs(r[a[x].x1]-r[a[i].x1]),f[x][]+myabs(r[a[x].x2]-r[a[i].x1]));
else f[i][]=myabs(r[st]-r[a[i].x1]);
x=query(,a[i].x2);
if(x<INF) f[i][]=minn(f[x][]+myabs(r[a[x].x1]-r[a[i].x2]),f[x][]+myabs(r[a[x].x2]-r[a[i].x2]));
else f[i][]=myabs(r[st]-r[a[i].x2]);
change(,a[i].x1,a[i].x2,i);
}
printf("%d\n",f[][]);
return ;
}

【bzoj3387-跨栏训练】线段树+dp的更多相关文章

  1. Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)

    [题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...

  2. HDU 3016 Man Down (线段树+dp)

    HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  3. CodeForces–833B--The Bakery(线段树&&DP)

    B. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...

  4. lightoj1085 线段树+dp

    //Accepted 7552 KB 844 ms //dp[i]=sum(dp[j])+1 j<i && a[j]<a[i] //可以用线段树求所用小于a[i]的dp[j ...

  5. [CF 474E] Pillars (线段树+dp)

    题目链接:http://codeforces.com/contest/474/problem/F 意思是给你两个数n和d,下面给你n座山的高度. 一个人任意选择一座山作为起始点,向右跳,但是只能跳到高 ...

  6. HDU-3872 Dragon Ball 线段树+DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3872 题意:有n个龙珠按顺序放在一列,每个龙珠有一个type和一个权值,要求你把这n个龙珠分成k个段, ...

  7. HDU4521+线段树+dp

    题意:在一个序列中找出最长的某个序列.找出的序列满足题中的条件. 关键:对于 第 i 个位置上的数,要知道与之相隔至少d的位置上的数的大小.可以利用线段树进行统计,查询.更新的时候利用dp的思想. / ...

  8. Codeforces Round #343 (Div. 2) D - Babaei and Birthday Cake 线段树+DP

    题意:做蛋糕,给出N个半径,和高的圆柱,要求后面的体积比前面大的可以堆在前一个的上面,求最大的体积和. 思路:首先离散化蛋糕体积,以蛋糕数量建树建树,每个节点维护最大值,也就是假如节点i放在最上层情况 ...

  9. Special Subsequence(离散化线段树+dp)

    Special Subsequence Time Limit: 5 Seconds      Memory Limit: 32768 KB There a sequence S with n inte ...

随机推荐

  1. Java中ArrayList与数组间相互转换

    在实际的 Java 开发中,如何选择数据结构是一个非常重要的问题. 衡量标准化(读的效率与改的效率) : ① Array: 读快改慢 ② Linked :改快读慢 ③ Hash:介于两者之间 实现Li ...

  2. 《学习OpenCV》课后习题解答4

    题目:(P104) 练习使用感兴趣区域(ROI).创建一个210*210的单通道图像并将其归0.在图像中使用ROI和cvSet()建立一个增长如金字塔状的数组.也就是:外部边界为0,下一个内部边界应该 ...

  3. week1 技术随笔

    类别c 内容c 开始时间s 结束时间e 被打断时间I 总计(min) 9.5 随笔 构建之法福后感 22:00 24:00 7 113 9.6 分析 需求分析 9:00 9:30 2 28 编码 词频 ...

  4. git初始化之git config

    git初始化之git config     1. 下面的命令将修改/home/[username]/.gitconfig文件,也就是说下面的配置只对每一个ssh的用户可见,所以每个人都需要做.   提 ...

  5. activity属性设置大全

    activity属性设置大全 android:allowTaskReparenting=["true" | "false"]         是否允许activ ...

  6. [C/C++] 字符串错题集

    1. 答案:A 这里考查转义字符,注意 \\ 表示字符 \\123表示字符 {\t 表示制表符这些都是一个字符. 2. 答案C 先不看有没有重复的,共5个字母,有5×4×3×2×1 = 120种组合. ...

  7. 【Python】python 调用c语言函数

    虽然python是万能的,但是对于某些特殊功能,需要c语言才能完成.这样,就需要用python来调用c的代码了具体流程:c编写相关函数 ,编译成库然后在python中加载这些库,指定调用函数.这些函数 ...

  8. 【bzoj1782】[Usaco2010 Feb]slowdown 慢慢游 树链剖分+线段树

    题目描述 每天Farmer John的N头奶牛(1 <= N <= 100000,编号1…N)从粮仓走向他的自己的牧场.牧场构成了一棵树,粮仓在1号牧场.恰好有N-1条道路直接连接着牧场, ...

  9. 【题解】SCOI2007组队

    恩……为什么大家都这么执着于 \(O(n^{2})\) 的复杂度捏?如果接受 \(O(n^{2} + nV)\) 的复杂度,那这题可不是道**题吗( • ̀ω•́ )✧ 首先把所有的人按照身高排个序, ...

  10. [洛谷4609] [FJOI2016]建筑师

    题目描述 LOJ题面:https://loj.ac/problem/2173. 洛谷题面:https://www.luogu.org/problemnew/show/P4609. Solution [ ...