[题目链接]

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. SQL Server中,with as使用介绍

    一.WITH AS的含义      WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到.有的时候 ...

  2. hibernate_03_session详解

    获得session对象有两种方法: 1)openSession 2)getCurrentSession 如果使用的是getCurrentSession需要在hibernate.cfg.xml文件中进行 ...

  3. 红黑联盟 php相关资讯

    http://www.2cto.com/tag/phpbanben.html

  4. mssql for VSCode Guide

    前言 mssql 出自微软自己的 Visual Studio Code 开源插件,代码托管于 GitHub 上. 不过需要注意的一点是,使用 insert into 语句新增的数据...中文是会乱码的 ...

  5. 与swift协议相关的技术

    一.协议定义与实现: 1.关联类型: 2.协议组合: 3.协议扩展: 4.协议实现. 二.协议使用:

  6. 【剑指Offer】59、按之字形顺序打印二叉树

      题目描述:   请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推.   解题思路:   这道题仍然是二 ...

  7. flex记忆

    ._rebate { display: -webkit-box; display: -moz-box; display: -webkit-flex; display: -moz-flex; displ ...

  8. QBXT春季培训酱油记

    Day-1: 下午回家收拾东西,明天去JN,先通知一下在JN的lll dalao明天去见他,然而手办到了,心情大好啊有没有,有没有! 晚上单曲循环<初音未来的消失>,睡觉的时候哭得稀里哗啦 ...

  9. Windows下Jupyter notebook 修改默认打开(工作、保存)文件夹(路径)

    今天晚上兴致一起突然想看看我写了那么多的ipynb文件都去哪了 首先查了一下,应该是都默认保存到    C:\Users\芩溪儿   路径下了 然后我就想,我是不是得改改啊,总在那跟别的文件夹在一起总 ...

  10. C++ 类型转化(运算符重载函数)和基本运算符重载(自增自减)

    类型转化(运算符重载函数) 用转换构造函数可以将一个指定类型的数据转换为类的对象.但是不能反过来将一个类的对象转换为一个其他类型的数据(例如将一个Complex类对象转换成double类型数据).在C ...