[题目链接]

https://www.lydsy.com/JudgeOnline/problem.php?id=3387

[算法]

f[i][0]表示从第i个栅栏的左端点走到原点的最少移动步数

f[i][1]表示从第i个栅栏的右端点走到原点的最少移动步数

我们可以用线段树优化转移

[代码]

#include<bits/stdc++.h>
using namespace std;
#define MAXN 50010
const int V = ; struct info
{
int x,y;
} a[MAXN]; int i,N,S,x,y;
int f[MAXN][]; struct SegmentTree
{
struct Node
{
int l,r;
int mx,tag;
} Tree[V << ];
inline void build(int index,int l,int r)
{
int mid;
Tree[index].l = l;
Tree[index].r = r;
Tree[index].mx = Tree[index].tag = ;
if (l == r) return;
mid = (l + r) >> ;
build(index << ,l,mid);
build(index << | ,mid + ,r);
}
inline void pushdown(int index)
{
if (Tree[index].tag)
{
Tree[index << ].mx = max(Tree[index << ].mx,Tree[index].tag);
Tree[index << | ].mx = max(Tree[index << | ].mx,Tree[index].tag);
Tree[index << ].tag = max(Tree[index << ].tag,Tree[index].tag);
Tree[index << | ].tag = max(Tree[index << | ].tag,Tree[index].tag);
Tree[index].tag = ;
}
}
inline void modify(int index,int l,int r,int val)
{
int mid;
if (Tree[index].l == l && Tree[index].r == r)
{
Tree[index].mx = Tree[index].tag = val;
return;
}
pushdown(index);
mid = (Tree[index].l + Tree[index].r) >> ;
if (mid >= r) modify(index << ,l,r,val);
else if (mid + <= l) modify(index << | ,l,r,val);
else
{
modify(index << ,l,mid,val);
modify(index << | ,mid + ,r,val);
}
}
inline int query(int index,int pos)
{
int mid;
if (Tree[index].l == Tree[index].r) return Tree[index].mx;
pushdown(index);
mid = (Tree[index].l + Tree[index].r) >> ;
if (mid >= pos) return query(index << ,pos);
else return query(index << | ,pos);
}
} T; int main()
{ scanf("%d%d",&N,&S);
S += V;
for (i = ; i <= N; i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
a[i].x += V;
a[i].y += V;
}
T.build(,,V << );
f[][] = f[][] = ;
a[].x = a[].y = V;
for (i = ; i <= N; i++)
{
x = T.query(,a[i].x);
y = T.query(,a[i].y);
f[i][] = min(f[x][] + abs(a[i].x - a[x].x),f[x][] + abs(a[i].x - a[x].y));
f[i][] = min(f[y][] + abs(a[i].y - a[y].x),f[y][] + abs(a[i].y - a[y].y));
T.modify(,a[i].x,a[i].y,i);
}
printf("%d\n",min(abs(S - a[N].x) + f[N][],abs(S - a[N].y) + f[N][])); return ; }

