[题目链接]

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. 关于jquery的clone()和javascript的cloneNode()

    区别: jquery的clone( ),如果是true就是深克隆,把事件都会克隆过去:如果是false,则仅仅克隆的是结构: javascript的cloneNode( ),如果是true,会将子节点 ...

  2. 【Oracle】恢复重做日志组

    我们在Oracle的日常运维中,有可能会遇到重做日志组丢失的情况.下面我将模拟丢失不同状态的日志组,并分别给出解决办法: 重做日志有以下几种状态,如下: -  CURRENT:此状态表示正在被 LGW ...

  3. jQuery顺序加载图片(终版)

    这一篇是对上一篇(jQuery顺序加载图片(初版)--http://www.cnblogs.com/newbie-cc/p/3707504.html)的改进. function loadImage(i ...

  4. 图像局部显著性—点特征(FREAK)

    参考文章:Freak特征提取算法  圆形区域分割 一.Brisk特征的计算过程(参考对比): 1.建立尺度空间:产生8层Octive层. 2.特征点检测:对这8张图进行FAST9-16角点检测,得到具 ...

  5. 【Android】进程间通信IPC——AIDL

    AIDL官网定义AIDL(Android 接口定义语言)与您可能使用过的其他 IDL 类似. 您可以利用它定义客户端与服务使用进程间通信 (IPC) 进行相互通信时都认可的编程接口. 在 Androi ...

  6. 切换原生appium里面H5页面

    #coding = utf-8from appium import webdriverimport time'''1.手机类型2.版本3.手机的唯一标识 deviceName4.app 包名appPa ...

  7. php第十节课

    数据访问 面向对象:类:由众多对象抽象出来的对象:由类实例化出来的,任何东西都可以看做对象来研究定义类:class 类名{ 成员变量 成员方法} 构造函数:写法特殊 执行特殊,对类里面的成员进行初始化 ...

  8. js实现cookie有效期至当次日凌晨

    实际开发中有要求用户一些行为每天一次,次日开始重新回复功能,一般前端都是通过cookie来记住用户的操作,然后进行判断当日是否还有机会,这时候需要给存储的cookie值一个有效期,让次日自动失效,重新 ...

  9. QT5.5移植全攻略【转】

    一.编译1.到www.qt.io下载源码,qt-everywhere-opensource-src-5.5.0 2.设置编译器或者说平台.编译器是通过xplatform参数指定的,xplatform后 ...

  10. 洛谷 P2412 查单词

    P2412 查单词 题目背景 滚粗了的HansBug在收拾旧英语书,然而他发现了什么奇妙的东西. 题目描述 udp2.T3如果遇到相同的字符串,输出后面的 蒟蒻HansBug在一本英语书里面找到了一个 ...