[题目链接]

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. 教材配套PPT初稿

    1-10章初稿,基本完整.有些粗糙,后面可能会稍作调整. 附更新情况如下: 1.增加了第10章内容: 2.第5章增加了一些内容: 3.第3章内容部分更新: 4.增加了第8-9章内容. 订正:更新了第8 ...

  2. weblogic详解

    一.简介 WebLogic是美国Oracle公司出品的一个application server,确切的说是一个基于JAVAEE架构的中间件,WebLogic是用于开发.集成.部署和管理大型分布式Web ...

  3. HDU_5833_高斯消元

    参考自:http://www.cnblogs.com/flipped/p/5771492.html 自己做的时候不知道如何求种数.看了题解,感觉思路灰常巧妙.同时也感觉这是一道好题. 精髓在于转化为线 ...

  4. java mongodb 使用MongoCollection,BasicDBObject 条件查询

    废话不说,上代码 //链接数据库 MongoClient mongoClient = new MongoClient( "172.26.xxx.xxx" , 27017 ); Mo ...

  5. 聚合函数与SQL排序

    聚合查询 通过SQL对数据进行某种操作或计算时需要使用函数(聚合函数,将多行汇为一行). 常用函数(5个): COUNT:计算表中的记录数(行数) SUM: 计算表中数值列中数据的合计值 AVG: 计 ...

  6. 使用Java生成具有安全哈希的QR码

    这是关于如何在Java中使用salt生成QR代码和安全散列字符串的分步教程. 首先,需要一个可以处理QR码的库,我决定使用Zebra Crossing("ZXing")库,因为它简 ...

  7. .Net Core 中X509Certificate2 私钥保存为 pem 的方法

    在自己签发CA证书和颁发X509证书时,私钥通过下面的方法保存为PEM 相关代码可以已经提交在了 https://github.com/q2g/q2g-helper-pem-nuget/pull/13 ...

  8. Java中RunTime.getRunTime().addShutdownHook用法

    今天在阅读Tomcat源码的时候,catalina这个类中使用了下边的代码,不是很了解,所以google了一下,然后测试下方法,Tomcat中的相关代码如下: Runtime.getRuntime() ...

  9. es 存入失败错误

    {, 'error': {'type': 'illegal_argument_exception', 'reason': 'Rejecting mapping update to [bd_date] ...

  10. C#第十五节课

    函数复习 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System. ...