[BZOJ 3387] Fence Obstacle Course的更多相关文章

  1. 【BZOJ 3387】 线段树= =

    57 跨栏训练为了让奶牛参与运动,约翰建造了 K 个栅栏.每条栅栏可以看做是二维平面上的一条线段,它们都平行于 X 轴.第 i 条栅栏所覆盖的 X 轴坐标的区间为 [ Ai,Bi ], Y 轴高度就是 ...

  2. 【BZOJ3387】[Usaco2004 Dec]Fence Obstacle Course栅栏行动 线段树

    [BZOJ3387][Usaco2004 Dec]Fence Obstacle Course栅栏行动 Description 约翰建造了N(1≤N≤50000)个栅栏来与牛同乐.第i个栅栏的z坐标为[ ...

  3. POJ2374 Fence Obstacle Course

    题意 Language:Default Fence Obstacle Course Time Limit: 3000MS Memory Limit: 65536K Total Submissions: ...

  4. POJ 2374 Fence Obstacle Course(线段树+动态规划)

    Fence Obstacle Course Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 2524   Accepted:  ...

  5. Fence Obstacle Course

    Fence Obstacle Course 有n个区间自下而上有顺序的排列,标号\(1\sim n\),第i个区间记做\([l_i,r_i]\),现在从第n个区间的起点s出发(显然s在\([l_n,r ...

  6. [BZOJ 1724] Fence Repair

    这大概是BZOJ里除了A+B Problem最水的一道题了吧 题面:http://www.lydsy.com/JudgeOnline/problem.php?id=1724 这道题其实有一些思路还是可 ...

  7. BZOJ 3253 Fence Repair 哈夫曼树 水题

    http://poj.org/problem?id=3253 这道题约等于合并果子,但是通过这道题能够看出来哈夫曼树是什么了. #include<cstdio> #include<c ...

  8. poj2374 Fence Obstacle Course[线段树+DP]

    https://vjudge.net/problem/POJ-2374 吐槽.在这题上面磕了许久..英文不好题面读错了qwq,写了个错的算法搞了很久..A掉之后瞥了一眼众多julao题解,**,怎么想 ...

  9. POJ2374 Fence Obstacle Course 【线段树】

    题目链接 POJ2374 题解 题意: 给出\(n\)个平行于\(x\)轴的栅栏,求从一侧栅栏的某个位置出发,绕过所有栅栏到达另一侧\(x = 0\)位置的最短水平距离 往上说都是线段树优化dp 我写 ...

随机推荐

  1. matplotlib之pyplot 学习示例

    现在通过numpy和matplotlib.pyplot 在Python上实现科学计算和绘图,而且和matlab极为相像(效率差点,关键是方便简单) 这里有大量plots代码例子.  1. 简单的绘图( ...

  2. Android 清空缓存

    APP开发中常有计算缓存大小和清空缓存的功能,此功能很常见,几乎每个应用都能看到,下面就用代码来实现此功能: 步骤为: 1.获取缓存路径 获取长时间保存的文件,Context.getExternalF ...

  3. GitHub上fork别人打代码后如何保持和原作者同步的更新

    1.进入你的GitHub发起Pull  request 2.选择compare across  forks 3.反向操作.base fork改为自己的,head fork改为原作者的 4.点击 cre ...

  4. REST、RESTful、SOA

    1.http://www.imooc.com/article/17650 2.SOA面向服务架构

  5. JavaScript函数和window对象

    一.什么是函数 函数的含义:类似于Java中的方法,是完成特定任务的代码语句块 使用更简单:不用定义属于某个类,直接使用 二.常用系统函数 parseInt ("字符串")     ...

  6. Monad (functional programming)

    In functional programming, a monad is a design pattern that defines how functions, actions, inputs, ...

  7. 题解 P2605 【[ZJOI2010]基站选址】(From luoguBlog)

    线段树优化dp 数组f[i][j]表示在前i个村庄内,第j个基站建在i处的最小费用 根据交线牛逼法和王鹤松式可得方程 f[i][j]=min(f[k][j−1]+cost(k,i)) cost(k,i ...

  8. Vue.js大总结

    最近回顾了一下Vue.js的基础知识,把认为重要的几个点简单的罗列了出来 vue渐进式的理解 vue可以开发很多插件,可以把很多插件组合到一起,渐进的增加vue的功能 update beforeUpd ...

  9. Unity中使用摇杆控制

    Unity中使用摇杆控制 本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/50 ...

  10. 洛谷 P2010 回文日期

    Noip2016普及组T2 题目描述 在日常生活中,通过年.月.日这三个要素可以表示出一个唯一确定的日期. 牛牛习惯用8位数字表示一个日期,其中,前4位代表年份,接下来2位代表月 份,最后2位代表日期 ...